memlnaut-nisps/tests/cpp/bench_report.mjs

180 lines
6.5 KiB
JavaScript
Raw Permalink Normal View History

feat: curve truth, DriverConfig, real telemetry, engine benchmark Four items from one workflow, committed together because their build and CI wiring genuinely interleaves — nisps/CMakeLists.txt, run-all-tests.sh and ci.yml each carry hunks from two of them, and the stage renumbering (1/5 -> 1/6) touches every line. Splitting would produce commits that do not build, which is worse than a commit that does four things and says so. S26 part 2 — the curve declaration now matches reality. params[].curve stays the mode-wide DEFAULT; a voice_spaces entry may now be {name, curve_overrides} declaring only the slots where THAT voice space deviates. The 6 modes with one voice space are byte-identical. The values were derived MECHANICALLY by a new codegen/curve-audit.ts that models the four idioms a p[N]*p[N] regex misses (alias form, memlcelium's implicit-counter sq() lambda, loop-generated indices, smooth_params_), inlines helpers, and RAISES rather than guessing when it cannot reduce an expression. A drift gate cross-checks 1179 (voice space x param) slots against engine source on every run and was proved to fail loudly on three drift classes. Application stays in the engine: nisps/engines, nisps/pipeline and nisps/core are untouched, generated output is pure insertion (755 insertions, 0 deletions), and the rebuilt nisps.wasm was byte-identical. S4 / 7.2 — firmware reads the active mode's driver config at mode start, and mic/line is real. My brief assumed the engine owns this; the code disagreed and the code was right. sound_analysis_midi's EngineT is NoOpEngine — the mic lives on a separately-composed AnalysisEngine member — so engine-level wiring would have compiled, passed every gate, and left the one mic mode on line input. Hence a mode-level seam defaulting to engine().driver_config(). Separately, DriverConfig's defaults (line_level 0, output_volume 1.0) had drifted from memllib's actual 3/0.8 because nothing had ever read them; wiring them as-is would have made every silent mode louder and its line input maximally insensitive — a behaviour change disguised as plumbing. Now pinned by a test. Also: GetSysClockSpeed() panic()s on unsupported sample rates and runs on the first line of setup(), so sample_rate needed a fallback ahead of clock setup. CI's firmware env list gains soundanalysismidi — it is the only mic variant and nothing else compiles that path. Plan 5e — telemetry is real. A loss_history C-API entry across the full 5-layer chain lets the browser read the per-iteration loss the core already records. The audit named one fabrication site; there were two — wasm-iml.ts's synchronous train() published lossHistory: [loss] as well. A third, ctx.loss, was not merely dead but actively synthetic (fallbacks of prev * 0.82 and a literal 0.5, rendered by nothing) and is deleted. The firmware buffer stays untouched, per the L25 call. EngineApi.lossHistory() reads spine state rather than the MLP handle, because trainAsync() fits on the worker's mirror net and the handle would give a subtly-wrong second answer. Plan 5f — engine throughput is measurable. One source compiled twice (CMake natively, emcc for WASM) so the targets compare directly and no WASM export is added. Sequencers are driven into a working state, and every row prints its own working-state evidence so a number produced by an idle engine is visible rather than plausible. Reports, never asserts: a wall-clock threshold on shared hardware is meaningless or flaky, same call as the firmware size job. ALIGNMENT: the telemetry defect is deleted (built, not deferred); the performance defect is rewritten to what is actually left — these are HOST numbers, and nothing measures the RP2350 at 150 MHz, which is the target the mission's constraint is about. Q4 (memllib ownership) and Q5 (legacy feedback modes) are closed. Corrections to my own earlier claims, both found by agents contradicting the brief: manifold/ONBOARDING.md was NOT "now accurate" — its primitives list still named five deleted primitives and cited a seededGradient() that does not exist. And the parity harness misses the sequencer engines because it runs 128 frames while their sequencers evaluate every 400-500 samples, NOT because all-params-0.5 fails to trigger them (it does trigger: 0.5 maps to ratio 2, firing three times per bar). The fix is a longer window, not different params. Gates: run-all-tests.sh ALL GREEN — 4/4 ctest, parity PASS, lint clean, curve drift 1179 slots ok, 39 e2e (was 33). Firmware: 5 envs built including the mic variant.
2026-07-21 22:02:23 +02:00
#!/usr/bin/env node
/**
* tests/cpp/bench_report.mjs merge one or more `engine_bench --json` runs
* into a single report, print a side-by-side table, and (with --compare)
* diff against a previous report.
*
* Sibling of parity_diff.mjs: same place, same job shape the C++ produces
* the numbers, node does the presentation.
*
* node bench_report.mjs <run.json> [<run.json> ...]
* [--out combined.json] [--compare previous.json]
*
* Each input is the JSON object `engine_bench --json` writes; its "target"
* field ("native" / "wasm") names the column. The combined document is
* { "generated": ISO8601, "runs": [ <run>, ... ] }
* and that is also what --compare expects to read.
*
* Exit codes: 0 always on a readable report (this tool asserts nothing
* see the REPORTING, NOT ASSERTING note in engine_bench.cpp), 2 on bad input.
*/
import { readFileSync, writeFileSync } from 'node:fs';
const argv = process.argv.slice(2);
const inputs = [];
let outPath = null;
let comparePath = null;
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
if (a === '--out') outPath = argv[++i];
else if (a === '--compare') comparePath = argv[++i];
else if (a.startsWith('--')) {
console.error(`[bench_report] unknown flag ${a}`);
process.exit(2);
} else inputs.push(a);
}
if (inputs.length === 0) {
console.error('[bench_report] usage: bench_report.mjs <run.json>... [--out FILE] [--compare FILE]');
process.exit(2);
}
function readJson(p) {
try {
return JSON.parse(readFileSync(p, 'utf8'));
} catch (e) {
console.error(`[bench_report] cannot read ${p}: ${e.message}`);
process.exit(2);
}
}
// An input is normally a bare `engine_bench --json` object, but accepting a
// previously combined report too costs one line and removes the obvious trap
// of feeding this tool its own output.
const runs = [];
for (const p of inputs) {
const doc = readJson(p);
if (Array.isArray(doc.runs)) runs.push(...doc.runs);
else runs.push(doc);
}
if (runs.some((r) => !Array.isArray(r.engines))) {
console.error('[bench_report] input is not an engine_bench report (no "engines" array)');
process.exit(2);
}
const combined = { generated: new Date().toISOString(), runs };
if (outPath) {
writeFileSync(outPath, JSON.stringify(combined, null, 2) + '\n');
}
// --- previous report, indexed [target][engine] -> ns_per_sample -------------
let prev = null;
if (comparePath) {
const doc = readJson(comparePath);
const byTarget = new Map();
for (const r of doc.runs ?? [doc]) {
const m = new Map();
for (const e of r.engines ?? []) m.set(e.engine, e);
byTarget.set(r.target, m);
}
prev = { byTarget, generated: doc.generated ?? '(unknown date)' };
}
// --- table -----------------------------------------------------------------
const engines = [];
for (const r of runs) for (const e of r.engines ?? []) {
if (!engines.includes(e.engine)) engines.push(e.engine);
}
const pad = (s, n) => String(s).padStart(n);
const padr = (s, n) => String(s).padEnd(n);
console.log('nisps engine benchmark — combined report');
for (const r of runs) {
console.log(` ${padr(r.target, 8)} block=${r.block_size} sr=${r.sample_rate} ` +
`repeats=${r.repeats} target_ms=${r.target_ms} seed=${r.seed} ` +
`ref=${Number(r.ref_ns_per_op).toFixed(3)} ns/op`);
}
if (prev) console.log(` compared against ${comparePath} (${prev.generated})`);
console.log('');
let header = padr('engine', 14);
for (const r of runs) {
header += ' | ' + padr(`${r.target} ns/smp`, 14) + pad('xRT', 8);
if (prev) header += pad('Δ%', 8);
}
if (runs.length === 2) header += ' | ' + pad(`${runs[1].target}/${runs[0].target}`, 12);
console.log(header);
console.log('-'.repeat(header.length));
for (const id of engines) {
let line = padr(id, 14);
const nsByTarget = [];
for (const r of runs) {
const e = (r.engines ?? []).find((x) => x.engine === id);
if (!e) {
line += ' | ' + padr('-', 14) + pad('-', 8) + (prev ? pad('-', 8) : '');
nsByTarget.push(null);
continue;
}
nsByTarget.push(e.ns_per_sample);
line += ' | ' + padr(e.ns_per_sample.toFixed(2), 14) + pad(e.realtime_x.toFixed(1), 8);
if (prev) {
const p = prev.byTarget.get(r.target)?.get(id);
if (!p || !p.ns_per_sample) line += pad('-', 8);
else {
const d = ((e.ns_per_sample - p.ns_per_sample) / p.ns_per_sample) * 100;
line += pad((d >= 0 ? '+' : '') + d.toFixed(1), 8);
}
}
}
if (runs.length === 2 && nsByTarget[0] && nsByTarget[1]) {
line += ' | ' + pad((nsByTarget[1] / nsByTarget[0]).toFixed(2) + 'x', 12);
} else if (runs.length === 2) {
line += ' | ' + pad('-', 12);
}
console.log(line);
}
console.log('');
console.log(' ns/smp = nanoseconds per sample (lower is faster).');
console.log(' xRT = seconds of audio produced per second of CPU (higher is faster).');
if (prev) console.log(' Δ% = change in ns/sample vs the compared report; POSITIVE means SLOWER.');
console.log(' Nothing here fails a build. See engine_bench.cpp "REPORTING, NOT ASSERTING".');
// A short/unrepeated run is fine as a "does it still work" smoke, and useless
// as a comparison. Say which one you just did rather than letting a ±30% swing
// be read as a regression.
const lowConfidence = runs.filter((r) => (r.repeats ?? 1) < 2 || (r.target_ms ?? 0) < 50);
if (lowConfidence.length) {
console.log('');
console.log(` NOTE: smoke-sized run (${lowConfidence.map((r) => r.target).join(', ')}) — ` +
'noise floor is tens of percent. Not comparison-grade.');
} else if (prev) {
console.log('');
console.log(' Noise floor at these settings is roughly ±3% on an idle machine, ' +
'occasionally ±8%.');
console.log(' Treat |Δ%| under ~10% as noise; a real regression of the kind this ' +
'exists to catch is 2-3x.');
}
// Working-state warnings: a timing number from an idle engine is worthless,
// so say so loudly rather than letting it sit in the table looking fine.
const idle = [];
for (const r of runs) for (const e of r.engines ?? []) {
const ev = String(e.evidence ?? '');
const value = Number(ev.split('=')[1]);
if (e.engine !== 'thru' && Number.isFinite(value) && value === 0) {
idle.push(`${r.target}/${e.engine} (${ev})`);
}
}
if (idle.length) {
console.log('');
console.log(` WARNING: engine(s) showed no working state: ${idle.join(', ')}`);
console.log(' Their timings measure an idle engine and must not be compared.');
}
if (outPath) console.log(`\n wrote ${outPath}`);