Spaces:
Running
Running
| import { mkdirSync, renameSync, writeFileSync } from "node:fs"; | |
| import { join, resolve } from "node:path"; | |
| const DEFAULT_REPORT_DIRECTORY = "/tmp/agente-ai"; | |
| function safeSegment(value) { | |
| return String(value ?? "benchmark") | |
| .trim() | |
| .replace(/[^a-zA-Z0-9._-]+/g, "-") | |
| .replace(/^-+|-+$/g, "") | |
| .slice(0, 96) || "benchmark"; | |
| } | |
| /** | |
| * Persiste un artefatto diagnostico del runner senza incidere sui calcoli. | |
| * Il write-then-rename evita file JSON parziali in caso di interruzione. | |
| */ | |
| export function saveBenchmarkReport(reportName, payload) { | |
| const reportDirectory = resolve(process.env.BENCHMARK_REPORT_DIR || DEFAULT_REPORT_DIRECTORY); | |
| mkdirSync(reportDirectory, { recursive: true }); | |
| const safeName = safeSegment(reportName); | |
| const stamp = new Date().toISOString().replace(/[:.]/g, "-"); | |
| const finalPath = join(reportDirectory, `${safeName}-${stamp}.json`); | |
| const temporaryPath = `${finalPath}.${process.pid}.tmp`; | |
| const document = { | |
| schemaVersion: "benchmark-report-v1", | |
| generatedAt: new Date().toISOString(), | |
| reportName: safeName, | |
| ...payload, | |
| }; | |
| writeFileSync(temporaryPath, `${JSON.stringify(document, null, 2)}\n`, { encoding: "utf8", mode: 0o600 }); | |
| renameSync(temporaryPath, finalPath); | |
| return finalPath; | |
| } | |