File size: 1,275 Bytes
0656077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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;
}