Skip to content

Commit 4cd835b

Browse files
committed
fix: make sure teardown is called only once after all the benchmarks ran
1 parent db2ed73 commit 4cd835b

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ async function runBenchmarks({
200200
}
201201
console.log(` ✔ Measured ${uri}`);
202202
benchmarkCompletedListeners.forEach((listener) => listener());
203-
teardownCore();
204203
}
204+
teardownCore();
205205
console.log(`[CodSpeed] Done running ${benches.length} benches.`);
206206
}

packages/benchmark.js-plugin/tests/index.integ.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,18 @@ describe("Benchmark.Suite", () => {
236236
"packages/benchmark.js-plugin/tests/registerOtherBenchmarks.ts::thesuite::RegExp"
237237
);
238238
});
239+
it("should call setupCore and teardownCore only once after run()", async () => {
240+
mockCore.Measurement.isInstrumented.mockReturnValue(true);
241+
const suite = withCodSpeed(new Benchmark.Suite("thesuite"));
242+
registerBenchmarks(suite);
243+
registerOtherBenchmarks(suite);
244+
245+
expect(mockCore.setupCore).not.toHaveBeenCalled();
246+
expect(mockCore.teardownCore).not.toHaveBeenCalled();
247+
248+
await suite.run({ maxTime: 0.1, initCount: 1 });
249+
250+
expect(mockCore.setupCore).toHaveBeenCalledTimes(1);
251+
expect(mockCore.teardownCore).toHaveBeenCalledTimes(1);
252+
});
239253
});

packages/tinybench-plugin/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ export function withCodSpeed(bench: Bench): Bench {
4444
const rootCallingFile = getCallingFile();
4545

4646
bench.run = async () => {
47-
setupCore();
4847
console.log(`[CodSpeed] running with @codspeed/tinybench v${__VERSION__}`);
48+
setupCore();
4949
for (const task of bench.tasks) {
5050
// run before hooks
5151
if (task.opts.beforeAll != null) {
@@ -77,10 +77,10 @@ export function withCodSpeed(bench: Bench): Bench {
7777
// print results
7878
console.log(` ✔ Measured ${uri}`);
7979
}
80+
teardownCore();
8081
console.log(`[CodSpeed] Done running ${bench.tasks.length} benches.`);
8182
return bench.tasks;
8283
};
83-
teardownCore();
8484
return bench;
8585
}
8686

packages/tinybench-plugin/tests/index.integ.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,23 @@ describe("Benchmark.Suite", () => {
158158
expect(afterEach).toHaveBeenCalledTimes(2);
159159
expect(afterAll).toHaveBeenCalledTimes(2);
160160
});
161+
162+
it("should call setupCore and teardownCore only once after run()", async () => {
163+
mockCore.Measurement.isInstrumented.mockReturnValue(true);
164+
const bench = withCodSpeed(new Bench())
165+
.add("RegExp", function () {
166+
/o/.test("Hello World!");
167+
})
168+
.add("RegExp2", () => {
169+
/o/.test("Hello World!");
170+
});
171+
172+
expect(mockCore.setupCore).not.toHaveBeenCalled();
173+
expect(mockCore.teardownCore).not.toHaveBeenCalled();
174+
175+
await bench.run();
176+
177+
expect(mockCore.setupCore).toHaveBeenCalledTimes(1);
178+
expect(mockCore.teardownCore).toHaveBeenCalledTimes(1);
179+
});
161180
});

0 commit comments

Comments
 (0)