Public: anyone can read this; the crew can change it.

The bundle's boot is held by counted work: the imports it resolves on the way to /readyz, against the source boot's #17347

opened this

Merged 37h 2m ago, live in production

fix/bundle-boot-counted → main 0 commits 1 thread · 1 unresolved

  • Merged. fix/bundle-boot-counted was merged into main as 57790fd.

Review

Sign in to review this change.
4 files changed +62 −28
docs/features/bundle.md +1 −1
@@ -8,4 +8,4 @@ row: Boot the forge from a bundle
88
99 Spawn to `/readyz` is 1.3-1.55 s against 3.8-4.4 s for `bun src/main.ts`, because the loader skips 3,985 modules. No dependency is added.
1010
11 `test/bundle-workers.test.ts` loads each worker from the bundle; `test/bundle-boot-time.test.ts` holds the boot to 1.5 s with a stated 3x margin for a loaded box. `forge.json` `run` and the release swap still boot the source: that is the owner's call (#17019).
11 `test/bundle-workers.test.ts` loads each worker from the bundle; `test/bundle-boot-time.test.ts` boots the bundle and the source and counts the imports each resolves on the way to `/readyz` (the bundle 1, the source 8,592, on the Mac and on a runner alike): counted work, the same on any machine; its header has the numbers and the bound. `forge.json` `run` and the release swap still boot the source: that is the owner's call (#17019).
scripts/gate.ts +1 −1
@@ -55,7 +55,7 @@ export const SLOW: readonly Exclusion[] = [
5555 { file: "test/shared-density.test.ts", reason: "spawns a fresh bun child that loads the whole app and makes forges to read its own RSS; 2 s alone, 2.3 s a test under full-suite load", issue: 16857 },
5656 { file: "test/bundle-boot.test.ts", reason: "bundles src/main.ts with bun build --minify and boots the one file with no source beside it; 2.6 s a test under full-suite load", issue: 16853 },
5757 { file: "test/bundle-workers.test.ts", reason: "bundles src/main.ts and every worker, then loads each worker from the bundle; 4 s a test under full-suite load", issue: 17019 },
58 { file: "test/bundle-boot-time.test.ts", reason: "bundles src/main.ts and boots it up to three times on an empty directory to time spawn to /readyz; 5 s a test alone", issue: 17019 },
58 { file: "test/bundle-boot-time.test.ts", reason: "bundles src/main.ts and boots it and the source once each on an empty directory, counting the imports each resolves to /readyz; 2 s a test alone", issue: 17019 },
5959 { file: "test/boot-idle-memory.test.ts", reason: "boots the real src/main.ts and waits real seconds for its RSS to settle; 8.6 s a test", issue: 16851 },
6060 { file: "test/boot-lazy.test.ts", reason: "imports the whole MCP module and its tools to compare their routes with the lazy list; 2.2 s alone, 2.5 s under full-suite load", issue: 17021 },
6161 { file: "test/landsim.test.ts", reason: "runs seeded landing simulations over real repositories and queues; 16 s a file alone, 28.8 s and 2.8 s a test under full-suite load", issue: 13765 },
test/bundle-boot-time.test.ts +42 −26
@@ -1,12 +1,15 @@
11 /**
2 * The boot the forge can run, the bundle, goes from spawn to `/readyz` 200 on an empty data
3 * directory under 1.5 s (#17019). Source `bun src/main.ts` takes 3.8-4.4 s of module loading on the
4 * crew box; the bundle 1.3-1.55 s.
2 * The boot the forge can run, the bundle, is the cheap path to `/readyz` 200 (#17019): source `bun
3 * src/main.ts` spends its boot resolving and loading thousands of modules, and the bundle does not.
4 * Counted, not timed: each boot runs with a preload that counts the imports it resolves
5 * (`helpers/count-imports.ts`), and at `/readyz` 200 says the count. The same work on any machine,
6 * so the bound reads the same on the Mac and on a runner. Two timed versions failed on runners: a
7 * wall-clock bound (busy runners) and a CPU share (about 1.0 on Linux, #17320, reverted in #17346).
58 *
6 * The margin: the gate box runs many shards at once, and the measure was on a loaded box too, so
7 * the test holds the boot to BUDGET_MS x MARGIN (4.5 s): still well under the source boot, so it
8 * fails if the bundle stops being the cheap path. Up to three boots, the fastest counts, since a
9 * stall on a loaded box is not the bundle's. The times are in the failure message.
9 * Measured on the Mac and on a runner alike (#17347's check, 2026-12-02: "imports resolved to
10 * ready: bundle 1; source 8592; share 0.0001"): the bundle 1 import resolved, the source 8,592.
11 * The bound: the bundle resolves under a hundredth of what the source does. The numbers print on
12 * every run, so each check's log carries its machine's.
1013 *
1114 * @tree-scan: boots all of src/ through a bundle of main.ts.
1215 */
@@ -20,34 +23,42 @@ import { collectOutput } from "./helpers/collect-output.ts";
2023 import { waitFor } from "./helpers/poll.ts";
2124 import { productionEnv } from "./helpers/production-env.ts";
2225
23 export const BUDGET_MS = 1500;
24 export const MARGIN = 3;
25 const TRIES = 3; // a loaded box stalls one boot; the fastest of a few shows the bundle
26 /** The most of the source boot's resolved imports the bundle may resolve. */
27 export const SHARE = 1 / 100;
2628
27 const root = mkdtempSync(join(tmpdir(), "forge-bundle-time-"));
29 const root = mkdtempSync(join(tmpdir(), "forge-bundle-imports-"));
2830 afterAll(() => rmSync(root, { recursive: true, force: true }));
31 const SOURCE = join(import.meta.dir, "..", "src", "main.ts");
32 const COUNTER = join(import.meta.dir, "helpers", "count-imports.ts");
33 const SAID = /imports resolved: (\d+)/;
2934
30 /** One boot: ms from spawn to `/readyz` 200, on a fresh empty data directory. */
31 async function boot(main: string, n: number): Promise<number> {
32 const dir = join(root, `run${n}`);
35 /** One boot of `main` to `/readyz` 200: the imports it resolved on the way. */
36 async function boot(main: string, label: string): Promise<number> {
37 const dir = join(root, label);
3338 mkdirSync(join(dir, "data"), { recursive: true });
3439 const caFile = join(dir, "ca.crt");
3540 writeFileSync(caFile, "-----BEGIN CERTIFICATE-----\nMIIBfake\n-----END CERTIFICATE-----\n");
3641 const port = await freePort();
3742 const env: Record<string, string> = { PATH: process.env["PATH"] ?? "", ...productionEnv({ dataDir: join(dir, "data"), port, sshPort: await freePort(), caFile }) };
3843 for (const key of ["HOME", "TMPDIR", "FORGE_CELLS", "FORGE_TEST_SOCKET"]) if (process.env[key]) env[key] = process.env[key]!;
39 const t0 = performance.now();
40 const child = Bun.spawn([process.execPath, main], { cwd: dir, env, stdin: "ignore", stdout: "pipe", stderr: "pipe" });
44 const child = Bun.spawn([process.execPath, "--preload", COUNTER, main], { cwd: dir, env, stdin: "ignore", stdout: "pipe", stderr: "pipe" });
4145 const out = { text: "" };
4246 const reading = collectOutput(out, child.stdout, child.stderr);
4347 try {
4448 let status = 0;
45 while (status !== 200 && child.exitCode === null && performance.now() - t0 < 60_000) {
46 status = await fetch(`http://127.0.0.1:${port}/readyz`).then((r) => r.status, () => 0);
47 if (status !== 200) await waitFor(() => false, { every: 25, within: 25 });
48 }
49 await waitFor(
50 async () => {
51 status = await fetch(`http://127.0.0.1:${port}/readyz`).then((r) => r.status, () => 0);
52 return status === 200 || child.exitCode !== null;
53 },
54 { every: 25, within: 120_000 },
55 );
4956 expect(status, out.text.slice(-1500)).toBe(200);
50 return performance.now() - t0;
57 child.kill("SIGUSR2");
58 expect(await waitFor(() => SAID.test(out.text) || child.exitCode !== null, { every: 10, within: 10_000 })).toBe(true);
59 const said = SAID.exec(out.text);
60 if (said === null) throw new Error(`${label} never said its count:\n${out.text.slice(-1500)}`);
61 return Number(said[1]);
5162 } finally {
5263 child.kill("SIGKILL");
5364 await child.exited;
@@ -55,9 +66,14 @@ async function boot(main: string, n: number): Promise<number> {
5566 }
5667 }
5768
58 test(`the bundle answers /readyz 200 within ${BUDGET_MS} ms x ${MARGIN} of spawn (fastest of ${TRIES})`, async () => {
69 test(`the bundle resolves under ${SHARE * 100}% of the imports the source boot does, on the way to /readyz`, async () => {
5970 const main = bundle(join(root, "out"));
60 const took: number[] = [];
61 for (let n = 0; n < TRIES && !(Math.min(...took) < BUDGET_MS * MARGIN); n++) took.push(await boot(main, n));
62 expect(Math.min(...took), `spawn to /readyz 200 took ${took.map(Math.round).join(", ")} ms`).toBeLessThan(BUDGET_MS * MARGIN);
63 }, 180_000);
71 const bundled = await boot(main, "bundle");
72 const source = await boot(SOURCE, "source");
73 const said = `imports resolved to ready: bundle ${bundled}; source ${source}; share ${(bundled / source).toFixed(4)}`;
74 // Every run says its numbers, so each check's log carries the machine's.
75 console.log(said);
76 // The count is live: a source boot resolves thousands.
77 expect(source, said).toBeGreaterThan(1_000);
78 expect(bundled / source, said).toBeLessThan(SHARE);
79 }, 240_000);
test/helpers/count-imports.ts added +18
@@ -0,0 +1,18 @@
1 /**
2 * A preload for a child forge (`--preload`): counts the imports it resolves at runtime, and says
3 * the count on SIGUSR2 as `imports resolved: <n>`. Counted work, the same on any machine
4 * (test/bundle-boot-time.test.ts).
5 */
6 const seen = new Set<string>();
7 Bun.plugin({
8 name: "count-imports",
9 setup(build) {
10 build.onResolve({ filter: /.*/ }, (args) => {
11 seen.add(`${args.importer}\0${args.path}`);
12 return undefined;
13 });
14 },
15 });
16 process.on("SIGUSR2", () => {

Nit: SIGUSR2 is the only way to read the count, and the test sends it right after /readyz 200. If a future boot path installs its own SIGUSR2 handler, the count would silently never print. The 10 s wait in the test fails loudly in that case, which is fine, but a short comment here naming the coupling would help.

17 process.stdout.write(`imports resolved: ${seen.size}\n`);
18 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs