import { median, spread } from './stats.mjs' import { importGroupTable } from './summary.mjs' /** Markdown tables of a results file r4-import.mjs wrote. */ const MiB = 1024 * 1024 const W2_MS = 1000 const orDash = (format) => (value) => value === null || value === undefined || Number.isNaN(value) ? '–' : format(value) const mb = orDash((bytes) => (bytes / MiB).toFixed(1)) const ms = orDash((value) => value.toFixed(0)) function table(header, rows) { const line = (cells) => `| ${cells.join(' | ')} |` const rule = line(header.map(() => '---')) return [line(header), rule, ...rows.map(line)].join('\n') } function groupBy(items, keyOf) { const groups = new Map() for (const item of items) { const key = keyOf(item) groups.set(key, [...(groups.get(key) ?? []), item]) } return groups } const values = (runs, pick) => runs.map(pick).filter(Number.isFinite) const maxOf = (list) => (list.length ? Math.max(...list) : null) /** "median (min–max)" or the single value. */ function range(list, format) { const stats = spread(list) if (!stats) return '–' if (stats.n === 1) return format(stats.median) return `${format(stats.median)} (${format(stats.min)}–${format(stats.max)})` } const growth = (r) => r.rssPeakBytes - r.rssBeforeBytes const contentGrowth = (r) => r.rssPeakContentBytes - r.rssBeforeContentBytes const linearGrowth = (r) => r.linear?.afterBytes - r.linear?.beforeRunBytes export const outcomeOf = (r) => r.resultKind ? `${r.outcome} ${r.resultKind}` : r.outcome function rustTimingRow([key, runs]) { const [engine, group] = key.split(' ') return [ engine, group, String(runs.length), range( values(runs, (r) => r.pageMs), ms, ), range( values(runs, (r) => r.startMs), ms, ), range( values(runs, (r) => r.taskMs), ms, ), ms(maxOf(values(runs, (r) => r.parseMs))), mb(maxOf(values(runs, linearGrowth))), range( values(runs, (r) => r.compileMs), ms, ), ] } export function rustTimingTable(runs) { const header = [ 'Engine', 'Group', 'Files', 'Page ms', 'Worker start ms', 'Worker task ms', 'Max parse ms', 'Max linear growth MiB', 'Compile ms (page load)', ] const groups = groupBy(runs, (r) => `${r.engine} ${r.group}`) return table(header, [...groups].map(rustTimingRow)) } function hostileRow(r) { return [ r.engine, r.class, mb(r.fileBytes), outcomeOf(r), ms(r.pageMs), ms(r.startMs), ms(r.taskMs), mb(growth(r)), mb(contentGrowth(r)), mb(r.linear?.afterBytes), mb(linearGrowth(r)), mb(r.settled?.contentDeltaBytes), ] } export function hostileTable(runs) { const header = [ 'Engine', 'Class', 'File MiB', 'Outcome', 'Page ms', 'Start ms', 'Task ms', 'Summed peak growth MiB', 'Content peak growth MiB', 'Linear MiB', 'Linear growth MiB', 'Settled content delta MiB', ] return table(header, runs.map(hostileRow)) } function proofCell(r) { const p = r.proofValue if (!p) return r.detail ?? '–' const linear = `linear ${mb(p.linearBytes)} MiB` if ('second' in p) { const first = p.first.returned ?? p.first.threw const second = p.second.returned ?? p.second.threw return `256 MiB → ${first}; 1100 MiB → ${second}; ${linear}` } const run = p.run.returned ?? p.run.threw return `${run} after ${ms(p.run.ms)} ms; ${linear}` } export function proofsTable(runs) { const header = [ 'Engine', 'Proof', 'Outcome', 'Result', 'Summed peak growth MiB', 'Content peak growth MiB', 'Settled content delta MiB', ] const rows = runs.map((r) => [ r.engine, r.proof, r.outcome, proofCell(r), mb(growth(r)), mb(contentGrowth(r)), mb(r.settled?.contentDeltaBytes), ]) return table(header, rows) } /** Settled content level before and after each import of one page. */ export function repeatTable(runs) { const header = [ 'Engine', 'Class', 'Settle', 'Outcomes', 'Content levels MiB', 'Delta per import MiB', ] const list = (bytes) => (bytes ?? []).map(mb).join(' → ') const rows = runs.map((r) => [ r.engine, r.class, r.settleMode ?? '–', (r.outcomes ?? []).join(', ') || r.outcome, list(r.contentLevels), (r.contentDeltas ?? []).map(mb).join(', ') || r.error || '–', ]) return table(header, rows) } export function workerFactsTable(facts) { const header = [ 'Engine', 'Module cloned', 'URL fallback', 'Clone errors', 'Workers ready', 'Not ready', 'CSP violations (page + worker)', ] const rows = Object.entries(facts).map(([engine, f]) => [ engine, String(f.clone), String(f.fetch), f.cloneErrors.join('; ') || 'none', String(f.started), String(f.notStarted), f.csp.join('; ') || 'none', ]) return table(header, rows) } /** Plan §B.3: back within 10 MiB of the pre-import level after GC. */ export function settledCheck(runs) { const checked = runs.filter((r) => r.settled?.mode === 'gc') if (!checked.length) return 'No run was settled by a forced GC.' const over = checked.filter((r) => !r.settled.contentWithin10MiB) const deltas = checked.map((r) => r.settled.contentDeltaBytes) const lines = [ `${checked.length} Chromium runs settled by a forced GC: ` + `${checked.length - over.length} within 10 MiB of the pre-import ` + `content level; delta median ${mb(median(deltas))} MiB, ` + `max ${mb(maxOf(deltas))} MiB, min ${mb(Math.min(...deltas))} MiB.`, ] for (const r of over) { const what = r.file ?? r.class ?? r.proof lines.push(`- over: ${what} ${mb(r.settled.contentDeltaBytes)} MiB`) } return lines.join('\n') } /** W2: worker start + the L-size import ≤ 1 s (Chromium, this machine). */ export function w2Line(runs) { const l = runs.find( (r) => r.engine === 'chromium' && r.class === 'L-envelope', ) if (!l) return 'W2: no Chromium L-envelope run.' const verdict = l.pageMs <= W2_MS ? 'within' : 'over' return ( `W2 (Chromium, L envelope): page ${ms(l.pageMs)} ms = start ` + `${ms(l.startMs)} ms + task ${ms(l.taskMs)} ms + routing, transfer ` + `and parse; ${verdict} the 1 s budget (${outcomeOf(l)}).` ) } export function headLines(result) { const engines = Object.entries(result.engines) .map(([e, v]) => `${e} ${v}`) .join(', ') const pkg = result.module.pkg const max = pkg.memory?.maximumBytes return [ `Host: ${result.host.cpuModel}, ${result.host.logicalCpus} threads, ` + `${mb(result.host.ramBytes)} MiB RAM.`, `Engines: ${engines} (playwright-core ${result.playwrightCore}), headless.`, `Module: ${pkg.dir}/faunosteo_wasm_bg.wasm ${pkg.wasmBytes} bytes, ` + `sha256 ${pkg.wasmSha256}, memory maximum ${mb(max)} MiB, ` + `stamp current: ${result.module.pkgStampCurrent}.`, `CSP served: ${result.server.csp}`, `Watchdog: ${result.watchdog.caveat}`, ] } export function r4Summary(result) { const parts = [...headLines(result)] if (result.import.runs.length) { parts.push( 'Import by group (Rust worker):', importGroupTable(result.import), ) parts.push('Rust worker timings:', rustTimingTable(result.import.runs)) } if (result.hostileClasses.runs.length) { parts.push('Hostile classes:', hostileTable(result.hostileClasses.runs)) parts.push(w2Line(result.hostileClasses.runs)) } if (result.repeat.runs.length) { parts.push('Repeated imports, one page:', repeatTable(result.repeat.runs)) } if (result.proofs.runs.length) { parts.push('Memory proofs (pkg-proofs):', proofsTable(result.proofs.runs)) } parts.push('Worker facts:', workerFactsTable(result.workerFacts)) const all = [ ...result.import.runs, ...result.hostileClasses.runs, ...result.proofs.runs, ] parts.push(settledCheck(all)) return parts.join('\n\n') }