Public: anyone can read this; the crew can change it.
The bundle's boot is held to a share of the source boot's CPU, measured in the same run, not a wall-clock bound #17320
Merged 37h 22m ago, live in production
fix/bundle-boot-cpu → main 0 commits 1 thread · 1 unresolved
3 files
changed +51
−26
docs/features/bundle.md +1 −1
| @@ -8,4 +8,4 @@ row: Boot the forge from a bundle | ||
| 8 | 8 | |
| 9 | 9 | 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. |
| 10 | 10 | |
| 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 in the same run and holds the bundle to a share of the source boot's CPU to ready, so a busy machine slows neither side against the other; its header has the measured shares 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[] = [ | ||
| 55 | 55 | { 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 }, |
| 56 | 56 | { 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 }, |
| 57 | 57 | { 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 three times each on an empty directory, for their CPU to /readyz; 6 s a test alone", issue: 17019 }, | |
| 59 | 59 | { 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 }, |
| 60 | 60 | { 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 }, |
| 61 | 61 | { 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 +49 −24
| @@ -1,12 +1,15 @@ | ||
| 1 | 1 | /** |
| 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 on an empty data | |
| 3 | * directory (#17019): source `bun src/main.ts` spends its boot loading modules, and the bundle | |
| 4 | * does not. Measured as CPU from spawn to ready, the bundle's against the source boot's in the same | |
| 5 | * run: a busy runner's waits count against neither, so it fails only if the bundle starts paying | |
| 6 | * what the source does. It was a wall-clock bound (4.5 s), red on loaded runners (2026-12-01: | |
| 7 | * 4804, 5372, 5158 ms on #17277). | |
| 5 | 8 | * |
| 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 | * SHARE: measured on the Mac, the bundle took 0.37 and 0.39 of the source boot's CPU (645-870 ms | |
| 10 | * against 1721-1985 ms); the bound is 0.6, half again over that. With a source module tree | |
| 11 | * imported at the bundle's top for one run (src/http/server.ts), the share was 0.94: red. The | |
| 12 | * numbers are in the failure message. | |
| 10 | 13 | * |
| 11 | 14 | * @tree-scan: boots all of src/ through a bundle of main.ts. |
| 12 | 15 | */ |
| @@ -20,44 +23,66 @@ import { collectOutput } from "./helpers/collect-output.ts"; | ||
| 20 | 23 | import { waitFor } from "./helpers/poll.ts"; |
| 21 | 24 | import { productionEnv } from "./helpers/production-env.ts"; |
| 22 | 25 | |
| 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 | /** | |
| 27 | * The most of the source boot's CPU to ready the bundle may spend, measured in the same run (the | |
| 28 | * file's note says how it was chosen). | |
| 29 | */ | |
| 30 | export const SHARE = 0.6; | |
| 31 | const TRIES = 3; // each side's fewest CPU of a few: one boot can catch a collection the other did not | |
| 26 | 32 | |
| 27 | 33 | const root = mkdtempSync(join(tmpdir(), "forge-bundle-time-")); |
| 28 | 34 | afterAll(() => rmSync(root, { recursive: true, force: true })); |
| 35 | const SOURCE = join(import.meta.dir, "..", "src", "main.ts"); | |
| 29 | 36 | |
| 30 | /** One boot: ms from spawn to `/readyz` 200, on a fresh empty data directory. */ | |
| 37 | /** One boot of `main`: the CPU (user + system, ms) it spent from spawn to `/readyz` 200. */ | |
| 31 | 38 | async function boot(main: string, n: number): Promise<number> { |
| 32 | const dir = join(root, `run${n}`); | |
| 39 | const dir = join(root, `run${n}-${main === SOURCE ? "source" : "bundle"}`); | |
| 33 | 40 | mkdirSync(join(dir, "data"), { recursive: true }); |
| 34 | 41 | const caFile = join(dir, "ca.crt"); |
| 35 | 42 | writeFileSync(caFile, "-----BEGIN CERTIFICATE-----\nMIIBfake\n-----END CERTIFICATE-----\n"); |
| 36 | 43 | const port = await freePort(); |
| 37 | 44 | const env: Record<string, string> = { PATH: process.env["PATH"] ?? "", ...productionEnv({ dataDir: join(dir, "data"), port, sshPort: await freePort(), caFile }) }; |
| 38 | 45 | 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 | 46 | const child = Bun.spawn([process.execPath, main], { cwd: dir, env, stdin: "ignore", stdout: "pipe", stderr: "pipe" }); |
| 41 | 47 | const out = { text: "" }; |
| 42 | 48 | const reading = collectOutput(out, child.stdout, child.stderr); |
| 49 | let status = 0; | |
| 43 | 50 | try { |
| 44 | 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 | } | |
| 51 | // Asked until it answers or exits: the readiness is the event; no clock bounds it here. | |
| 52 | await waitFor( | |
| 53 | async () => { | |
| 54 | status = await fetch(`http://127.0.0.1:${port}/readyz`).then((r) => r.status, () => 0); | |
| 55 | return status === 200 || child.exitCode !== null; | |
| 56 | }, | |
| 57 | { every: 25, within: 120_000 }, | |
| 58 | ); | |
| 49 | 59 | expect(status, out.text.slice(-1500)).toBe(200); |
| 50 | return performance.now() - t0; | |
| 51 | 60 | } finally { |
| 52 | 61 | child.kill("SIGKILL"); |
| 53 | 62 | await child.exited; |
| 54 | 63 | await reading; |
| 55 | 64 | } |
| 65 | // CPU, not wall: a busy runner's queueing for a core counts against neither side. | |
| 66 | const used = child.resourceUsage(); | |
| 67 | if (used === undefined) throw new Error("no resource usage for the boot"); | |
| 68 | return Number(used.cpuTime.total) / 1000; | |
| 56 | 69 | } |
| 57 | 70 | |
| 58 | test(`the bundle answers /readyz 200 within ${BUDGET_MS} ms x ${MARGIN} of spawn (fastest of ${TRIES})`, async () => { | |
| 71 | // The bundle is the cheap path (#17019): source `bun src/main.ts` spends its boot loading modules, | |
| 72 | // and the bundle does not. Both are booted here, on the same machine in the same run, and each | |
| 73 | // side's fewest CPU to ready is compared, so it fails if the bundle starts paying what the source | |
| 74 | // does, and not because the runner is busy. | |
| 75 | test(`the bundle reaches /readyz on under ${SHARE * 100}% of the source boot's CPU (fewest of ${TRIES} each)`, async () => { | |
| 59 | 76 | 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); | |
| 77 | const bundled: number[] = []; | |
| 78 | const source: number[] = []; | |
| 79 | for (let n = 0; n < TRIES; n++) { | |
| 80 | bundled.push(await boot(main, n)); | |
| 81 | source.push(await boot(SOURCE, n)); | |
| 82 | } | |
| 83 | const share = Math.min(...bundled) / Math.min(...source); | |
| 84 | const said = `CPU to ready: bundle ${bundled.map(Math.round).join(", ")} ms; source ${source.map(Math.round).join(", ")} ms; share ${share.toFixed(2)}`; | |
| 85 | // Every run says its share, so each check's log carries the number its machine measured. | |
| 86 | console.log(said); | |
| 87 | expect(share, said).toBeLessThan(SHARE); | |
| 88 | }, 240_000); |
new above
offline
The numbers here are from a Mac. Add the Linux crew-box share, because that is where the check runs, and confirm 0.6 has real headroom there.