-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.ts
More file actions
64 lines (56 loc) · 1.77 KB
/
index.ts
File metadata and controls
64 lines (56 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import fs from "fs";
import path from "path";
import { Benchmark, ResultData } from "./interfaces";
declare const __VERSION__: string;
export function getProfileFolder(): string | null {
return process.env.CODSPEED_PROFILE_FOLDER || null;
}
export function writeWalltimeResults(
benchmarks: Benchmark[],
asyncWarning = false
): void {
const profileFolder = getProfileFolder();
const resultDir = (() => {
if (profileFolder) {
return path.join(profileFolder, "results");
} else {
// Fallback: write to .codspeed in current working directory
return path.join(process.cwd(), ".codspeed");
}
})();
fs.mkdirSync(resultDir, { recursive: true });
const resultPath = path.join(resultDir, `${process.pid}.json`);
// Check if file already exists and merge benchmarks
let existingBenchmarks: Benchmark[] = [];
if (fs.existsSync(resultPath)) {
try {
const existingData = JSON.parse(
fs.readFileSync(resultPath, "utf-8")
) as ResultData;
existingBenchmarks = existingData.benchmarks || [];
} catch (error) {
console.warn(`[CodSpeed] Failed to read existing results file: ${error}`);
}
}
const data: ResultData = {
creator: {
name: "codspeed-node",
version: __VERSION__,
pid: process.pid,
},
instrument: { type: "walltime" },
benchmarks: [...existingBenchmarks, ...benchmarks],
metadata: asyncWarning
? {
async_warning: "Profiling is inaccurate due to async operations",
}
: undefined,
};
fs.writeFileSync(resultPath, JSON.stringify(data, null, 2));
console.log(
`[CodSpeed] Results written to ${resultPath} (${data.benchmarks.length} total benchmarks)`
);
}
export * from "./interfaces";
export * from "./quantiles";
export * from "./utils";