import { headLines } from './r4-summary.mjs' import { spread } from './stats.mjs' /** * Markdown tables of r4-large-files.mjs (TS baseline vs Rust arrayBuffer vs * Rust stream, per file and engine) and of r4-leak-series.mjs (settled * levels per import in one page, WebKit repetition). */ const MiB = 1024 * 1024 const READ_PREFIX = 'apps/app/tests/fixtures/pdf/r4/read/' const TOLERANCE_BYTES = 10 * MiB 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') } const growth = (r) => r && r.rssPeakBytes - r.rssBeforeBytes const contentGrowth = (r) => r && r.rssPeakContentBytes - r.rssBeforeContentBytes const pair = (a, b) => `${mb(a)} / ${mb(b)}` const outcomeOf = (r) => { if (!r) return '–' return r.resultKind ? `${r.outcome} ${r.resultKind}` : r.outcome } const labelOf = (r) => (r.file ?? r.class).replace(READ_PREFIX, '') /** "byob 791 × ≤ 64 KiB" from the worker's read facts. */ function readCell(r) { if (!r?.readVia) return r?.precheck ? 'not read (pre-check)' : '–' const via = r.readVia.replace('stream-', '') const kib = (r.maxChunkBytes / 1024).toFixed(0) return `${via} ${r.chunks} × ≤ ${kib} KiB` } function outcomeCell(ab, stream) { const a = outcomeOf(ab) const s = outcomeOf(stream) return a === s ? s : `AB ${a}; stream ${s}` } function largeRow(byPath, ts) { const ab = byPath.arrayBuffer const stream = byPath.stream const any = stream ?? ab const gc = (r) => r?.settled?.mode === 'gc' ? r.settled.contentDeltaBytes : null return [ labelOf(any), mb(any.fileBytes), any.engine, outcomeCell(ab, stream), ts ? pair(ts.growthBytes, ts.contentGrowthBytes) : '–', ms(ts?.durationMs), pair(growth(ab), contentGrowth(ab)), ms(ab?.durationMs), pair(growth(stream), contentGrowth(stream)), ms(stream?.durationMs), pair(ab?.linear?.afterBytes, stream?.linear?.afterBytes), readCell(stream), pair(gc(ab), gc(stream)), ] } /** One row per (file, engine): TS, Rust arrayBuffer, Rust stream. */ export function largeFilesTable(result) { const groups = new Map() for (const run of result.runs) { const key = `${labelOf(run)} ${run.engine}` const entry = groups.get(key) ?? {} entry[run.readPath] = run groups.set(key, entry) } const tsOf = (r) => result.tsBaseline.rows.find( (t) => t.engine === r.engine && t.file === r.file, ) const header = [ 'File / class', 'MiB', 'Engine', 'Outcome', 'TS growth summed / content MiB', 'TS ms', 'Rust AB growth summed / content MiB', 'AB ms', 'Rust stream growth summed / content MiB', 'Stream ms', 'Linear MiB AB / stream', 'Stream read', 'Chromium GC delta MiB AB / stream', ] const rows = [...groups.values()].map((byPath) => { const any = byPath.stream ?? byPath.arrayBuffer return largeRow(byPath, any.file ? tsOf(any) : null) }) return table(header, rows) } export function largeFilesSummary(result) { return [ ...headLines(result), `TS baseline: ${result.tsBaseline.file ?? 'none'}. Growth = peak minus ` + 'the level before the file is set (both sides); ms = Node clock, ' + 'file set to result (TS: to the first alert, error or navigation).', 'Large files (TS vs Rust arrayBuffer vs Rust stream):', largeFilesTable(result), ].join('\n\n') } const list = (values) => values.map(mb).join(', ') function leakRow(r) { if (r.outcome !== 'series') { return [r.engine, r.class, String(r.rep), r.outcome, r.error ?? '–'].concat( Array(8).fill('–'), ) } const d = r.deltas.content return [ r.engine, r.class, String(r.rep), r.reading, mb(r.levels.content[0]), mb(d.init), list(d.perImport), mb(d.last5), mb(d.finalIdle), mb(d.last5AtFinal), d.last5 <= TOLERANCE_BYTES && d.last5AtFinal <= TOLERANCE_BYTES ? 'yes' : 'no', mb(r.peakContentBytes), outcomesCell(r.outcomes), ] } /** "7 × ok" or the distinct outcomes with their counts. */ function outcomesCell(outcomes) { const counts = new Map() for (const o of outcomes) counts.set(o, (counts.get(o) ?? 0) + 1) return [...counts].map(([o, n]) => `${n} × ${o}`).join('; ') } /** Content RSS after each import of one page: init apart, then 6 more. */ export function leakTable(runs) { const header = [ 'Engine', 'Document', 'Rep', 'Reading', 'Start content MiB', 'Init Δ MiB', 'Δ per import after init MiB', 'Last 5 imports MiB', 'Final 5 s idle Δ MiB', 'Last 5 at final MiB', '≤ 10 MiB', 'Series peak content MiB', 'Outcomes', ] return table(header, runs.map(leakRow)) } function eventsCell(r) { if (!r.workerEvents?.length) return 'none' return r.workerEvents .map((e) => `#${e.import} ${e.kind} (${e.phase}): ${e.text}`) .join('; ') } export function webkitRepeatTable(runs) { const header = [ 'Engine', 'Document', 'Imports', 'Outcomes', 'Worker error / messageerror events', 'Content after import MiB first / min / max / last', 'Series peak content MiB', 'Page ms median (min–max)', ] const rows = runs.map((r) => { const c = r.contentAfter ?? {} const p = spread(r.pageMs ?? []) ?? {} return [ r.engine, r.class, String(r.imports ?? '–'), r.outcomes ? outcomesCell(r.outcomes) : r.outcome, eventsCell(r), [c.first, c.min, c.max, c.last].map(mb).join(' / '), mb(r.peakContentBytes), `${ms(p.median)} (${ms(p.min)}–${ms(p.max)})`, ] }) return table(header, rows) } export function leakSummary(result) { const parts = [...headLines(result), result.readings] if (result.leak.runs.length) { parts.push('Leak series (content RSS):', leakTable(result.leak.runs)) } if (result.webkitRepeat.runs.length) { parts.push( 'WebKit repetition, one page:', webkitRepeatTable(result.webkitRepeat.runs), ) } return parts.join('\n\n') }