Skip to content

Commit b1db2f2

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

File tree

19 files changed

+557
-119
lines changed

19 files changed

+557
-119
lines changed

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/bench/tinybench/index.bench.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { withCodSpeed } from "@codspeed/tinybench-plugin";
22
import { Bench } from "tinybench";
33
import { registerFiboBenchmarks } from "./fibo.bench";
44
import { registerFoobarbazBenchmarks } from "./foobarbaz.bench";
5+
import { registerTimingBenchmarks } from "./timing.bench";
56

67
export const bench = withCodSpeed(new Bench());
78

89
(async () => {
910
registerFiboBenchmarks(bench);
1011
registerFoobarbazBenchmarks(bench);
12+
registerTimingBenchmarks(bench);
1113

1214
await bench.run();
1315
console.table(bench.table());
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { Bench } from "tinybench";
2+
3+
const sleep = (ms: number): Promise<void> => {
4+
return new Promise((resolve) => setTimeout(resolve, ms));
5+
};
6+
7+
export function registerTimingBenchmarks(bench: Bench) {
8+
bench.add("wait 1ms", async () => {
9+
await sleep(1);
10+
});
11+
12+
bench.add("wait 500ms", async () => {
13+
await sleep(500);
14+
});
15+
16+
bench.add("wait 1sec", async () => {
17+
await sleep(1000);
18+
});
19+
}

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+
}

0 commit comments

Comments
 (0)