Node-RED TIPS (Performance#2_http_in)
前回に引き続き、Node-RED Performance です。
今回は http_in node について書いてみます。
http_in
HTTP リクエストを受け付けるエンドポイントになる node ですね。
下の図の一番左、「/test11」と書かれている node がそれです。
http_response
上の図では、途中から分岐して、上段に、無駄に重い処理があります。この処理の完了を待つことなく、200 OK を返したいので、http_response node を、無駄に重い処理の後ろにつなげるのではなく、前で分岐させました。どうです?良さそうですよね?
レスポンスが遅い!
ところが、200 OK が返るまでに 7, 8 秒程度かかります。
???
しかも、あーだこーだやりながら Deploy を繰り返すと、数 ms で返ってくるようになりました。
???
これはイカン!
ということで、下のパターンを試しました。
何も書かれていない function node は、すぐ上にある node と同じです。
[{"id":"c76cd433.8cf378","type":"http in","z":"c6467cff.71db2","name":"","url":"/test11","method":"get","upload":false,"swaggerDoc":"","x":90,"y":40,"wires":[["1dcb4495.1c795b"]]},{"id":"769c069e.221f98","type":"function","z":"c6467cff.71db2","name":"","func":"node.send(msg);\nreturn [ null, msg];","outputs":2,"noerr":0,"x":270,"y":540,"wires":[["19f3e9f.a5fc616"],["c1c711ec.f2f9c"]]},{"id":"640e75ac.99f14c","type":"http response","z":"c6467cff.71db2","name":"","statusCode":"200","headers":{},"x":840,"y":80,"wires":[]},{"id":"a8cb8f75.0b1e9","type":"http in","z":"c6467cff.71db2","name":"","url":"/test13","method":"get","upload":false,"swaggerDoc":"","x":90,"y":240,"wires":[["8ebbfa2.d5bad08"]]},{"id":"ff1dc267.b1a02","type":"http in","z":"c6467cff.71db2","name":"","url":"/test16","method":"get","upload":false,"swaggerDoc":"","x":90,"y":540,"wires":[["769c069e.221f98"]]},{"id":"1dcb4495.1c795b","type":"function","z":"c6467cff.71db2","name":"return msg;","func":"return msg;","outputs":1,"noerr":0,"x":290,"y":40,"wires":[["c5cfc44e.6c97d8","ccfea0dd.ebe0e"]]},{"id":"c5cfc44e.6c97d8","type":"function","z":"c6467cff.71db2","name":"3 ('a'.repeat(1000000000))","func":"b1 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb2 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb3 = Buffer.from('a'.repeat(1000000000), 'utf-8');","outputs":1,"noerr":0,"x":670,"y":40,"wires":[[]]},{"id":"8ebbfa2.d5bad08","type":"function","z":"c6467cff.71db2","name":"return [ msg, msg];","func":"return [msg, msg];","outputs":2,"noerr":0,"x":310,"y":240,"wires":[["3f76d5e5.661a5a"],["3a9aa285.ccde2e"]]},{"id":"3f76d5e5.661a5a","type":"function","z":"c6467cff.71db2","name":"3 ('a'.repeat(1000000000))","func":"b1 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb2 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb3 = Buffer.from('a'.repeat(1000000000), 'utf-8');","outputs":1,"noerr":0,"x":670,"y":240,"wires":[[]]},{"id":"c1c711ec.f2f9c","type":"function","z":"c6467cff.71db2","name":"3 ('a'.repeat(1000000000))","func":"b1 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb2 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb3 = Buffer.from('a'.repeat(1000000000), 'utf-8');","outputs":1,"noerr":0,"x":670,"y":580,"wires":[[]]},{"id":"3e6b2982.76ba96","type":"http in","z":"c6467cff.71db2","name":"","url":"/test12","method":"get","upload":false,"swaggerDoc":"","x":90,"y":140,"wires":[["6e24bd23.a3d064"]]},{"id":"6e24bd23.a3d064","type":"function","z":"c6467cff.71db2","name":"","func":"return msg;","outputs":1,"noerr":0,"x":270,"y":140,"wires":[["820cf9b6.1b2698","2821e4f1.b48eec"]]},{"id":"2821e4f1.b48eec","type":"function","z":"c6467cff.71db2","name":"3 ('a'.repeat(1000000000))","func":"b1 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb2 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb3 = Buffer.from('a'.repeat(1000000000), 'utf-8');","outputs":1,"noerr":0,"x":670,"y":180,"wires":[[]]},{"id":"777d9263.7a53dc","type":"function","z":"c6467cff.71db2","name":"","func":"return [ msg, msg];","outputs":2,"noerr":0,"x":270,"y":340,"wires":[["e64605ec.27aeb8"],["8088429d.50409"]]},{"id":"b99dd3cd.c978","type":"http in","z":"c6467cff.71db2","name":"","url":"/test14","method":"get","upload":false,"swaggerDoc":"","x":90,"y":340,"wires":[["777d9263.7a53dc"]]},{"id":"8088429d.50409","type":"function","z":"c6467cff.71db2","name":"3 ('a'.repeat(1000000000))","func":"b1 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb2 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb3 = Buffer.from('a'.repeat(1000000000), 'utf-8');","outputs":1,"noerr":0,"x":670,"y":380,"wires":[[]]},{"id":"db20d5c4.8f8f98","type":"function","z":"c6467cff.71db2","name":"node.send(msg); return [ null, msg];","func":"node.send(msg);\nreturn [ null, msg];","outputs":2,"noerr":0,"x":360,"y":440,"wires":[["943cecf2.703e"],["a040ebc4.736e68"]]},{"id":"361e72dc.37724e","type":"http in","z":"c6467cff.71db2","name":"","url":"/test15","method":"get","upload":false,"swaggerDoc":"","x":90,"y":440,"wires":[["db20d5c4.8f8f98"]]},{"id":"943cecf2.703e","type":"function","z":"c6467cff.71db2","name":"3 ('a'.repeat(1000000000))","func":"b1 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb2 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb3 = Buffer.from('a'.repeat(1000000000), 'utf-8');","outputs":1,"noerr":0,"x":670,"y":440,"wires":[[]]},{"id":"ccfea0dd.ebe0e","type":"function","z":"c6467cff.71db2","name":"msg.payload = {};","func":"msg.payload = {};\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":80,"wires":[["640e75ac.99f14c"]]},{"id":"820cf9b6.1b2698","type":"function","z":"c6467cff.71db2","name":"msg.payload = {};","func":"msg.payload = {};\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":140,"wires":[["fa15db69.d534d8"]]},{"id":"fa15db69.d534d8","type":"http response","z":"c6467cff.71db2","name":"","statusCode":"200","headers":{},"x":840,"y":140,"wires":[]},{"id":"3a9aa285.ccde2e","type":"function","z":"c6467cff.71db2","name":"msg.payload = {};","func":"msg.payload = {};\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":280,"wires":[["792b9e5.101e26"]]},{"id":"792b9e5.101e26","type":"http response","z":"c6467cff.71db2","name":"","statusCode":"200","headers":{},"x":840,"y":280,"wires":[]},{"id":"e64605ec.27aeb8","type":"function","z":"c6467cff.71db2","name":"msg.payload = {};","func":"msg.payload = {};\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":340,"wires":[["a40f188c.139988"]]},{"id":"a40f188c.139988","type":"http response","z":"c6467cff.71db2","name":"","statusCode":"200","headers":{},"x":840,"y":340,"wires":[]},{"id":"a040ebc4.736e68","type":"function","z":"c6467cff.71db2","name":"msg.payload = {};","func":"msg.payload = {};\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":480,"wires":[["ee8c3fd5.9910e"]]},{"id":"ee8c3fd5.9910e","type":"http response","z":"c6467cff.71db2","name":"","statusCode":"200","headers":{},"x":840,"y":480,"wires":[]},{"id":"19f3e9f.a5fc616","type":"function","z":"c6467cff.71db2","name":"msg.payload = {};","func":"msg.payload = {};\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":540,"wires":[["9b8362e9.84a4f"]]},{"id":"9b8362e9.84a4f","type":"http response","z":"c6467cff.71db2","name":"","statusCode":"200","headers":{},"x":840,"y":540,"wires":[]},{"id":"a5bb321.84e93d","type":"function","z":"c6467cff.71db2","name":"","func":"node.send(msg);\nreturn [ null, msg];","outputs":2,"noerr":0,"x":270,"y":740,"wires":[["ae293ade.607618"],["52fb4159.99405"]]},{"id":"f4d690ca.273d4","type":"http in","z":"c6467cff.71db2","name":"","url":"/test18","method":"get","upload":false,"swaggerDoc":"","x":90,"y":740,"wires":[["a5bb321.84e93d"]]},{"id":"52fb4159.99405","type":"function","z":"c6467cff.71db2","name":"3 ('a'.repeat(1000000000))","func":"b1 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb2 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb3 = Buffer.from('a'.repeat(1000000000), 'utf-8');","outputs":1,"noerr":0,"x":670,"y":780,"wires":[[]]},{"id":"f8545abc.fb5bf8","type":"function","z":"c6467cff.71db2","name":"node.send([ null, msg ]); return msg;","func":"node.send([ null, msg ]);\nreturn msg;","outputs":2,"noerr":0,"x":370,"y":640,"wires":[["bdaa6c8a.c227e"],["2f23c925.618cf6"]]},{"id":"c1a355.dd1a0ca8","type":"http in","z":"c6467cff.71db2","name":"","url":"/test17","method":"get","upload":false,"swaggerDoc":"","x":90,"y":640,"wires":[["f8545abc.fb5bf8"]]},{"id":"bdaa6c8a.c227e","type":"function","z":"c6467cff.71db2","name":"3 ('a'.repeat(1000000000))","func":"b1 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb2 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb3 = Buffer.from('a'.repeat(1000000000), 'utf-8');","outputs":1,"noerr":0,"x":670,"y":640,"wires":[[]]},{"id":"2f23c925.618cf6","type":"function","z":"c6467cff.71db2","name":"msg.payload = {};","func":"msg.payload = {};\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":680,"wires":[["ee0ddcf9.1a525"]]},{"id":"ee0ddcf9.1a525","type":"http response","z":"c6467cff.71db2","name":"","statusCode":"200","headers":{},"x":840,"y":680,"wires":[]},{"id":"ae293ade.607618","type":"function","z":"c6467cff.71db2","name":"msg.payload = {};","func":"msg.payload = {};\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":740,"wires":[["e4bf1949.52ff88"]]},{"id":"e4bf1949.52ff88","type":"http response","z":"c6467cff.71db2","name":"","statusCode":"200","headers":{},"x":840,"y":740,"wires":[]},{"id":"7246cc72.2f4e54","type":"comment","z":"c6467cff.71db2","name":"(*) 7,000 - 8,000 ms","info":"","x":1010,"y":80,"wires":[]},{"id":"85274428.388da8","type":"comment","z":"c6467cff.71db2","name":"7,000 - 8,000 ms","info":"","x":1000,"y":280,"wires":[]},{"id":"d6aefa43.c76408","type":"comment","z":"c6467cff.71db2","name":"7,000 - 8,000 ms","info":"","x":1000,"y":480,"wires":[]},{"id":"68c87e77.1b0b4","type":"comment","z":"c6467cff.71db2","name":"4 -7 ms","info":"","x":970,"y":340,"wires":[]},{"id":"6c9cf3a6.87b47c","type":"comment","z":"c6467cff.71db2","name":"4 -7 ms","info":"","x":970,"y":540,"wires":[]},{"id":"20d2a849.eb7ca8","type":"comment","z":"c6467cff.71db2","name":"4 -7 ms","info":"","x":970,"y":680,"wires":[]},{"id":"de3b9725.0d9a28","type":"comment","z":"c6467cff.71db2","name":"4 -7 ms","info":"","x":970,"y":740,"wires":[]},{"id":"e4ef3434.27f8f8","type":"comment","z":"c6467cff.71db2","name":"(*) 4 -7 ms","info":"","x":980,"y":140,"wires":[]}]
右端にある comment は、ブラウザでアクセスしてレスポンスが返るまでの時間です。
よく見てみると。。。
(*) 付きの、1 つ目と 2 つ目は一旦無視して、output が 2 つ以上の場合、
・明示的に出力順を調整した場合、その順に従って処理され、
・明示的に出力順を調整しない場合、output 1 から順に処理される、
ようです。
output が 1 つで分岐している場合は?
これがやっかい。見た目では順番がわかるわけもなく、当然、GUI 上の位置が関係するはずもないので、flows.json の wires を確認しながら実行すると、やっぱり、単純に wires 配列内の要素の順番でした。
{
"id": "1dcb4495.1c795b",
"type": "function",
"z": "c6467cff.71db2",
"name": "return msg;",
"func": "return msg;",
"outputs": 1,
"noerr": 0,
"x": 290,
"y": 40,
"wires": [
[
"c5cfc44e.6c97d8",
"ccfea0dd.ebe0e"
]
]
},
{
"id": "c5cfc44e.6c97d8",
"type": "function",
"z": "c6467cff.71db2",
"name": "3 ('a'.repeat(1000000000))",
"func": "b1 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb2 = Buffer.from('a'.repeat(1000000000), 'utf-8');\nb3 = Buffer.from('a'.repeat(1000000000), 'utf-8');",
"outputs": 1,
"noerr": 0,
"x": 670,
"y": 40,
"wires": [
[]
]
},
{
"id": "ccfea0dd.ebe0e",
"type": "function",
"z": "c6467cff.71db2",
"name": "msg.payload = {};",
"func": "msg.payload = {};\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 650,
"y": 80,
"wires": [
[
"640e75ac.99f14c"
]
]
},
wires 配列内の要素の順序は、どう決まる?
これも単純。1 つ目を繋いで Deploy して、後から 2 つ目を Deploy すれば、[1 つ目, 2 つ目] になりました。ただ、これがどこまで保証されるかはわかりません。。。
まとめ
見た感じよくわからないのは困るので、分岐させる場合は output を 2 つ以上作って、出力順を制御することにします。
というか、シングルスレッドだし、json の構造どおりだし、そりゃそうだよね、って感じでしたね!
この記事が気に入ったらサポートをしてみませんか?