diff --git a/packages/ui/bench/README.md b/packages/ui/bench/README.md index 24b2ac56..1cdb6a5f 100644 --- a/packages/ui/bench/README.md +++ b/packages/ui/bench/README.md @@ -11,7 +11,8 @@ walltime for elapsed time, including async frame output, then runs the benchmark again with memory tracking to report allocations and peak memory. `createUI()` and fixture construction are outside the timed -sample. The initial-render comparison measures starting `ui.main()` through the first +sample, and each `beforeEach` forces a garbage collection afterwards so setup +garbage is not collected during a timed sample. The initial-render comparison measures starting `ui.main()` through the first frame's output callback, after the terminal setup write. Each update workload compares six tree shapes at 100, 500, 1,000, and 1,500 component elements: diff --git a/packages/ui/bench/helpers.ts b/packages/ui/bench/helpers.ts index 8980e46e..3ccd3968 100644 --- a/packages/ui/bench/helpers.ts +++ b/packages/ui/bench/helpers.ts @@ -12,6 +12,14 @@ export async function waitFor(predicate: () => boolean): Promise { } } +/** + * Collect fixture setup garbage before a timed sample so its GC pause is not + * charged to the sample. Requires `--expose-gc`, which the bench project sets. + */ +export function collectGarbage(): void { + globalThis.gc?.(); +} + // see the readme for explanation of tree shapes export const shapes = ['grass', 'bamboo', 'bush', 'evergreen', 'maple', 'sequoia'] as const; export type TreeShape = (typeof shapes)[number]; diff --git a/packages/ui/bench/render.bench.ts b/packages/ui/bench/render.bench.ts index 38594baf..1239082e 100644 --- a/packages/ui/bench/render.bench.ts +++ b/packages/ui/bench/render.bench.ts @@ -1,6 +1,7 @@ import { test } from 'vitest'; import { applyWorkload, + collectGarbage, setupUI, shapes, waitFor, @@ -29,6 +30,7 @@ test('initial render', { timeout: 300_000 }, async ({ bench }) => { { async beforeEach() { fixture = await setupUI(benchmarkCase.nodeCount, benchmarkCase.shape); + collectGarbage(); }, async afterEach() { await fixture.close(); @@ -52,6 +54,7 @@ test.for(workloads)('%s', { timeout: 300_000 }, async (workload, { bench }) => { fixture = await setupUI(benchmarkCase.nodeCount, benchmarkCase.shape); await fixture.start(); iteration = 0; + collectGarbage(); }, async afterEach() { await fixture.close(); diff --git a/vitest.config.mts b/vitest.config.mts index 57e75405..b2736663 100644 --- a/vitest.config.mts +++ b/vitest.config.mts @@ -16,6 +16,8 @@ export default defineConfig({ name: 'bench', benchmark: { enabled: true, include: ['**/*.bench.ts'] }, experimental: { viteModuleRunner: false }, + // lets beforeEach hooks collect setup garbage outside timed samples + execArgv: ['--expose-gc'], }, }, ],