/** * Benchmark inputs for /bench. * * Source of truth is the committed artifact each bench writes: * benches/runtime/results/latest.json (`pnpm bench:runtime:record`) * benches/bundle/results/latest.json (`pnpm --filter @distilled.cloud/bench-bundle record`) * A missing/unparsable file yields `null` and that section is omitted — the * site never fails on bench data. `seed` is kept on the types so an ad-hoc * snapshot can still be flagged as interim if one is ever needed. */ import { readFile } from "node:fs/promises"; import { join } from "node:path"; export interface RuntimeResult { readonly name: string; readonly provider: string; readonly service: string; readonly op: string; readonly stage: string; readonly note: string; readonly opsPerSec: number; /** nanoseconds */ readonly p50: number; readonly p99: number; readonly samples: number; } export interface RuntimeBench { readonly schema: number; readonly generatedAt: string; readonly commit: string; readonly machine: { readonly runtime: string; readonly cpu?: string; readonly os?: string; }; readonly profile: string; readonly seed?: boolean; readonly results: ReadonlyArray; } export interface BundleRow { readonly fixture: string; readonly variant: string; readonly description?: string; readonly coldMs: number; /** `null` when the bench ran once (`--runs 1`). */ readonly warmMs: number | null; readonly bytes: number; readonly gzipBytes: number; readonly moduleCount: number; readonly opsRetained: ReadonlyArray<{ readonly service: string; readonly retained: number; readonly total: number; }>; readonly leaks: number; } export interface BundleBench { readonly schema: number; readonly generatedAt: string; readonly commit: string; readonly host?: { readonly platform?: string; readonly cpu?: string | null; readonly cores?: number; readonly memoryGb?: number; }; readonly rolldown: string; /** Runtime that drove the builds, e.g. `node 24.9.0`. */ readonly runtime?: string; /** Older recordings name the runtime by key instead of `runtime`. */ readonly node?: string; readonly bun?: string; readonly runs: number; readonly seed?: boolean; readonly rows: ReadonlyArray; } const readJson = async (file: string): Promise => { try { return JSON.parse(await readFile(file, "utf8")) as T; } catch { return null; } }; const firstOf = async ( files: ReadonlyArray, valid: (doc: T) => boolean, ): Promise => { for (const file of files) { const doc = await readJson(file); if (doc && valid(doc)) return doc; } return null; }; export const readRuntimeBench = (repoRoot: string): Promise => firstOf( [join(repoRoot, "benches", "runtime", "results", "latest.json")], (d) => d.schema === 1 && Array.isArray(d.results) && d.results.length > 0, ); export const readBundleBench = (repoRoot: string): Promise => firstOf( [join(repoRoot, "benches", "bundle", "results", "latest.json")], (d) => d.schema === 1 && Array.isArray(d.rows) && d.rows.length > 0, );