Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/benchmark.js-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ async function runBenchmarks({
benchPayload = bench.fn as CallableFunction;
}

if (typeof bench.options.setup === "function") {
await bench.options.setup();
}

if (isAsync) {
await optimizeFunction(benchPayload);
await (async function __codspeed_root_frame__() {
Expand All @@ -198,6 +202,11 @@ async function runBenchmarks({
Measurement.stopInstrumentation(uri);
})();
}

if (typeof bench.options.teardown === "function") {
await bench.options.teardown();
}

console.log(` ✔ Measured ${uri}`);
benchmarkCompletedListeners.forEach((listener) => listener());
}
Expand Down
34 changes: 34 additions & 0 deletions packages/benchmark.js-plugin/tests/index.integ.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ describe("Benchmark", () => {
}
}
);
it("should call setup and teardown", async () => {
mockCore.Measurement.isInstrumented.mockReturnValue(true);
const setup = jest.fn();
const teardown = jest.fn();
const bench = withCodSpeed(
new Benchmark(
"RegExpSingle",
function () {
/o/.test("Hello World!");
},
{ ...benchOptions, setup, teardown }
)
);
await bench.run();
expect(setup).toHaveBeenCalled();
expect(teardown).toHaveBeenCalled();
});
});

describe("Benchmark.Suite", () => {
Expand Down Expand Up @@ -250,4 +267,21 @@ describe("Benchmark.Suite", () => {
expect(mockCore.setupCore).toHaveBeenCalledTimes(1);
expect(mockCore.teardownCore).toHaveBeenCalledTimes(1);
});
it("should call setup and teardown", async () => {
mockCore.Measurement.isInstrumented.mockReturnValue(true);
const setup = jest.fn();
const teardown = jest.fn();

const suite = withCodSpeed(new Benchmark.Suite("thesuite")).add(
"RegExpSingle",
function () {
/o/.test("Hello World!");
},
{ ...benchOptions, setup, teardown }
);
await suite.run();

expect(setup).toHaveBeenCalled();
expect(teardown).toHaveBeenCalled();
});
});