@@ -6,10 +6,14 @@ import {
66 setupCore ,
77 teardownCore ,
88} from "@codspeed/core" ;
9- import { Benchmark , chai , Suite } from "vitest" ;
9+ import { Benchmark , type RunnerTestSuite } from "vitest" ;
1010import { NodeBenchmarkRunner } from "vitest/runners" ;
1111import { getBenchFn } from "vitest/suite" ;
12- import { callSuiteHook , patchRootSuiteWithFullFilePath } from "./common" ;
12+ import {
13+ callSuiteHook ,
14+ isVitestTaskBenchmark ,
15+ patchRootSuiteWithFullFilePath ,
16+ } from "./common" ;
1317
1418const currentFileName =
1519 typeof __filename === "string"
@@ -27,25 +31,20 @@ function logCodSpeed(message: string) {
2731
2832async function runInstrumentedBench (
2933 benchmark : Benchmark ,
34+ suite : RunnerTestSuite ,
3035 currentSuiteName : string
3136) {
3237 const uri = `${ currentSuiteName } ::${ benchmark . name } ` ;
3338 const fn = getBenchFn ( benchmark ) ;
3439
35- await callSuiteHook ( benchmark . suite , benchmark , "beforeEach" ) ;
36- try {
37- await optimizeFunction ( fn ) ;
38- } catch ( e ) {
39- // if the error is not an assertion error, we want to fail the run
40- // we allow assertion errors because we want to be able to use `expect` in the benchmark to allow for better authoring
41- // assertions are allowed to fail in the optimization phase since it might be linked to stateful code
42- if ( ! ( e instanceof chai . AssertionError ) ) {
43- throw e ;
44- }
45- }
46- await callSuiteHook ( benchmark . suite , benchmark , "afterEach" ) ;
40+ await optimizeFunction ( async ( ) => {
41+ await callSuiteHook ( suite , benchmark , "beforeEach" ) ;
42+ // @ts -expect-error we do not need to bind the function to an instance of tinybench's Bench
43+ await fn ( ) ;
44+ await callSuiteHook ( suite , benchmark , "afterEach" ) ;
45+ } ) ;
4746
48- await callSuiteHook ( benchmark . suite , benchmark , "beforeEach" ) ;
47+ await callSuiteHook ( suite , benchmark , "beforeEach" ) ;
4948 await mongoMeasurement . start ( uri ) ;
5049 global . gc ?.( ) ;
5150 await ( async function __codspeed_root_frame__ ( ) {
@@ -55,44 +54,36 @@ async function runInstrumentedBench(
5554 Measurement . stopInstrumentation ( uri ) ;
5655 } ) ( ) ;
5756 await mongoMeasurement . stop ( uri ) ;
58- await callSuiteHook ( benchmark . suite , benchmark , "afterEach" ) ;
57+ await callSuiteHook ( suite , benchmark , "afterEach" ) ;
5958
6059 logCodSpeed ( `${ uri } done` ) ;
6160}
6261
6362async function runInstrumentedBenchmarkSuite (
64- suite : Suite ,
63+ suite : RunnerTestSuite ,
6564 parentSuiteName ?: string
6665) {
6766 const currentSuiteName = parentSuiteName
6867 ? parentSuiteName + "::" + suite . name
6968 : suite . name ;
7069
71- // do not call `beforeAll` if we are in the root suite, since it is already called by vitest
72- // see https://github.com/vitest-dev/vitest/blob/1fee63f2598edc228017f18eca325f85ee54aee0/packages/runner/src/run.ts#L293
73- if ( parentSuiteName !== undefined ) {
74- await callSuiteHook ( suite , suite , "beforeAll" ) ;
75- }
70+ await callSuiteHook ( suite , suite , "beforeAll" ) ;
7671
7772 for ( const task of suite . tasks ) {
7873 if ( task . mode !== "run" ) continue ;
7974
80- if ( task . meta ?. benchmark ) {
81- await runInstrumentedBench ( task as Benchmark , currentSuiteName ) ;
75+ if ( isVitestTaskBenchmark ( task ) ) {
76+ await runInstrumentedBench ( task , suite , currentSuiteName ) ;
8277 } else if ( task . type === "suite" ) {
8378 await runInstrumentedBenchmarkSuite ( task , currentSuiteName ) ;
8479 }
8580 }
8681
87- // do not call `afterAll` if we are in the root suite, since it is already called by vitest
88- // see https://github.com/vitest-dev/vitest/blob/1fee63f2598edc228017f18eca325f85ee54aee0/packages/runner/src/run.ts#L324
89- if ( parentSuiteName !== undefined ) {
90- await callSuiteHook ( suite , suite , "afterAll" ) ;
91- }
82+ await callSuiteHook ( suite , suite , "afterAll" ) ;
9283}
9384
9485export class InstrumentedRunner extends NodeBenchmarkRunner {
95- async runSuite ( suite : Suite ) : Promise < void > {
86+ async runSuite ( suite : RunnerTestSuite ) : Promise < void > {
9687 logDebug ( `PROCESS PID: ${ process . pid } in ${ currentFileName } ` ) ;
9788 setupCore ( ) ;
9889
0 commit comments