Skip to content

Commit 2b1bdc9

Browse files
feat(tinybench-plugin): bump tinybench and add walltime support
1 parent c4947c9 commit 2b1bdc9

File tree

22 files changed

+583
-147
lines changed

22 files changed

+583
-147
lines changed

.github/workflows/codspeed.yml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ on:
77
workflow_dispatch:
88

99
jobs:
10-
codspeed:
11-
name: Run CodSpeed
10+
codspeed-instrumented:
11+
name: Run CodSpeed instrumented
1212
runs-on: "ubuntu-latest"
1313
steps:
1414
- uses: "actions/checkout@v4"
@@ -35,3 +35,31 @@ jobs:
3535
pnpm --workspace-concurrency 1 -r bench-tinybench
3636
pnpm --workspace-concurrency 1 -r bench-benchmark-js
3737
pnpm --workspace-concurrency 1 -r bench-vitest
38+
39+
codspeed-walltime:
40+
name: Run CodSpeed walltime
41+
runs-on: "codspeed-macro"
42+
steps:
43+
- uses: "actions/checkout@v4"
44+
with:
45+
fetch-depth: 0
46+
- name: Install valgrind
47+
run: |
48+
sudo apt-get update
49+
sudo apt-get install -y valgrind
50+
- uses: pnpm/action-setup@v2
51+
- uses: actions/setup-node@v3
52+
with:
53+
cache: pnpm
54+
node-version-file: .nvmrc
55+
- run: pnpm install --frozen-lockfile --prefer-offline
56+
- run: pnpm moon run :build
57+
58+
- name: Run benchmarks
59+
# use version from `main` branch to always test the latest version, in real projects, use a tag, like `@v2`
60+
uses: CodSpeedHQ/action@main
61+
with:
62+
# Only tinybench supports walltime for now
63+
run: |
64+
pnpm moon run tinybench-plugin:bench
65+
pnpm --workspace-concurrency 1 -r bench-tinybench

examples/with-javascript-cjs/package.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/with-javascript-esm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"@codspeed/benchmark.js-plugin": "workspace:*",
1111
"@codspeed/tinybench-plugin": "workspace:*",
1212
"benchmark": "^2.1.4",
13-
"tinybench": "^2.5.0"
13+
"tinybench": "^4.0.1"
1414
}
1515
}

examples/with-typescript-cjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"@types/benchmark": "^2.1.2",
1212
"benchmark": "^2.1.4",
1313
"esbuild-register": "^3.4.2",
14-
"tinybench": "^2.5.0",
14+
"tinybench": "^4.0.1",
1515
"typescript": "^5.1.3"
1616
}
1717
}

examples/with-typescript-esm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"@types/benchmark": "^2.1.2",
1515
"benchmark": "^2.1.4",
1616
"esbuild-register": "^3.4.2",
17-
"tinybench": "^2.5.0",
17+
"tinybench": "^4.0.1",
1818
"typescript": "^5.1.3",
1919
"vitest": "^1.2.2"
2020
}

examples/with-typescript-simple-cjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"@types/benchmark": "^2.1.2",
1212
"benchmark": "^2.1.4",
1313
"esbuild-register": "^3.4.2",
14-
"tinybench": "^2.5.0",
14+
"tinybench": "^4.0.1",
1515
"typescript": "^5.1.3"
1616
}
1717
}

examples/with-typescript-simple-esm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@types/benchmark": "^2.1.2",
1313
"benchmark": "^2.1.4",
1414
"esbuild-register": "^3.4.2",
15-
"tinybench": "^2.5.0",
15+
"tinybench": "^4.0.1",
1616
"typescript": "^5.1.3"
1717
}
1818
}

packages/core/src/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ export const isBound = native_core.isBound;
1010

1111
export const mongoMeasurement = new MongoMeasurement();
1212

