import { appendFileSync, mkdirSync } from 'node:fs'; import path from 'node:path'; import { PERF_DIR } from './manifest'; function pad(n: number) { return String(n).padStart(2, '0'); } export function runId(): string { if (process.env.PERF_RUN_ID) return process.env.PERF_RUN_ID; const d = new Date(); return `${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}-${pad(d.getHours())}${pad(d.getMinutes())}`; } /** Каталог прогона: PERF_RESULTS_DIR (тесты) или perf/results/. */ export function runDir(): string { return process.env.PERF_RESULTS_DIR ?? path.join(PERF_DIR, 'results', runId()); } export interface ResultRecord { run_id?: string; scenario: string; target: string; role: string; scale: number; cpu_rate: number; repeat: number; metrics: Record; artifacts: string[]; incomplete?: boolean; skip_reason?: string; ts?: string; } /** Формат — docstring perf/perfkit/results.py: perf/results//.jsonl. */ export function append(rec: ResultRecord): string { const dir = runDir(); mkdirSync(dir, { recursive: true }); const full = { run_id: runId(), incomplete: false, ts: new Date().toISOString().replace(/\.\d+Z$/, 'Z'), ...rec, }; const file = path.join(dir, `${rec.scenario}.jsonl`); appendFileSync(file, JSON.stringify(full) + '\n', 'utf-8'); return file; }