Public: anyone can read this; the crew can change it.
"a quiet writer costs almost no CPU while the reader waits" passes every time on a loaded check machine #5232
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/5208-a-quiet-writer-costs-almost-no-cpu-while → main 1 commit fork
1 file
changed +27
−14
test/pull-pipe.test.ts +27 −14
| @@ -21,7 +21,7 @@ test("a child writing 200 MB stays blocked while nobody reads, and memory stays | ||
| 21 | 21 | expect(total).toBe(200 * MB); |
| 22 | 22 | expect(await proc.exited).toBe(0); |
| 23 | 23 | expect(process.memoryUsage.rss() - before).toBeLessThan(64 * MB); |
| 24 | }); | |
| 24 | }, 30_000); | |
| 25 | 25 | |
| 26 | 26 | test("a reader that takes one chunk and stalls holds git blocked, memory bounded", async () => { |
| 27 | 27 | const before = process.memoryUsage.rss(); |
| @@ -62,17 +62,30 @@ test("cancelling the stream tells the caller to stop the writer and closes the p | ||
| 62 | 62 | expect(proc.signalCode).toBe("SIGKILL"); |
| 63 | 63 | }); |
| 64 | 64 | |
| 65 | test("a quiet writer costs almost no CPU while the reader waits", async () => { | |
| 66 | const pipe = await pullPipe(); | |
| 67 | const proc = Bun.spawn(["sleep", "2"], { | |
| 68 | stdin: "ignore", | |
| 69 | stdout: pipe.writeFd, | |
| 70 | stderr: "ignore", | |
| 71 | }); | |
| 72 | pipe.sealed(); | |
| 73 | const before = process.cpuUsage(); | |
| 74 | for await (const _ of pipe.stream) void _; | |
| 75 | const used = process.cpuUsage(before); | |
| 76 | expect(await proc.exited).toBe(0); | |
| 77 | expect((used.user + used.system) / 1000).toBeLessThan(100); | |
| 65 | test("a quiet writer costs almost no wakeups while the reader waits", async () => { | |
| 66 | // Process CPU time also counts every other thing a loaded machine runs, so | |
| 67 | // count what the pipe itself does: each poll of a quiet writer is one | |
| 68 | // Bun.sleep. Backing off to 100 ms makes that ~25 over 2 s; a busy poll | |
| 69 | // would make hundreds. | |
| 70 | const realSleep = Bun.sleep; | |
| 71 | let sleeps = 0; | |
| 72 | Bun.sleep = ((ms: Parameters<typeof Bun.sleep>[0]) => { | |
| 73 | sleeps++; | |
| 74 | return realSleep(ms); | |
| 75 | }) as typeof Bun.sleep; | |
| 76 | try { | |
| 77 | const pipe = await pullPipe(); | |
| 78 | const proc = Bun.spawn(["sleep", "2"], { | |
| 79 | stdin: "ignore", | |
| 80 | stdout: pipe.writeFd, | |
| 81 | stderr: "ignore", | |
| 82 | }); | |
| 83 | pipe.sealed(); | |
| 84 | for await (const _ of pipe.stream) void _; | |
| 85 | expect(await proc.exited).toBe(0); | |
| 86 | } finally { | |
| 87 | Bun.sleep = realSleep; | |
| 88 | } | |
| 89 | expect(sleeps).toBeGreaterThan(0); | |
| 90 | expect(sleeps).toBeLessThan(40); | |
| 78 | 91 | }); |
new above
offline