Skip to content

Commit 92f0d3c

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

File tree

12 files changed

+99
-101
lines changed

12 files changed

+99
-101
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/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/tinybench-plugin/moon.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tasks:
22
bench:
3-
command: node -r esbuild-register benches/sample.ts
3+
command: node --loader esbuild-register/loader -r esbuild-register benches/sample.ts
44
inputs:
55
- "benches/**"
66
local: true

packages/tinybench-plugin/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
"license": "Apache-2.0",
2727
"devDependencies": {
2828
"@types/stack-trace": "^0.0.30",
29-
"tinybench": "^2.5.0",
29+
"esbuild-register": "^3.4.2",
30+
"tinybench": "^4.0.1",
3031
"vitest": "^1.2.2"
3132
},
3233
"dependencies": {
3334
"@codspeed/core": "workspace:^4.0.1",
3435
"stack-trace": "1.0.0-pre2"
3536
},
3637
"peerDependencies": {
37-
"tinybench": "^2.3.0"
38+
"tinybench": "^4.0.1"
3839
}
3940
}

packages/tinybench-plugin/src/index.ts

Lines changed: 36 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,64 @@
11
import {
22
getGitDir,
3+
getMeasurementMode,
34
Measurement,
5+
MeasurementMode,
46
mongoMeasurement,
5-
optimizeFunction,
6-
setupCore,
77
SetupInstrumentsRequestBody,
88
SetupInstrumentsResponse,
9-
teardownCore,
109
tryIntrospect,
1110
} from "@codspeed/core";
1211
import path from "path";
1312
import { get as getStackTrace } from "stack-trace";
14-
import { Bench, Task } from "tinybench";
13+
import { Bench } from "tinybench";
1514
import { fileURLToPath } from "url";
16-
17-
declare const __VERSION__: string;
15+
import { runInstrumentedBench } from "./instrumented";
16+
import { runWalltimeBench } from "./walltime";
1817

1918
tryIntrospect();
2019

21-
type CodSpeedBenchOptions = Task["opts"] & {
22-
uri: string;
23-
};
24-
25-
function isCodSpeedBenchOptions(
26-
options: Task["opts"]
27-
): options is CodSpeedBenchOptions {
28-
return "uri" in options;
29-
}
20+
// Store URI mapping externally since fnOpts is private
21+
export const taskUriMap = new WeakMap<Bench, Map<string, string>>();
3022

3123
export function withCodSpeed(bench: Bench): Bench {
32-
if (!Measurement.isInstrumented()) {
24+
const measurementMode = getMeasurementMode();
25+
const rootCallingFile = getCallingFile();
26+
27+
// Initialize URI mapping for this bench instance
28+
if (!taskUriMap.has(bench)) {
29+
taskUriMap.set(bench, new Map());
30+
}
31+
const uriMap = taskUriMap.get(bench)!;
32+
33+
// Setup URI generation for tasks
34+
const rawAdd = bench.add;
35+
bench.add = (name, fn, opts?) => {
36+
const callingFile = getCallingFile();
37+
const uri = `${callingFile}::${name}`;
38+
// Store URI mapping
39+
uriMap.set(name, uri);
40+
return rawAdd.bind(bench)(name, fn, opts);
41+
};
42+
43+
// Apply the appropriate measurement strategy based on mode and instrumentation
44+
if (
45+
measurementMode === MeasurementMode.Instrumentation &&
46+
Measurement.isInstrumented()
47+
) {
48+
runInstrumentedBench(bench, rootCallingFile);
49+
} else if (measurementMode === MeasurementMode.WallTime) {
50+
runWalltimeBench(bench, rootCallingFile);
51+
} else {
52+
// Fallback: instrumentation requested but not available
3353
const rawRun = bench.run;
3454
bench.run = async () => {
3555
console.warn(
3656
`[CodSpeed] ${bench.tasks.length} benches detected but no instrumentation found, falling back to tinybench`
3757
);
3858
return await rawRun.bind(bench)();
3959
};
40-
return bench;
4160
}
4261

43-
const rawAdd = bench.add;
44-
bench.add = (name, fn, opts: CodSpeedBenchOptions) => {
45-
const callingFile = getCallingFile();
46-
const uri = `${callingFile}::${name}`;
47-
const options = Object.assign({}, opts ?? {}, { uri });
48-
return rawAdd.bind(bench)(name, fn, options);
49-
};
50-
const rootCallingFile = getCallingFile();
51-
52-
bench.run = async () => {
53-
console.log(`[CodSpeed] running with @codspeed/tinybench v${__VERSION__}`);
54-
setupCore();
55-
for (const task of bench.tasks) {
56-
const uri = isCodSpeedBenchOptions(task.opts)
57-
? task.opts.uri
58-
: `${rootCallingFile}::${task.name}`;
59-
60-
await task.opts.beforeAll?.call(task);
61-
62-
// run optimizations
63-
await optimizeFunction(async () => {
64-
await task.opts.beforeEach?.call(task);
65-
await task.fn();
66-
await task.opts.afterEach?.call(task);
67-
});
68-
69-
// run instrumented benchmark
70-
await task.opts.beforeEach?.call(task);
71-
72-
await mongoMeasurement.start(uri);
73-
global.gc?.();
74-
await (async function __codspeed_root_frame__() {
75-
Measurement.startInstrumentation();
76-
await task.fn();
77-
Measurement.stopInstrumentation(uri);
78-
})();
79-
await mongoMeasurement.stop(uri);
80-
81-
await task.opts.afterEach?.call(task);
82-
83-
await task.opts.afterAll?.call(task);
84-
85-
// print results
86-
console.log(` ✔ Measured ${uri}`);
87-
}
88-
teardownCore();
89-
console.log(`[CodSpeed] Done running ${bench.tasks.length} benches.`);
90-
return bench.tasks;
91-
};
9262
return bench;
9363
}
9464

0 commit comments

Comments
 (0)