Title: Performance issue when sending large responses for concurrent requests · Issue #37680 · nodejs/node · GitHub
Open Graph Title: Performance issue when sending large responses for concurrent requests · Issue #37680 · nodejs/node
X Title: Performance issue when sending large responses for concurrent requests · Issue #37680 · nodejs/node
Description: Version: 12.20.1 Platform: Linux xxx 5.4.0-1038-aws #4018.04.1-Ubuntu SMP Sat Feb 6 01:56:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux, or Darwin xxx 19.6.0 Darwin Kernel Version 19.6.0: Tue Jan 12 22:13:05 PST 2021; root:xnu-6153.141.161/...
Open Graph Description: Version: 12.20.1 Platform: Linux xxx 5.4.0-1038-aws #4018.04.1-Ubuntu SMP Sat Feb 6 01:56:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux, or Darwin xxx 19.6.0 Darwin Kernel Version 19.6.0: Tue Jan 12 2...
X Description: Version: 12.20.1 Platform: Linux xxx 5.4.0-1038-aws #4018.04.1-Ubuntu SMP Sat Feb 6 01:56:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux, or Darwin xxx 19.6.0 Darwin Kernel Version 19.6.0: Tue Jan 12 2...
Opengraph URL: https://github.com/nodejs/node/issues/37680
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Performance issue when sending large responses for concurrent requests","articleBody":"\u003c!--\r\nThank you for reporting an issue.\r\n\r\nThis issue tracker is for bugs and issues found within Node.js core.\r\nIf you require more general support please file an issue on our help\r\nrepo. https://github.com/nodejs/help\r\n\r\n\r\nPlease fill in as much of the template below as you're able.\r\n\r\nVersion: output of `node -v`\r\nPlatform: output of `uname -a` (UNIX), or output of `\"$([Environment]::OSVersion | ForEach-Object VersionString) $(if ([Environment]::Is64BitOperatingSystem) { \"x64\" } else { \"x86\" })\"` in PowerShell console (Windows)\r\nSubsystem: if known, please specify affected core module name\r\n--\u003e\r\n\r\n* **Version**: 12.20.1\r\n* **Platform**: Linux xxx 5.4.0-1038-aws #40~18.04.1-Ubuntu SMP Sat Feb 6 01:56:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux, or Darwin xxx 19.6.0 Darwin Kernel Version 19.6.0: Tue Jan 12 22:13:05 PST 2021; root:xnu-6153.141.16~1/RELEASE_X86_64 x86_64\r\n* **Subsystem**:\r\n\r\n### What steps will reproduce the bug?\r\n\r\n\u003c!--\r\nEnter details about your bug, preferably a simple code snippet that can be\r\nrun using `node` directly without installing third-party dependencies.\r\n--\u003e\r\n\r\n```\r\nconst express = require(\"express\");\r\n\r\nfunction makeBigObject(leaves, depth) {\r\n if (depth === 0) {\r\n return \"howdy\";\r\n } else {\r\n const ret = {};\r\n for (let i = 0; i \u003c leaves; ++i) {\r\n ret[i] = makeBigObject(leaves, depth - 1);\r\n }\r\n return ret;\r\n }\r\n}\r\n\r\nfunction getTimeMs() {\r\n return Date.now() - firstRequestStartTime;\r\n}\r\n\r\nconst app = express();\r\n\r\nconst bigObject = makeBigObject(2000, 2);\r\nlet requestCount = 0;\r\nlet firstRequestStartTime;\r\n\r\nasync function requestHandler({ requestIndex, req, res }) {\r\n if (requestIndex === 1) {\r\n firstRequestStartTime = Date.now();\r\n }\r\n\r\n console.log(\r\n `[${getTimeMs()}] Serializing response for request ${requestIndex}...`\r\n );\r\n const serializedBigObject = JSON.stringify(bigObject);\r\n\r\n const flushStartTimeMs = Date.now();\r\n res.on(\"finish\", () =\u003e {\r\n const flushDurationMs = Date.now() - flushStartTimeMs;\r\n console.log(\r\n `[${getTimeMs()}] -- Took ${flushDurationMs}ms to flush response for request ${requestIndex} --`\r\n );\r\n });\r\n\r\n console.log(\r\n `[${getTimeMs()}] Sending ${Math.round(\r\n serializedBigObject.length / 1024 / 1024\r\n )}MB response for request ${requestIndex}...`\r\n );\r\n res.send(serializedBigObject);\r\n // res.send(\"ok\");\r\n\r\n console.log(`[${getTimeMs()}] - Handler done for request ${requestIndex} -`);\r\n}\r\n\r\napp.get(\"/\", async (req, res) =\u003e {\r\n const requestIndex = ++requestCount;\r\n requestHandler({ requestIndex, req, res });\r\n});\r\n\r\napp.listen(\"/tmp/sock\", () =\u003e\r\n console.log(`Example app listening on Unix domain socket /tmp/sock!`)\r\n);\r\n```\r\n\r\n### How often does it reproduce? Is there a required condition?\r\n\r\nSend multiple concurrent requests to the app. For example,\r\n\r\n(for x in $(seq 5); do time curl --unix-socket /tmp/sock http://localhost/ \u003e /dev/null \u0026 done)\r\n\r\n### What is the expected behavior?\r\n\r\n\u003c!--\r\nIf possible please provide textual output instead of screenshots.\r\n--\u003e\r\n\r\nI expect that responses are flushed fairly fast and early requests don't \"wait\" for later requests to flush together\r\n\r\n[0] Processing request 1...\r\n[23] Serializing response for request 1...\r\n[390] Sending 55MB response for request 1...\r\n[536] - Handler done for request 1 -\r\n[537] Processing request 2...\r\n[547] -- Took 158ms to flush response for request 1 --\r\n[558] Serializing response for request 2...\r\n[925] Sending 55MB response for request 2...\r\n[1065] - Handler done for request 2 -\r\n[1065] Processing request 3...\r\n[1077] -- Took 152ms to flush response for request 2 --\r\n[1086] Serializing response for request 3...\r\n[1444] Sending 55MB response for request 3...\r\n[1568] - Handler done for request 3 -\r\n[1568] Processing request 4...\r\n[1579] -- Took 135ms to flush response for request 3 --\r\n[1589] Serializing response for request 4...\r\n[1948] Sending 55MB response for request 4...\r\n[2075] - Handler done for request 4 -\r\n[2075] Processing request 5...\r\n[2087] -- Took 139ms to flush response for request 4 --\r\n[2096] Serializing response for request 5...\r\n[2455] Sending 55MB response for request 5...\r\n[2581] - Handler done for request 5 -\r\n[2591] -- Took 136ms to flush response for request 5 --\r\n\r\n\r\n### What do you see instead?\r\n\r\n\u003c!--\r\nIf possible please provide textual output instead of screenshots.\r\n--\u003e\r\n\r\nEarlier requests were blocked and not making progress until the later requests finished processing/serializing. The more concurrent requests, the longer the delay and the worse the problem is.\r\n\r\n[0] Serializing response for request 1...\r\n[334] Sending 55MB response for request 1...\r\n[491] - Handler done for request 1 -\r\n[492] Serializing response for request 2...\r\n[820] Sending 55MB response for request 2...\r\n[957] - Handler done for request 2 -\r\n[958] Serializing response for request 3...\r\n[1292] Sending 55MB response for request 3...\r\n[1430] - Handler done for request 3 -\r\n[1432] Serializing response for request 4...\r\n[1763] Sending 55MB response for request 4...\r\n[1902] - Handler done for request 4 -\r\n[1910] Serializing response for request 5...\r\n[2229] Sending 55MB response for request 5...\r\n[2371] - Handler done for request 5 -\r\n[2536] -- Took 773ms to flush response for request 4 --\r\n[2538] -- Took 309ms to flush response for request 5 --\r\n[2539] -- Took 1719ms to flush response for request 2 --\r\n[2540] -- Took 2206ms to flush response for request 1 --\r\n[2540] -- Took 1248ms to flush response for request 3 --\r\n\r\n### Additional information\r\n\r\n\u003c!--\r\nTell us anything else you think we should know.\r\n--\u003e\r\n\r\nif replacing res.send(serializedBigObject) with res.send(\"ok\"), then you get expected behavior. So response size matters.\r\n\r\n[0] Serializing response for request 1...\r\n[341] Sending 55MB response for request 1...\r\n[345] - Handler done for request 1 -\r\n[346] -- Took 5ms to flush response for request 1 --\r\n[348] Serializing response for request 2...\r\n[660] Sending 55MB response for request 2...\r\n[661] - Handler done for request 2 -\r\n[661] -- Took 1ms to flush response for request 2 --\r\n[661] Serializing response for request 3...\r\n[976] Sending 55MB response for request 3...\r\n[977] - Handler done for request 3 -\r\n[977] -- Took 1ms to flush response for request 3 --\r\n[977] Serializing response for request 4...\r\n[1296] Sending 55MB response for request 4...\r\n[1297] - Handler done for request 4 -\r\n[1297] -- Took 1ms to flush response for request 4 --\r\n[1297] Serializing response for request 5...\r\n[1633] Sending 55MB response for request 5...\r\n[1633] - Handler done for request 5 -\r\n[1634] -- Took 1ms to flush response for request 5 --\r\n","author":{"url":"https://github.com/xue-cai","@type":"Person","name":"xue-cai"},"datePublished":"2021-03-09T20:28:37.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/37680/node/issues/37680"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:51014bc4-2e8b-a0ed-2ff3-2e886103182b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 81B4:14011B:528C0C0:74DCFE3:6A4FE83B |
| html-safe-nonce | 6e48620088ae1ee5461a70c3d225d2759cf36ec813b38a7b5cbb303e30f25479 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MUI0OjE0MDExQjo1MjhDMEMwOjc0RENGRTM6NkE0RkU4M0IiLCJ2aXNpdG9yX2lkIjoiNDU1MDk5NjA4MzYxOTEyOTQwMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f73ce29f17865cc35f493732b4a88e6ffa80f6f124c164b72ccf506b18782dda |
| hovercard-subject-tag | issue:826588684 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/37680/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3b61451c5d21a562fc16f43ad1dea1088cca83cf685545f3b15556cad41e6012/nodejs/node/issues/37680 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3b61451c5d21a562fc16f43ad1dea1088cca83cf685545f3b15556cad41e6012/nodejs/node/issues/37680 |
| og:image:alt | Version: 12.20.1 Platform: Linux xxx 5.4.0-1038-aws #4018.04.1-Ubuntu SMP Sat Feb 6 01:56:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux, or Darwin xxx 19.6.0 Darwin Kernel Version 19.6.0: Tue Jan 12 2... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | xue-cai |
| hostname | github.com |
| expected-hostname | github.com |
| None | 19b98c3d9767bb8a56238a89ff8401d1cbdf3bc5c353799aacf49fe6c76b4b7c |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 142593e66413bbae27fd485859993597d5a3b104 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width