13+
export enum MeasurementMode {
14+
Instrumentation = "instrumentation",
15+
WallTime = "walltime",
16+
}
17+
18+
export function getMeasurementMode(): MeasurementMode {
19+
const isCodSpeedEnabled = process.env.CODSPEED_ENV !== undefined;
20+
if (isCodSpeedEnabled) {
21+
// If CODSPEED_ENV is set, check CODSPEED_RUNNER_MODE
22+
if (process.env.CODSPEED_RUNNER_MODE === "walltime") {
23+
return MeasurementMode.WallTime;
24+
} else {
25+
return MeasurementMode.Instrumentation;
26+
}
27+
}
28+
29+
// Default to walltime mode when CODSPEED_ENV is not set
30+
return MeasurementMode.WallTime;
31+
}
32+
1333
export const setupCore = () => {
1434
native_core.Measurement.stopInstrumentation(
1535
`Metadata: codspeed-node ${__VERSION__}`
@@ -29,4 +49,5 @@ export type {
2949
export { getV8Flags, tryIntrospect } from "./introspection";
3050
export { optimizeFunction, optimizeFunctionSync } from "./optimization";
3151
export * from "./utils";
52+
export * from "./walltime";
3253
export const Measurement = native_core.Measurement;

packages/core/src/walltime/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import { Benchmark, ResultData } from "./interfaces";
4+
5+
declare const __VERSION__: string;
6+
7+
export function getProfileFolder(): string | null {
8+
return process.env.CODSPEED_PROFILE_FOLDER || null;
9+
}
10+
11+
export function writeWalltimeResults(benchmarks: Benchmark[]) {
12+
const profileFolder = getProfileFolder();
13+
let resultPath: string;
14+
15+
if (profileFolder) {
16+
const resultsDir = path.join(profileFolder, "results");
17+
fs.mkdirSync(resultsDir, { recursive: true });
18+
resultPath = path.join(resultsDir, `${process.pid}.json`);
19+
} else {
20+
// Fallback: write to .codspeed in current working directory
21+
const codspeedDir = path.join(process.cwd(), ".codspeed");
22+
fs.mkdirSync(codspeedDir, { recursive: true });
23+
resultPath = path.join(codspeedDir, `results_${Date.now()}.json`);
24+
}
25+
26+
const data: ResultData = {
27+
creator: {
28+
name: "codspeed-node",
29+
version: __VERSION__,
30+
pid: process.pid,
31+
},
32+
instrument: { type: "walltime" },
33+
benchmarks: benchmarks,
34+
};
35+
36+
fs.writeFileSync(resultPath, JSON.stringify(data, null, 2));
37+
console.log(`[CodSpeed] Results written to ${resultPath}`);
38+
}
39+
40+
export * from "./interfaces";
41+
export * from "./quantiles";
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export interface BenchmarkStats {
2+
min_ns: number;
3+
max_ns: number;
4+
mean_ns: number;
5+
stdev_ns: number;
6+
q1_ns: number;
7+
median_ns: number;
8+
q3_ns: number;
9+
rounds: number;
10+
total_time: number;
11+
iqr_outlier_rounds: number;
12+
stdev_outlier_rounds: number;
13+
iter_per_round: number;
14+
warmup_iters: number;
15+
}
16+
17+
export interface BenchmarkConfig {
18+
warmup_time_ns: number | null;
19+
min_round_time_ns: number | null;
20+
max_time_ns?: number | null;
21+
max_rounds?: number | null;
22+
}
23+
24+
export interface Benchmark {
25+
name: string;
26+
uri: string;
27+
config: BenchmarkConfig;
28+
stats: BenchmarkStats;
29+
}
30+
31+
export interface InstrumentInfo {
32+
type: string;
33+
}
34+
35+
export interface ResultData {
36+
creator: {
37+
name: string;
38+
version: string;
39+
pid: number;
40+
};
41+
instrument: { type: "walltime" };
42+
benchmarks: Benchmark[];
43+
}

0 commit comments

Comments
 (0)