Skip to content

Commit 801bc56

Browse files
feat(tinybench-plugin): bump tinybench and add walltime support
1 parent e48ddb9 commit 801bc56

File tree

20 files changed

+588
-121
lines changed

20 files changed

+588
-121
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"@codspeed/benchmark.js-plugin": "workspace:*",
1010
"@codspeed/tinybench-plugin": "workspace:*",
1111
"benchmark": "^2.1.4",
12-
"tinybench": "^2.5.0"
12+
"tinybench": "^4.0.1"
1313
}
1414
}

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.ts

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import fs from "fs";
2+
import path from "path";
3+
4+
declare const __VERSION__: string;
5+
6+
export interface BenchmarkStats {
7+
min_ns: number;
8+
max_ns: number;
9+
mean_ns: number;
10+
stdev_ns: number;
11+
q1_ns: number;
12+
median_ns: number;
13+
q3_ns: number;
14+
rounds: number;
15+
total_time: number;
16+
iqr_outlier_rounds: number;
17+
stdev_outlier_rounds: number;
18+
iter_per_round: number;
19+
warmup_iters: number;
20+
}
21+
22+
export interface BenchmarkConfig {
23+
warmup_time_ns: number;
24+
min_round_time_ns: number;
25+
max_time_ns: number;
26+
max_rounds: number | null;
27+
}
28+
29+
export interface Benchmark {
30+
name: string;
31+
uri: string;
32+
config: BenchmarkConfig;
33+
stats: BenchmarkStats;
34+
}
35+
36+
export interface InstrumentInfo {
37+
type: string;
38+
clock_info: {
39+
implementation: string;
40+
monotonic: boolean;
41+
adjustable: boolean;
42+
resolution: number;
43+
};
44+
}
45+
46+
export interface ResultData {
47+
creator: {
48+
name: string;
49+
version: string;
50+
pid: number;
51+
};
52+
instrument: InstrumentInfo;
53+
benchmarks: Benchmark[];
54+
}
55+
56+
export function getProfileFolder(): string | null {
57+
return process.env.CODSPEED_PROFILE_FOLDER || null;
58+
}
59+
60+
export function getCreatorMetadata() {
61+
return {
62+
creator: {
63+
name: "@codspeed/core",
64+
version: __VERSION__,
65+
pid: process.pid,
66+
},
67+
};
68+
}
69+
70+
export function getWalltimeInstrumentInfo(): InstrumentInfo {
71+
return {
72+
type: "walltime",
73+
clock_info: {
74+
implementation: "perf_counter",
75+
monotonic: true,
76+
adjustable: false,
77+
resolution: 1e-9, // nanosecond resolution
78+
},
79+
};
80+
}
81+
82+
export function writeWalltimeResults(
83+
benchmarks: Benchmark[],
84+
integrationName: string
85+
) {
86+
const profileFolder = getProfileFolder();
87+
let resultPath: string;
88+
89+
if (profileFolder) {
90+
const resultsDir = path.join(profileFolder, "results");
91+
fs.mkdirSync(resultsDir, { recursive: true });
92+
resultPath = path.join(resultsDir, `${process.pid}.json`);
93+
} else {
94+
// Fallback: write to .codspeed in current working directory
95+
const codspeedDir = path.join(process.cwd(), ".codspeed");
96+
fs.mkdirSync(codspeedDir, { recursive: true });
97+
resultPath = path.join(codspeedDir, `results_${Date.now()}.json`);
98+
}
99+
100+
const data: ResultData = {
101+
creator: {
102+
name: integrationName,
103+
version: __VERSION__,
104+
pid: process.pid,
105+
},
106+
instrument: getWalltimeInstrumentInfo(),
107+
benchmarks: benchmarks,
108+
};
109+
110+
fs.writeFileSync(resultPath, JSON.stringify(data, null, 2));
111+
console.log(`[CodSpeed] Results written to ${resultPath}`);
112+
}
113+
114+
export function createDefaultBenchmarkConfig(): BenchmarkConfig {
115+
return {
116+
warmup_time_ns: 1_000_000_000, // 1 second default
117+
min_round_time_ns: 1_000_000, // 1ms default
118+
max_time_ns: 3_000_000_000, // 3 seconds default
119+
max_rounds: null,
120+
};
121+
}

packages/tinybench-plugin/benches/sample.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Bench } from "tinybench";
22
import { withCodSpeed } from "..";
33
import parsePr from "./parsePr";
4+
import { registerTimingBenchmarks } from "./timing";
45

56
const LONG_BODY =
67
new Array(1_000)
@@ -33,6 +34,8 @@ bench
3334
parsePr({ body: LONG_BODY, title: "test", number: 124 });
3435
});
3536

37+
registerTimingBenchmarks(bench);
38+
3639
bench.run().then(() => {
3740
console.table(bench.table());
3841
});

0 commit comments

Comments
 (0)