Skip to content

Commit 012d605

Browse files
fix(tinybench-plugin): prevent multiple benches from overriding each other
1 parent ec1089a commit 012d605

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

packages/core/src/walltime/index.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,28 @@ export function writeWalltimeResults(benchmarks: Benchmark[]) {
1212
const profileFolder = getProfileFolder();
1313
let resultPath: string;
1414

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

2639
const data: ResultData = {
@@ -30,11 +43,13 @@ export function writeWalltimeResults(benchmarks: Benchmark[]) {
3043
pid: process.pid,
3144
},
3245
instrument: { type: "walltime" },
33-
benchmarks: benchmarks,
46+
benchmarks: [...existingBenchmarks, ...benchmarks],
3447
};
3548

3649
fs.writeFileSync(resultPath, JSON.stringify(data, null, 2));
37-
console.log(`[CodSpeed] Results written to ${resultPath}`);
50+
console.log(
51+
`[CodSpeed] Results written to ${resultPath} (${data.benchmarks.length} total benchmarks)`
52+
);
3853
}
3954

4055
export * from "./interfaces";

0 commit comments

Comments
 (0)