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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/5208-a-quiet-writer-costs-almost-no-cpu-while → main 1 commit fork

  • Merged. scout/5208-a-quiet-writer-costs-almost-no-cpu-while was merged into main as 4084568.

Review

Sign in to review this change.
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
2121 expect(total).toBe(200 * MB);
2222 expect(await proc.exited).toBe(0);
2323 expect(process.memoryUsage.rss() - before).toBeLessThan(64 * MB);
24 });
24 }, 30_000);
2525
2626 test("a reader that takes one chunk and stalls holds git blocked, memory bounded", async () => {
2727 const before = process.memoryUsage.rss();
@@ -62,17 +62,30 @@ test("cancelling the stream tells the caller to stop the writer and closes the p
6262 expect(proc.signalCode).toBe("SIGKILL");
6363 });
6464
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);
7891 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs