Skip to content

Commit 451c68f

Browse files
--wip-- [skip ci]
1 parent e48ddb9 commit 451c68f

File tree

15 files changed

+876
-117
lines changed

15 files changed

+876
-117
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ 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+
if (process.env.CODSPEED_RUNNER_MODE === "instrumentation") {
20+
return MeasurementMode.Instrumentation;
21+
}
22+
23+
return MeasurementMode.WallTime;
24+
}
25+
1326
export const setupCore = () => {
1427
native_core.Measurement.stopInstrumentation(
1528
`Metadata: codspeed-node ${__VERSION__}`
@@ -29,4 +42,5 @@ export type {
2942
export { getV8Flags, tryIntrospect } from "./introspection";
3043
export { optimizeFunction, optimizeFunctionSync } from "./optimization";
3144
export * from "./utils";
45+
export * from "./walltime";
3246
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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"creator": {
3+
"name": "@codspeed/tinybench-plugin",
4+
"version": "4.0.1",
5+
"pid": 1117216
6+
},
7+
"instrument": {
8+
"type": "walltime",
9+
"clock_info": {
10+
"implementation": "perf_counter",
11+
"monotonic": true,
12+
"adjustable": false,
13+
"resolution": 1e-9
14+
}
15+
},
16+
"benchmarks": [
17+
{
18+
"name": "RegExp",
19+
"uri": "packages/tinybench-plugin/tests/index.integ.test.ts::RegExp",
20+
"config": {
21+
"warmup_time_ns": 1000000000,
22+
"min_round_time_ns": 1000000,
23+
"max_time_ns": 3000000000,
24+
"max_rounds": null
25+
},
26+
"stats": {
27+
"min_ns": 115.00716209411621,
28+
"max_ns": 312857.0020198822,
29+
"mean_ns": 155.2439173289162,
30+
"stdev_ns": 711.1750982143501,
31+
"q1_ns": 135.00452041625977,
32+
"median_ns": 139.99640941619873,
33+
"q3_ns": 145.00319957733154,
34+
"total_time": 0.10000005885958671,
35+
"iter_per_round": 644148,
36+
"rounds": 1,
37+
"iqr_outlier_rounds": 65073,
38+
"stdev_outlier_rounds": 691,
39+
"warmup_iters": 0
40+
}
41+
}
42+
]
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"creator": {
3+
"name": "@codspeed/tinybench-plugin",
4+
"version": "4.0.1",
5+
"pid": 1118088
6+
},
7+
"instrument": {
8+
"type": "walltime",
9+
"clock_info": {
10+
"implementation": "perf_counter",
11+
"monotonic": true,
12+
"adjustable": false,
13+
"resolution": 1e-9
14+
}
15+
},
16+
"benchmarks": [
17+
{
18+
"name": "RegExp",
19+
"uri": "packages/tinybench-plugin/tests/index.integ.test.ts::RegExp",
20+
"config": {
21+
"warmup_time_ns": 1000000000,
22+
"min_round_time_ns": 1000000,
23+
"max_time_ns": 3000000000,
24+
"max_rounds": null
25+
},
26+
"stats": {
27+
"min_ns": 114.99226093292236,
28+
"max_ns": 267071.99215888977,
29+
"mean_ns": 153.09416204669492,
30+
"stdev_ns": 694.3098124168152,
31+
"q1_ns": 129.99773025512695,
32+
"median_ns": 134.00614261627197,
33+
"q3_ns": 139.99640941619873,
34+
"total_time": 0.10000080046057701,
35+
"iter_per_round": 653198,
36+
"rounds": 1,
37+
"iqr_outlier_rounds": 94000,
38+
"stdev_outlier_rounds": 691,
39+
"warmup_iters": 0
40+
}
41+
}
42+
]
43+
}

0 commit comments

Comments
 (0)