|
| 1 | +import { Measurement, setupCore, teardownCore } from "@codspeed/core"; |
| 2 | +import { Benchmark, Suite } from "vitest"; |
| 3 | +import { NodeBenchmarkRunner } from "vitest/runners"; |
| 4 | +import { getBenchFn } from "vitest/suite"; |
| 5 | + |
| 6 | +declare const __VERSION__: string; |
| 7 | + |
| 8 | +async function runBenchmarkSuite( |
| 9 | + suite: Suite, |
| 10 | + runner: NodeBenchmarkRunner, |
| 11 | + parentSuiteName?: string |
| 12 | +) { |
| 13 | + const benchmarkGroup: Benchmark[] = []; |
| 14 | + const benchmarkSuiteGroup: Suite[] = []; |
| 15 | + for (const task of suite.tasks) { |
| 16 | + if (task.mode !== "run") continue; |
| 17 | + |
| 18 | + if (task.meta?.benchmark) benchmarkGroup.push(task as Benchmark); |
| 19 | + else if (task.type === "suite") benchmarkSuiteGroup.push(task); |
| 20 | + } |
| 21 | + |
| 22 | + const currentSuiteName = parentSuiteName |
| 23 | + ? parentSuiteName + "::" + suite.name |
| 24 | + : suite.name; |
| 25 | + |
| 26 | + // run nested suites first |
| 27 | + if (benchmarkSuiteGroup.length) { |
| 28 | + await Promise.all( |
| 29 | + benchmarkSuiteGroup.map((subSuite) => |
| 30 | + runBenchmarkSuite(subSuite, runner, currentSuiteName) |
| 31 | + ) |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + // return early if there are no benchmarks to run |
| 36 | + if (benchmarkGroup.length === 0) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + for (const benchmark of benchmarkGroup) { |
| 41 | + const uri = `${currentSuiteName}::${benchmark.name}`; |
| 42 | + // @ts-expect-error we do not need to bind the function to an instance of tinybench's Bench |
| 43 | + const fn = getBenchFn(benchmark).bind(this); |
| 44 | + |
| 45 | + // run optimizations |
| 46 | + await fn(); |
| 47 | + |
| 48 | + // run instrumented benchmark |
| 49 | + await (async function __codspeed_root_frame__() { |
| 50 | + Measurement.startInstrumentation(); |
| 51 | + await fn(); |
| 52 | + Measurement.stopInstrumentation(uri); |
| 53 | + })(); |
| 54 | + |
| 55 | + // TODO: try to use something like `updateTask` instead to use the output of vitest instead console.log |
| 56 | + console.log(`[CodSpeed] ${uri} done`); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +class CodSpeedRunner extends NodeBenchmarkRunner { |
| 61 | + /** |
| 62 | + * Called before running all tests in collected paths. |
| 63 | + */ |
| 64 | + onBeforeRun() { |
| 65 | + // TODO: try to use something like `updateTask` instead to use the output of vitest instead console.log |
| 66 | + console.log( |
| 67 | + `[CodSpeed] running with @codspeed/vitest-runner v${__VERSION__}` |
| 68 | + ); |
| 69 | + setupCore(); |
| 70 | + } |
| 71 | + |
| 72 | + async runSuite(suite: Suite): Promise<void> { |
| 73 | + // TODO: try to use something like `updateTask` instead to use the output of vitest instead console.log |
| 74 | + console.log("Running suite", suite.name); |
| 75 | + |
| 76 | + await runBenchmarkSuite(suite, this); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Called right after running all tests in collected paths. |
| 81 | + */ |
| 82 | + onAfterRun() { |
| 83 | + teardownCore(); |
| 84 | + // TODO: try to use something like `updateTask` instead to use the output of vitest instead console.log |
| 85 | + console.log(`[CodSpeed] Done running benches.`); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export default CodSpeedRunner; |
0 commit comments