Skip to content

Commit efb0428

Browse files
committed
fixup! feat(vitest): run walltime benchmarks in forks
1 parent 26ca4f7 commit efb0428

5 files changed

Lines changed: 32 additions & 8 deletions

File tree

‎packages/vitest-plugin/src/__tests__/isolation.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ describe("walltime forks", () => {
9999
const run = await runFixture();
100100

101101
expect(run.status).toBe(0);
102-
for (const name of ["first", "second"]) {
102+
for (const name of ["first", "second", "third"]) {
103103
expect(run.pidsByName.get(name)?.size).toBe(DEFAULT_FORKS);
104104
expect(run.roundsByName.get(name)).toBeGreaterThanOrEqual(
105105
DEFAULT_FORKS * MIN_ROUNDS_PER_FORK,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ import { Plugin } from "vite";
1212
import { type ViteUserConfig } from "vitest/config";
1313
import { resolveVitestBackend } from "./vitestBackend";
1414

15+
declare module "vitest" {
16+
interface BenchFnOptions {
17+
/** This benchmark's `forks`, over the plugin's; see {@link WalltimeOptions}. */
18+
forks?: WalltimeOptions["forks"];
19+
}
20+
}
21+
1522
// get this file's directory path from import.meta.url
1623
const __dirname = new URL(".", import.meta.url).pathname;
1724
const isFileInTs = import.meta.url.endsWith(".ts");

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,20 @@ const FORKED_BENCHMARKS_META = "codspeedForkedBenchmarks";
6868

6969
type ForkMeta = TaskMeta & { [FORKED_BENCHMARKS_META]?: ForkedBenchmark[] };
7070

71-
export function setForkedBenchmarks(
71+
/**
72+
* Adds benchmarks to the test's queue: a test can run several groups
73+
* (`bench.compare()`, `registration.run()`), and a retried or repeated test
74+
* queues the same ones again, which replace the earlier entries.
75+
*/
76+
export function queueForkedBenchmarks(
7277
meta: TaskMeta,
7378
benchmarks: ForkedBenchmark[],
7479
): void {
75-
(meta as ForkMeta)[FORKED_BENCHMARKS_META] = benchmarks;
80+
const uris = new Set(benchmarks.map(({ uri }) => uri));
81+
(meta as ForkMeta)[FORKED_BENCHMARKS_META] = [
82+
...forkedBenchmarks(meta).filter(({ uri }) => !uris.has(uri)),
83+
...benchmarks,
84+
];
7685
}
7786

7887
export function forkedBenchmarks(meta: TaskMeta): ForkedBenchmark[] {

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { createRequire } from "module";
1717
import path from "path";
1818
import type {
19+
BenchFnOptions,
1920
BenchmarkGroup,
2021
BenchmarkProvider,
2122
BenchRegistrationInput,
@@ -24,7 +25,7 @@ import type {
2425
} from "vitest";
2526
import {
2627
installInstrumentHooks,
27-
setForkedBenchmarks,
28+
queueForkedBenchmarks,
2829
tinybenchTaskToBenchmark,
2930
writeAndLogWalltimeResults,
3031
type TinybenchBench,
@@ -257,8 +258,10 @@ async function runWalltime(
257258

258259
throwOnErroredTasks(tasks);
259260
if (mode.kind === "Fork") {
261+
// A task that didn't complete fails the test below, like without forks.
260262
for (const task of tasks) {
261-
writeForkSamples(mode.samplesFile, task.result?.latency.samples ?? []);
263+
if (task.result?.state !== "completed") continue;
264+
writeForkSamples(mode.samplesFile, task.result.latency.samples ?? []);
262265
}
263266
} else {
264267
collectWalltimeResults(bench, getUri);
@@ -285,13 +288,13 @@ async function queueForks(
285288
const { Bench } = await importHostTinybench();
286289
// Never run: it resolves tinybench's defaults for the reported config.
287290
const bench = new Bench(options);
288-
setForkedBenchmarks(
291+
queueForkedBenchmarks(
289292
test.meta,
290293
registrations.map(({ name, fnOpts }) => ({
291294
uri: getUri(name),
292295
name,
293296
forks:
294-
(fnOpts as WalltimeOptions | undefined)?.forks ??
297+
(fnOpts as BenchFnOptions | undefined)?.forks ??
295298
(options as WalltimeOptions | undefined)?.forks,
296299
options: resolvedOptions(bench),
297300
})),

‎packages/vitest-plugin/tests/fixtures/isolation/pids.bench.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function logPid(name: string) {
2121
}
2222

2323
const firstForks = process.env.ISOLATION_FIXTURE_FIRST_FORKS;
24+
const RUN_OPTIONS = { iterations: 4, time: 1 };
2425

2526
describe("isolation", () => {
2627
test("benchmarks", async ({ bench }) => {
@@ -42,6 +43,10 @@ describe("isolation", () => {
4243
}),
4344
);
4445
}
45-
await bench.compare(...benchmarks, { iterations: 4, time: 1 });
46+
await bench.compare(...benchmarks, RUN_OPTIONS);
47+
// A second group in the same test: its benchmark is forked too.
48+
await bench("third", { beforeAll: logPid("third") }, () => {}).run(
49+
RUN_OPTIONS,
50+
);
4651
});
4752
});

0 commit comments

Comments
 (0)