Skip to content

Commit 5a51ace

Browse files
GuillaumeLagrangeart049
authored andcommitted
feat(core): add a warning in results.json about walltime profiling code
1 parent 8b062ba commit 5a51ace

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

packages/core/src/walltime/index.ts

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

2641
const data: ResultData = {
@@ -30,11 +45,18 @@ export function writeWalltimeResults(benchmarks: Benchmark[]) {
3045
pid: process.pid,
3146
},
3247
instrument: { type: "walltime" },
33-
benchmarks: benchmarks,
48+
benchmarks: [...existingBenchmarks, ...benchmarks],
49+
metadata: asyncWarning
50+
? {
51+
async_warning: "Profiling is inaccurate due to async operations",
52+
}
53+
: undefined,
3454
};
3555

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

4062
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
@@ -83,7 +83,7 @@ export function runWalltimeBench(bench: Bench, rootCallingFile: string): void {
8383

8484
// Write results to JSON file using core function
8585
if (benchmarks.length > 0) {
86-
writeWalltimeResults(benchmarks);
86+
writeWalltimeResults(benchmarks, true);
8787
}
8888

8989
console.log(

0 commit comments

Comments
 (0)