Skip to content

Commit 2e87449

Browse files
committed
fixup! feat(tinybench): run walltime benchmarks in forks
1 parent a591f57 commit 2e87449

3 files changed

Lines changed: 9 additions & 42 deletions

File tree

‎packages/tinybench-plugin/src/walltime.ts‎

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ class WalltimeBenchRunner extends BaseBenchRunner {
103103
const { isolationMode } = this;
104104
if (isolationMode.kind === "Forks") {
105105
const samples = await runForks(uri, isolationMode.forks);
106-
setTaskResult(task, samples);
107106
return this.registerCodspeedBenchmark(task, samplesToResult(samples));
108107
}
109108

@@ -123,7 +122,6 @@ class WalltimeBenchRunner extends BaseBenchRunner {
123122
const { isolationMode } = this;
124123
if (isolationMode.kind === "Forks") {
125124
const samples = runForksSync(uri, isolationMode.forks);
126-
setTaskResult(task, samples);
127125
return this.registerCodspeedBenchmark(task, samplesToResult(samples));
128126
}
129127

@@ -153,9 +151,10 @@ class WalltimeBenchRunner extends BaseBenchRunner {
153151
}
154152
// A fork hands its samples to the parent, which reports the benchmark.
155153
if (this.isolationMode.kind === "Fork") {
156-
writeForkSamples(this.isolationMode.samplesFile, [
157-
...task.result.latency.samples,
158-
]);
154+
writeForkSamples(
155+
this.isolationMode.samplesFile,
156+
task.result.latency.samples,
157+
);
159158
return;
160159
}
161160
this.registerCodspeedBenchmark(task, task.result);
@@ -238,16 +237,3 @@ function convertTinybenchResultToBenchmarkStats(
238237
warmup_iters: warmupIterations,
239238
};
240239
}
241-
242-
/**
243-
* Fills `task.result` from the pooled fork samples so `bench.table()` and
244-
* `bench.results` work. Only tinybench 4/5 expose a way in; v6 keeps the
245-
* result `#private`.
246-
*/
247-
function setTaskResult(task: Task, samples: number[]): void {
248-
const { processRunResult } = task as unknown as {
249-
processRunResult?: (run: { latencySamples: number[] }) => void;
250-
};
251-
// tinybench sorts the samples in place.
252-
processRunResult?.call(task, { latencySamples: [...samples] });
253-
}

‎packages/tinybench-plugin/tests/fixtures/isolation.mjs‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Run by `isolation.integ.test.ts`. Prints the PID of every process that runs a
2-
// benchmark, and the sample count of every `task.result` the parent ends with.
2+
// benchmark.
33
import { Bench } from "tinybench";
44
import { withCodSpeed } from "../../dist/index.es5.js";
55

@@ -24,9 +24,3 @@ if (process.env.ISOLATION_FIXTURE_SYNC !== undefined) {
2424
} else {
2525
await bench.run();
2626
}
27-
28-
if (process.env.CODSPEED_ISOLATED_BENCHMARK === undefined) {
29-
for (const task of bench.tasks) {
30-
console.log(`result ${task.name} ${task.result?.samples.length ?? 0}`);
31-
}
32-
}

‎packages/tinybench-plugin/tests/isolation.integ.test.ts‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const MIN_ROUNDS_PER_FORK = 4;
1919
/** Fails a run whose benchmark process got stuck instead of blocking the suite. */
2020
const RUN_TIMEOUT_MS = 60_000;
2121
const RAN_LINE = /^ran (\d+) (.+)$/;
22-
const RESULT_LINE = /^result (.+) (\d+)$/;
2322

2423
interface FixtureRun {
2524
status: number | null;
@@ -29,8 +28,6 @@ interface FixtureRun {
2928
parentPid: number;
3029
/** Rounds of each benchmark in the results file. */
3130
roundsByName: Map<string, number>;
32-
/** Samples in the parent's `task.result` of each benchmark. */
33-
parentSamplesByName: Map<string, number>;
3431
}
3532

3633
interface FixtureOptions {
@@ -83,19 +80,12 @@ async function runFixture({
8380
const [status] = (await once(child, "close")) as [number | null];
8481

8582
const pidsByName = new Map<string, Set<number>>();
86-
const parentSamplesByName = new Map<string, number>();
8783
for (const line of stdout.split("\n")) {
8884
const ran = RAN_LINE.exec(line);
89-
if (ran) {
90-
const pids = pidsByName.get(ran[2]) ?? new Set<number>();
91-
pids.add(Number(ran[1]));
92-
pidsByName.set(ran[2], pids);
93-
continue;
94-
}
95-
const result = RESULT_LINE.exec(line);
96-
if (result) {
97-
parentSamplesByName.set(result[1], Number(result[2]));
98-
}
85+
if (!ran) continue;
86+
const pids = pidsByName.get(ran[2]) ?? new Set<number>();
87+
pids.add(Number(ran[1]));
88+
pidsByName.set(ran[2], pids);
9989
}
10090

10191
const roundsByName = new Map<string, number>();
@@ -111,7 +101,6 @@ async function runFixture({
111101
pidsByName,
112102
parentPid: child.pid!,
113103
roundsByName,
114-
parentSamplesByName,
115104
};
116105
} finally {
117106
rmSync(profileFolder, { recursive: true, force: true });
@@ -141,8 +130,6 @@ describe("walltime forks", () => {
141130
expect(rounds).toBeGreaterThanOrEqual(
142131
DEFAULT_FORKS * MIN_ROUNDS_PER_FORK,
143132
);
144-
// The parent reports the pooled rounds as its own `task.result`.
145-
expect(run.parentSamplesByName.get(name)).toBe(rounds);
146133
}
147134
// Forks of different benchmarks are different processes too.
148135
const all = new Set(

0 commit comments

Comments
 (0)