|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import { Benchmark, ResultData } from "./interfaces"; |
| 4 | + |
| 5 | +declare const __VERSION__: string; |
| 6 | + |
| 7 | +export function getProfileFolder(): string | null { |
| 8 | + return process.env.CODSPEED_PROFILE_FOLDER || null; |
| 9 | +} |
| 10 | + |
| 11 | +export function writeWalltimeResults(benchmarks: Benchmark[]) { |
| 12 | + 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`); |
| 24 | + } |
| 25 | + |
| 26 | + const data: ResultData = { |
| 27 | + creator: { |
| 28 | + name: "codspeed-node", |
| 29 | + version: __VERSION__, |
| 30 | + pid: process.pid, |
| 31 | + }, |
| 32 | + instrument: { type: "walltime" }, |
| 33 | + benchmarks: benchmarks, |
| 34 | + }; |
| 35 | + |
| 36 | + fs.writeFileSync(resultPath, JSON.stringify(data, null, 2)); |
| 37 | + console.log(`[CodSpeed] Results written to ${resultPath}`); |
| 38 | +} |
| 39 | + |
| 40 | +export * from "./interfaces"; |
| 41 | +export * from "./quantiles"; |
| 42 | +export * from "./utils"; |
0 commit comments