Repository navigation
usockets: bound the accepts of one listener readiness event #44198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
9
commits into
main
Choose a base branch
from
robobun/693d0b54/accept-budget
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d20041a
usockets: bound the accepts of one listener readiness event
robobun be24e8c
bench: add a server snippet for connection-per-request load
robobun 2143b8a
[autofix.ci] apply automated fixes
autofix-ci[bot] 08a6969
bench: answer in the raw servers when the request head is complete
robobun e57577b
test: drop the low-priority queue test, bound the poll sampler
robobun 69cae5c
test: require the poll count to rise by the client count and stay
robobun 580200d
test: destroy the accepted sockets of the node:tls kind in stop()
robobun 4802a75
bench: read the CPU baseline after the report line
robobun 47c8d50
test: report the listener kinds that Windows skips
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // A server for connection-per-request load: each client opens a connection, sends one | ||
| // request and closes it. `ab` does this when it runs without -k. | ||
| // | ||
| // bun http-connection-per-request.mjs [Bun.serve | Bun.listen | node:http | node:net] | ||
| // node http-connection-per-request.mjs [node:http | node:net] | ||
| // ab -n 100000 -c 64 http://127.0.0.1:3000/ | ||
| // | ||
| // Each second with requests it prints the count and the CPU time of the server per request. | ||
| import http from "node:http"; | ||
| import net from "node:net"; | ||
|
|
||
| const kind = process.argv[2] ?? (typeof Bun === "undefined" ? "node:http" : "Bun.serve"); | ||
| const port = Number(process.env.PORT ?? 3000); | ||
| const body = "Hello, World!"; | ||
| const response = `HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: ${body.length}\r\nConnection: close\r\n\r\n${body}`; | ||
|
|
||
| let requests = 0; | ||
|
|
||
| // The head of a request can arrive in more than one chunk. The raw servers answer when it is | ||
| // complete, as the HTTP servers do. `head` is null after the answer. | ||
| function complete(head, chunk) { | ||
| head += chunk.toString("latin1"); | ||
| return head.includes("\r\n\r\n") ? null : head; | ||
| } | ||
|
|
||
| switch (kind) { | ||
| case "Bun.serve": | ||
| Bun.serve({ | ||
| port, | ||
| fetch() { | ||
| requests++; | ||
| return new Response(body); | ||
| }, | ||
| }); | ||
| break; | ||
| case "Bun.listen": | ||
| Bun.listen({ | ||
| hostname: "0.0.0.0", | ||
| port, | ||
| socket: { | ||
| open(socket) { | ||
| socket.data = ""; | ||
| }, | ||
| data(socket, chunk) { | ||
| if (socket.data === null) return; | ||
| socket.data = complete(socket.data, chunk); | ||
| if (socket.data !== null) return; | ||
| requests++; | ||
| socket.end(response); | ||
| }, | ||
| }, | ||
| }); | ||
| break; | ||
| case "node:http": | ||
| http | ||
| .createServer((req, res) => { | ||
| requests++; | ||
| res.end(body); | ||
| }) | ||
| .listen(port); | ||
| break; | ||
| case "node:net": | ||
| net | ||
| .createServer(socket => { | ||
| let head = ""; | ||
| socket.on("error", () => {}); | ||
| socket.on("data", chunk => { | ||
| if (head === null) return; | ||
| head = complete(head, chunk); | ||
| if (head !== null) return; | ||
| requests++; | ||
| socket.end(response); | ||
| }); | ||
| }) | ||
| .listen(port); | ||
| break; | ||
| default: | ||
| throw new Error(`unknown server "${kind}"`); | ||
| } | ||
|
|
||
| console.log(`${kind} on port ${port}`); | ||
|
|
||
| let last = { requests, cpu: process.cpuUsage() }; | ||
| setInterval(() => { | ||
| const cpu = process.cpuUsage(); | ||
| const count = requests - last.requests; | ||
| if (count) { | ||
| const micros = cpu.user - last.cpu.user + (cpu.system - last.cpu.system); | ||
| console.log(`${count} requests, ${(micros / count).toFixed(1)} us of CPU per request`); | ||
| } | ||
| // Read after the report, so that the next interval does not pay for this one's log line. | ||
| last = { requests, cpu: process.cpuUsage() }; | ||
| }, 1000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.