Skip to content

Commit e91fa4f

Browse files
feat(core): add a warning in results.json about walltime profiling code
1 parent eddc2bc commit e91fa4f

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

packages/core/src/walltime/index.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,35 @@ export function getProfileFolder(): string | null {
88
return process.env.CODSPEED_PROFILE_FOLDER || null;
99
}
1010

11-
export function writeWalltimeResults(benchmarks: Benchmark[]) {
11+
export function writeWalltimeResults(
12+
benchmarks: Benchmark[],
13+
asyncWarning = false
14+
): void {
1215
const profileFolder = getProfileFolder();
1316
let resultPath: string;
1417

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`);
18+
const resultDir = (() => {
19+
if (profileFolder) {
20+
return path.join(profileFolder, "results");
21+
} else {
22+
// Fallback: write to .codspeed in current working directory
23+
return path.join(process.cwd(), ".codspeed");
24+
}
25+
})();
26+
fs.mkdirSync(resultDir, { recursive: true });
27+
resultPath = path.join(resultDir, `${process.pid}.json`);
28+
29+
// Check if file already exists and merge benchmarks
30+
let existingBenchmarks: Benchmark[] = [];
31+
if (fs.existsSync(resultPath)) {
32+
try {
33+
const existingData = JSON.parse(
34+
fs.readFileSync(resultPath, "utf-8")
35+
) as ResultData;
36+
existingBenchmarks = existingData.benchmarks || [];
37+
} catch (error) {
38+
console.warn(`[CodSpeed] Failed to read existing results file: ${error}`);
39+
}
2440
}
2541

2642
const data: ResultData = {
@@ -30,11 +46,18 @@ export function writeWalltimeResults(benchmarks: Benchmark[]) {
3046
pid: process.pid,
3147
},
3248
instrument: { type: "walltime" },
33-
benchmarks: benchmarks,
49+
benchmarks: [...existingBenchmarks, ...benchmarks],
50+
metadata: asyncWarning
51+
? {
52+
async_warning: "Profiling is inaccurate due to async operations",
53+
}
54+
: undefined,
3455
};
3556

3657
fs.writeFileSync(resultPath, JSON.stringify(data, null, 2));
37-
console.log(`[CodSpeed] Results written to ${resultPath}`);
58+
console.log(
59+
`[CodSpeed] Results written to ${resultPath} (${data.benchmarks.length} total benchmarks)`
60+
);
3861
}
3962

4063
export * from "./interfaces";

packages/core/src/walltime/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ export interface ResultData {
4040
};
4141
instrument: { type: "walltime" };
4242
benchmarks: Benchmark[];
43+
metadata?: Record<string, unknown>;
4344
}

packages/tinybench-plugin/src/walltime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function runWalltimeBench(bench: Bench, rootCallingFile: string): void {
8181

8282
// Write results to JSON file using core function
8383
if (benchmarks.length > 0) {
84-
writeWalltimeResults(benchmarks);
84+
writeWalltimeResults(benchmarks, true);
8585
}
8686

8787
console.log(

0 commit comments

Comments
 (0)