Skip to content

Commit 2f7a55b

Browse files
test(vitest-plugin): add with-vitest-v5 example
Benchmark the plugin against Vitest 5 in CI the way the v3 and v4 examples do for their majors, using the `bench` fixture and `bench.compare()`. Refs COD-2931 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 1595df2 commit 2f7a55b

5 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "with-vitest-v5",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"bench-vitest": "vitest bench --run"
7+
},
8+
"devDependencies": {
9+
"@codspeed/vitest-plugin": "workspace:*",
10+
"typescript": "^5.1.3",
11+
"vitest": "^5.0.0"
12+
}
13+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, test } from "vitest";
2+
import {
3+
iterativeFibonacci as iterativeFibonacciExport,
4+
recursiveFibonacci as recursiveFibonacciExport,
5+
} from "./fibonacci";
6+
7+
// Read the imported bindings once: Vite's module runner exposes exports through
8+
// getters, and reading one inside the measured loop adds overhead to every
9+
// iteration (Vitest warns about it).
10+
const recursiveFibonacci = recursiveFibonacciExport;
11+
const iterativeFibonacci = iterativeFibonacciExport;
12+
13+
// Vitest 5 declares benchmarks through the `bench` test-context fixture: each
14+
// `bench()` returns a registration that runs on `.run()`, or as part of a
15+
// `bench.compare()` group.
16+
describe("fibonacci", () => {
17+
test("fibo 15", async ({ bench }) => {
18+
await bench.compare(
19+
bench("recursive", () => {
20+
recursiveFibonacci(15);
21+
}),
22+
bench("iterative", () => {
23+
iterativeFibonacci(15);
24+
}),
25+
);
26+
});
27+
28+
test("fibo 20", async ({ bench }) => {
29+
await bench.compare(
30+
bench("recursive", () => {
31+
recursiveFibonacci(20);
32+
}),
33+
bench("iterative", () => {
34+
iterativeFibonacci(20);
35+
}),
36+
);
37+
});
38+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export function recursiveFibonacci(n: number): number {
2+
if (n < 2) {
3+
return n;
4+
}
5+
return recursiveFibonacci(n - 1) + recursiveFibonacci(n - 2);
6+
}
7+
8+
export function iterativeFibonacci(n: number): number {
9+
let a = 0;
10+
let b = 1;
11+
for (let i = 0; i < n; i++) {
12+
const temp = a + b;
13+
a = b;
14+
b = temp;
15+
}
16+
return a;
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2023"],
4+
"module": "ESNext",
5+
"verbatimModuleSyntax": true,
6+
"target": "es2022",
7+
"strict": true,
8+
"esModuleInterop": true,
9+
"skipLibCheck": true,
10+
"forceConsistentCasingInFileNames": true,
11+
"moduleResolution": "Node"
12+
}
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import codspeedPlugin from "@codspeed/vitest-plugin";
2+
import { defineConfig } from "vitest/config";
3+
4+
export default defineConfig({
5+
plugins: [codspeedPlugin()],
6+
});

0 commit comments

Comments
 (0)