Skip to content

Commit b147415

Browse files
committed
feat(vitest): run each benchmark in its own process
Both the Vitest 5 provider and the legacy analysis runner measure through `Isolation.run`. An isolated child still reports every benchmark to Vitest, but only measures its own. Vitest measures in forked workers, and the argv of a worker is not the command that starts the benchmarks. The plugin records the command of the main Vitest process in `CODSPEED_ISOLATION_COMMAND` from its `config()` hook, before the workers are forked, and each worker re-runs that command once per benchmark. This means one full Vitest run per benchmark.
1 parent cf218bd commit b147415

3 files changed

Lines changed: 84 additions & 36 deletions

File tree

‎packages/vitest-plugin/src/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
getV8Flags,
44
InstrumentHooks,
55
mongoMeasurement,
6+
recordIsolationCommand,
67
SetupInstrumentsRequestBody,
78
SetupInstrumentsResponse,
89
} from "@codspeed/core";
@@ -38,6 +39,10 @@ export default function codspeedPlugin(): Plugin {
3839
},
3940
enforce: "post",
4041
config(userConfig): ViteUserConfig {
42+
// Vitest measures in forked workers, whose argv is not the command that
43+
// starts the benchmarks. Record the command of this main process so the
44+
// workers can run it again for each benchmark. Workers inherit the env.
45+
recordIsolationCommand();
4146
return {
4247
test: {
4348
pool: "forks",

‎packages/vitest-plugin/src/legacy/analysis.ts‎

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
InstrumentHooks,
3+
Isolation,
34
logDebug,
45
mongoMeasurement,
56
optimizeFunction,
@@ -41,44 +42,50 @@ async function runAnalysisBench(
4142
suite: RunnerTestSuite,
4243
currentSuiteName: string,
4344
tinybenchModule: Tinybench,
45+
isolation: Isolation,
4446
) {
4547
const uri = `${currentSuiteName}::${benchmark.name}`;
46-
const fn = getBenchFn(benchmark);
47-
48-
// Constructing a Bench applies tinybench's no-op defaults for the setup and
49-
// teardown hooks and gives them the Task they expect. The bench itself is
50-
// never run: this runner drives the benchmark function directly.
51-
const bench = new tinybenchModule.Bench(getBenchOptions(benchmark));
52-
const task = new tinybenchModule.Task(bench, benchmark.name, fn);
53-
54-
await bench.setup(task, "warmup");
55-
await optimizeFunction(async () => {
48+
if (!isolation.shouldRun(uri)) return;
49+
50+
await isolation.run(uri, async () => {
51+
const fn = getBenchFn(benchmark);
52+
53+
// Constructing a Bench applies tinybench's no-op defaults for the setup and
54+
// teardown hooks and gives them the Task they expect. The bench itself is
55+
// never run: this runner drives the benchmark function directly.
56+
const bench = new tinybenchModule.Bench(getBenchOptions(benchmark));
57+
const task = new tinybenchModule.Task(bench, benchmark.name, fn);
58+
59+
await bench.setup(task, "warmup");
60+
await optimizeFunction(async () => {
61+
await callSuiteHook(suite, benchmark, "beforeEach");
62+
await fn();
63+
await callSuiteHook(suite, benchmark, "afterEach");
64+
});
65+
await bench.teardown(task, "warmup");
66+
67+
await bench.setup(task, "run");
5668
await callSuiteHook(suite, benchmark, "beforeEach");
57-
await fn();
69+
await mongoMeasurement.start(uri);
70+
global.gc?.();
71+
await wrapWithRootFrame(async () => {
72+
InstrumentHooks.startBenchmark();
73+
await fn();
74+
InstrumentHooks.stopBenchmark();
75+
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
76+
})();
77+
await mongoMeasurement.stop(uri);
5878
await callSuiteHook(suite, benchmark, "afterEach");
79+
await bench.teardown(task, "run");
80+
81+
logCodSpeed(`${uri} done`);
5982
});
60-
await bench.teardown(task, "warmup");
61-
62-
await bench.setup(task, "run");
63-
await callSuiteHook(suite, benchmark, "beforeEach");
64-
await mongoMeasurement.start(uri);
65-
global.gc?.();
66-
await wrapWithRootFrame(async () => {
67-
InstrumentHooks.startBenchmark();
68-
await fn();
69-
InstrumentHooks.stopBenchmark();
70-
InstrumentHooks.setExecutedBenchmark(process.pid, uri);
71-
})();
72-
await mongoMeasurement.stop(uri);
73-
await callSuiteHook(suite, benchmark, "afterEach");
74-
await bench.teardown(task, "run");
75-
76-
logCodSpeed(`${uri} done`);
7783
}
7884

7985
async function runAnalysisBenchmarkSuite(
8086
suite: RunnerTestSuite,
8187
tinybenchModule: Tinybench,
88+
isolation: Isolation,
8289
parentSuiteName?: string,
8390
) {
8491
const currentSuiteName = parentSuiteName
@@ -91,9 +98,20 @@ async function runAnalysisBenchmarkSuite(
9198
if (task.mode !== "run") continue;
9299

93100
if (task.type === "suite") {
94-
await runAnalysisBenchmarkSuite(task, tinybenchModule, currentSuiteName);
101+
await runAnalysisBenchmarkSuite(
102+
task,
103+
tinybenchModule,
104+
isolation,
105+
currentSuiteName,
106+
);
95107
} else if (isVitestTaskBenchmark(task)) {
96-
await runAnalysisBench(task, suite, currentSuiteName, tinybenchModule);
108+
await runAnalysisBench(
109+
task,
110+
suite,
111+
currentSuiteName,
112+
tinybenchModule,
113+
isolation,
114+
);
97115
}
98116
}
99117

@@ -107,9 +125,24 @@ export class AnalysisRunner extends NodeBenchmarkRunner {
107125

108126
patchRootSuiteWithFullFilePath(suite);
109127

110-
logCodSpeed(`running suite ${suite.name}`);
111-
await runAnalysisBenchmarkSuite(suite, await this.importTinybench());
112-
logCodSpeed(`running suite ${suite.name} done`);
128+
// Without instrumentation nothing is measured, so there is nothing to isolate.
129+
const isolation = InstrumentHooks.isInstrumented()
130+
? Isolation.current()
131+
: Isolation.shared();
132+
// The parent already printed the suite lines, children stay quiet.
133+
const isChild = isolation.isChild();
134+
135+
if (!isChild) {
136+
logCodSpeed(`running suite ${suite.name}`);
137+
}
138+
await runAnalysisBenchmarkSuite(
139+
suite,
140+
await this.importTinybench(),
141+
isolation,
142+
);
143+
if (!isChild) {
144+
logCodSpeed(`running suite ${suite.name} done`);
145+
}
113146

114147
teardownCore();
115148
}

‎packages/vitest-plugin/src/v5/provider.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
getGitDir,
33
getInstrumentMode,
44
InstrumentHooks,
5+
Isolation,
56
optimizeFunction,
67
optimizeFunctionSync,
78
setupCore,
@@ -155,12 +156,21 @@ async function runAnalysis(
155156
registrations: BenchRegistrationInput[],
156157
getUri: (name: string) => string,
157158
): Promise<BenchResult[]> {
158-
const label = InstrumentHooks.isInstrumented() ? "Measured" : "Checked";
159+
const instrumented = InstrumentHooks.isInstrumented();
160+
const label = instrumented ? "Measured" : "Checked";
161+
// Without instrumentation nothing is measured, so there is nothing to isolate.
162+
const isolation = instrumented ? Isolation.current() : Isolation.shared();
159163
const results: BenchResult[] = [];
160164
for (const { name, fn, fnOpts } of registrations) {
161165
const uri = getUri(name);
162-
await runAnalysisTask(fn, fnOpts, uri);
163-
console.log(`[CodSpeed] ${label} ${uri}`);
166+
// An isolated child still reports every benchmark to Vitest, but it only
167+
// measures its own.
168+
if (isolation.shouldRun(uri)) {
169+
await isolation.run(uri, async () => {
170+
await runAnalysisTask(fn, fnOpts, uri);
171+
console.log(`[CodSpeed] ${label} ${uri}`);
172+
});
173+
}
164174
results.push(analysisResult(name));
165175
}
166176
return results;

0 commit comments

Comments
 (0)