Skip to content

Commit 1d6e63f

Browse files
committed
fix(benchmark.js-plugin): call complete listeners in isolated children
A child skipped the complete listeners of runs that don't own its benchmark, so a benchmark started from such a listener was never measured.
1 parent 35e1d71 commit 1d6e63f

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

‎packages/benchmark.js-plugin/src/index.ts‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,33 +171,34 @@ async function runBenchmarks({
171171
}: RunBenchmarksOptions): Promise<void> {
172172
const isolation = Isolation.current();
173173
const isChild = isolation.isChild();
174-
const selected = benches
175-
.map((bench, i) => ({
176-
bench,
177-
uri: bench.uri ?? `${baseUri}::unknown_${i}`,
178-
}))
179-
.filter(({ uri }) => isolation.shouldRun(uri));
174+
const all = benches.map((bench, i) => ({
175+
bench,
176+
uri: bench.uri ?? `${baseUri}::unknown_${i}`,
177+
}));
180178
// A child only measures one benchmark. A run call that owns none of them
181179
// must not touch the instruments, the call that owns it does.
182-
if (isChild && selected.length === 0) {
183-
return;
184-
}
180+
const measures = all.some(({ uri }) => isolation.shouldRun(uri));
185181

186182
// The parent already printed the banner and the summary, children stay quiet.
187183
if (!isChild) {
188184
console.log(
189185
`[CodSpeed] running with @codspeed/benchmark.js v${__VERSION__}`,
190186
);
191187
}
192-
setupCore();
193-
for (const { bench, uri } of selected) {
194-
await isolation.run(uri, () => measureBenchmark(bench, uri));
195-
// The parent reports for every benchmark, the child ran only one of them.
196-
if (!isChild) {
197-
benchmarkCompletedListeners.forEach((listener) => listener());
188+
if (measures) {
189+
setupCore();
190+
}
191+
for (const { bench, uri } of all) {
192+
if (isolation.shouldRun(uri)) {
193+
await isolation.run(uri, () => measureBenchmark(bench, uri));
198194
}
195+
// Also in children, for benchmarks they skip: a `complete` listener may
196+
// start the run that owns the child's benchmark.
197+
benchmarkCompletedListeners.forEach((listener) => listener());
198+
}
199+
if (measures) {
200+
teardownCore();
199201
}
200-
teardownCore();
201202
if (!isChild) {
202203
console.log(`[CodSpeed] Done running ${benches.length} benches.`);
203204
}

‎packages/benchmark.js-plugin/tests/fixtures/isolation.cjs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,18 @@ if (process.env.ISOLATION_FIXTURE_FAIL !== undefined) {
3333
});
3434
}
3535

36+
// Started from a `complete` listener of the first suite: a child measuring
37+
// `chained` only reaches it through that listener.
38+
const chained = withCodSpeed(new Benchmark.Suite("chained"));
39+
chained.add("chained", () => {});
40+
let completed = 0;
41+
suite.on("complete", () => {
42+
completed++;
43+
if (completed === suite.length) {
44+
// After the first run tears down, so the two runs don't overlap.
45+
setImmediate(() => chained.run());
46+
}
47+
});
48+
3649
// An unhandled rejection ends the process with exit code 1.
3750
suite.run();

‎packages/benchmark.js-plugin/tests/isolation.integ.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { join } from "path";
44

55
const FIXTURE = join(__dirname, "fixtures", "isolation.cjs");
66
const PROCESS_ISOLATION_ENV = "CODSPEED_PROCESS_ISOLATION";
7-
/** `first`, `second`, `third` and the deferred `async` benchmark. */
8-
const BENCHMARK_COUNT = 4;
7+
/** `first`, `second`, `third`, the deferred `async`, and `chained`. */
8+
const BENCHMARK_COUNT = 5;
99
/** Fails a run whose benchmark process got stuck instead of blocking the suite. */
1010
const RUN_TIMEOUT_MS = 60_000;
1111
const EXECUTED_LINE = /^executed (\d+) (.+)$/;

0 commit comments

Comments
 (0)