// Rust import in real engines (R4.2e, plan §B.1.5, §B.3, W2, W4): the // measurement page /r4/ runs each import in a module worker created for it // (the compiled Module posted by structured clone, the File read in the // worker, `SheetHandle.import_pdf` or the strict decode then `draft.restore`, // `terminate()` afterwards). Four sections, engine after engine, a fresh // context per run, the browser under the baseline's 4 GiB RSS watchdog: // import the TypeScript import baseline's files and groups, measured as // import-baseline.mjs measures them (RSS before, peak; durations); // hostile the classes of packages/core-wasm/tests/pdf-import.budget.test.ts // (L and CAP envelopes, 16 MiB dense values, depth 64, the rescue // copy, inflation, bombs, the 50 MiB file), built in Node; // repeat the L envelope and the 16 MiB dense document imported 3 times in // one page, the settled level after each (one-time residue or // growth per import); // proofs on the proofs build: the 1 GiB linear ceiling traps, and the // import with the OD-3 cap off on the 16 MiB dense document. // Chromium also gets the settled level before and after each run (CDP // HeapProfiler.collectGarbage + 1 s): the content process must come back // within 10 MiB once the worker is terminated (recorded, never a failure); // Firefox and WebKit get 5 s idle instead, outside the import section. // // PLAYWRIGHT_NODE_MODULES=... node scripts/browser/r4-import.mjs \ // [--engines chromium,firefox,webkit] \ // [--sections import,hostile,repeat,proofs] \ // [--groups base,r3,step6,tolerance,read] [--classes ] \ // [--scratch ] [--out ] import { execFileSync } from 'node:child_process' import { mkdirSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { dirname, join } from 'node:path' import { parseArgs } from 'node:util' import { delay, firstLine, tagged, timeoutAfter, untilStopped, } from './lib/async.mjs' import { draftBundle, moduleFacts, snapshotBuilds } from './lib/core-module.mjs' import { writeHostileClasses } from './lib/hostile-classes.mjs' import { hostFacts, memAvailableBytes } from './lib/host.mjs' import { importInputs, materialise, scratchDir } from './lib/import-inputs.mjs' import { REPO_ROOT, RESULTS_DIR, relativeToRepo } from './lib/paths.mjs' import { ENGINES, closeEngine, launchEngine, loadPlaywright, } from './lib/playwright.mjs' import { r4Summary } from './lib/r4-summary.mjs' import { resultsJson } from './lib/results-json.mjs' import { sum } from './lib/stats.mjs' import { watchdogFacts } from './lib/watchdog.mjs' import { R4_MOUNTS, startServer } from './serve.mjs' const SECTIONS = ['import', 'hostile', 'repeat', 'proofs'] // The page's own timers are 10 s (start) and 120 s (task); this one only // catches a page that stopped answering. const NODE_TIMEOUT_MS = 140_000 const GC_SETTLE_MS = 1000 const IDLE_SETTLE_MS = 5000 const TEN_MIB = 10 * 1024 * 1024 const CAP_OFF_INPUT = 'dense-numbers-16mib' const REPEAT_CLASSES = ['L-envelope', 'dense-numbers-16mib'] const REPEATS = 3 const today = () => new Date().toISOString().slice(0, 10) const log = (line) => console.error(`[${new Date().toISOString()}] ${line}`) const listOf = (text) => (text ? text.split(',').filter(Boolean) : null) function options() { const { values } = parseArgs({ options: { engines: { type: 'string', default: ENGINES.join(',') }, sections: { type: 'string', default: SECTIONS.join(',') }, groups: { type: 'string' }, classes: { type: 'string' }, scratch: { type: 'string', default: tmpdir() }, out: { type: 'string', default: join(RESULTS_DIR, `${today()}-r4-import.json`), }, }, }) const engines = listOf(values.engines) const sections = listOf(values.sections) const bad = [...engines.filter((e) => !ENGINES.includes(e))] bad.push(...sections.filter((s) => !SECTIONS.includes(s))) if (bad.length) throw new Error(`unknown engines or sections: ${bad}`) const groups = listOf(values.groups) const classes = listOf(values.classes) return { ...values, engines, sections, groups, classes } } /** Settled level: forced GC + 1 s (Chromium), 5 s idle, or none. */ async function settledLevel(session, tab, mode) { if (mode === 'none') return null if (mode === 'gc') { await tab.cdp.send('HeapProfiler.collectGarbage') await delay(GC_SETTLE_MS) } else { await delay(IDLE_SETTLE_MS) } const snapshot = session.watchdog.snapshot() return { rssBytes: snapshot.currentBytes, rssContentBytes: snapshot.currentContentBytes, } } async function openPage(session, baseUrl, bundle) { const context = await session.browser.newContext() const page = await context.newPage() const events = { pageErrors: [], consoleErrors: [] } page.on('pageerror', (e) => events.pageErrors.push(firstLine(e))) page.on('console', (m) => { if (m.type() === 'error') events.consoleErrors.push(m.text().slice(0, 300)) }) const chromium = session.engine === 'chromium' const cdp = chromium ? await context.newCDPSession(page) : null await page.goto(`${baseUrl}/r4/`) await page.waitForFunction(() => window.r4Ready !== undefined) const compile = await page.evaluate((text) => { window.r4Bundle = text return window.r4Ready.facts }, bundle) return { context, page, events, cdp, compile } } /** Sets the file on the page's input, then runs the import or the proof. */ async function drive(tab, item) { const { page } = tab if (item.path) await page.setInputFiles('#file', item.path) if (item.section !== 'proofs') { return page.evaluate(() => window.r4Import(window.r4Page.file, window.r4Bundle, 'auto'), ) } return page.evaluate( (name) => window.r4Proof(name, window.r4Bundle, window.r4Page.file), item.proof, ) } /** The page's main-thread facts since `since` (page clock). */ async function pageFacts(tab, since) { const page = await tab.page.evaluate(() => ({ csp: window.r4Page.csp, longTaskSupported: window.r4Page.longTaskSupported, longTasks: window.r4Page.longTasks, })) if (!page.longTaskSupported) return { csp: page.csp, longTasks: null } const tasks = page.longTasks.filter((t) => t.start >= since).map((t) => t.ms) const longTasks = { count: tasks.length, maxMs: tasks.length ? Math.max(...tasks) : 0, totalMs: sum(tasks), } return { csp: page.csp, longTasks } } /** The settled levels and the 10 MiB check of plan §B.3. */ function settledDelta(mode, before, after) { if (!before || !after) return null const content = after.rssContentBytes - before.rssContentBytes return { mode, before, after, summedDeltaBytes: after.rssBytes - before.rssBytes, contentDeltaBytes: content, contentWithin10MiB: content <= TEN_MIB, } } /** The page result, flat; nulls dropped to keep the results file small. */ function resultFields(result) { const diag = result.diag ?? {} const fields = { route: result.route, outcome: result.outcome, resultKind: result.kind, detail: result.detail ?? result.error, phase: result.phase, summary: result.summary, proofValue: result.value, pageMs: result.pageMs, startMs: result.startMs, workerMs: result.workerMs, taskMs: diag.ms, readMs: diag.readMs, sheetMs: diag.sheetMs, runMs: diag.runMs, parseMs: result.parseMs, outChars: result.outChars, precheck: diag.precheck, linear: { readyBytes: result.readyLinearBytes, beforeRunBytes: diag.linearBeforeRun, afterBytes: diag.linearBytes, }, transfer: result.transfer, via: result.via, cloneError: result.cloneError, workerCsp: result.workerCsp, } return compact(fields) } function compact(object) { const kept = Object.entries(object).filter( ([, v]) => v !== null && v !== undefined && !(Array.isArray(v) && !v.length), ) return Object.fromEntries(kept) } function rssOf(before, peak) { return { rssBeforeBytes: before.currentBytes, rssBeforeContentBytes: before.currentContentBytes, rssPeakBytes: peak.peakBytes, rssPeakContentBytes: peak.peakContentBytes, } } async function measure(session, tab, item) { const settledBefore = await settledLevel(session, tab, item.settle) const since = await tab.page.evaluate(() => performance.now()) const before = session.watchdog.snapshot() await session.watchdog.resetPeak() const start = performance.now() const race = Promise.race([ tagged('done', drive(tab, item)), timeoutAfter(NODE_TIMEOUT_MS), ]) const end = await untilStopped(session, race) const snapshot = session.watchdog.snapshot() const head = { durationMs: end.at - start, ...rssOf(before, snapshot) } if (end.kind !== 'done') { return { outcome: end.kind, error: end.error ?? null, ...head } } const after = { rssAfterBytes: snapshot.currentBytes, rssAfterContentBytes: snapshot.currentContentBytes, } const settledAfter = await settledLevel(session, tab, item.settle) const facts = await pageFacts(tab, since) return { ...resultFields(end.value), ...head, ...after, settled: settledDelta(item.settle, settledBefore, settledAfter), compileMs: tab.compile.compileMs, compileError: tab.compile.error, longTasks: facts.longTasks, ...compact({ cspViolations: facts.csp, ...tab.events }), } } /** "outcome kind", plus the phase and error text of a worker failure. */ function pageOutcome(r) { const head = r.kind ? `${r.outcome} ${r.kind}` : r.outcome return r.outcome === 'failed' ? `${head} (${r.phase}: ${r.error})` : head } /** * One page, `item.repeat` imports of the same file, the settled level * before and after each: tells a one-time residue (code compiled into the * page's Module, allocator pools) from a growth per import. */ async function measureRepeat(session, tab, item) { const levels = [await settledLevel(session, tab, item.settle)] const outcomes = [] for (let i = 0; i < item.repeat; i += 1) { const race = Promise.race([ tagged('done', drive(tab, item)), timeoutAfter(NODE_TIMEOUT_MS), ]) const end = await untilStopped(session, race) if (end.kind !== 'done') { return { outcome: end.kind, error: end.error ?? null, outcomes } } outcomes.push(pageOutcome(end.value)) levels.push(await settledLevel(session, tab, item.settle)) } const content = levels.map((level) => level.rssContentBytes) return { outcome: 'repeated', outcomes, settleMode: item.settle, contentLevels: content, contentDeltas: content.slice(1).map((value, i) => value - content[i]), } } async function oneRun(session, baseUrl, item, context) { const file = item.input ? materialise(item.input, context.scratch) : null const run = { ...item, path: file?.path ?? item.path } const measureOf = item.section === 'repeat' ? measureRepeat : measure let tab = null try { tab = await openPage(session, baseUrl, context.bundle) return await measureOf(session, tab, run) } catch (error) { const stopped = session.watchdog.snapshot().stopped return { outcome: stopped ? 'stopped_by_ceiling' : 'error', error: firstLine(error), } } finally { await tab?.context.close().catch(() => undefined) file?.cleanup() } } /** What a run row keeps of its plan item (the file named as the baseline). */ function headOf(item) { return compact({ section: item.section, group: item.group, kind: item.kind, class: item.classId, proof: item.proof, repeat: item.repeat, file: item.file, fileBytes: item.fileBytes, }) } /** Chromium settles with a forced GC everywhere; the others on attacks. */ function settleMode(engine, section) { if (engine === 'chromium') return 'gc' return section === 'import' ? 'none' : 'idle' } async function engineRuns(pw, engine, context) { let session = await launchEngine(pw, engine) const runs = [] try { for (const planned of context.plan) { const item = { ...planned, settle: settleMode(engine, planned.section) } const run = await oneRun(session, context.baseUrl, item, context) const version = session.version runs.push({ engine, engineVersion: version, ...headOf(item), ...run }) const label = item.file ?? item.classId ?? item.proof const ms = Math.round(run.durationMs ?? 0) log(`${engine} ${label}: ${run.outcome} ${run.resultKind ?? ''} ${ms} ms`) if (run.outcome === 'watchdog_failed') throw new Error('watchdog failed') if (run.outcome !== 'stopped_by_ceiling') continue await closeEngine(session) session = await launchEngine(pw, engine) } } finally { await closeEngine(session) } return runs } const importItem = (input) => ({ section: 'import', group: input.group, kind: input.kind ?? null, file: relativeToRepo(input.path), input, }) const classItem = (section, c, extra = {}) => ({ section, classId: c.id, fileBytes: c.bytes, path: c.path, ...extra, }) function proofItems(classes, facts) { if (!facts.proofs) return { items: [], skipped: 'pkg-proofs not built' } const items = [{ section: 'proofs', proof: 'ceiling' }] const input = classes.find((c) => c.id === CAP_OFF_INPUT) if (!input) return { items, skipped: `cap_off: ${CAP_OFF_INPUT} not built` } items.push({ ...classItem('proofs', input), proof: 'cap_off' }) return { items, skipped: null } } /** The runs of every engine, in order: import, hostile, repeat, proofs. */ function planOf(sections, inputs, classes, facts) { const wanted = (section) => sections.includes(section) const proofs = wanted('proofs') ? proofItems(classes, facts) : { items: [], skipped: 'not asked' } const repeated = classes.filter((c) => REPEAT_CLASSES.includes(c.id)) const plan = [ ...(wanted('import') ? inputs.map(importItem) : []), ...(wanted('hostile') ? classes.map((c) => classItem('hostile', c)) : []), ...(wanted('repeat') ? repeated.map((c) => classItem('repeat', c, { repeat: REPEATS })) : []), ...proofs.items, ] return { plan, proofsSkipped: proofs.skipped } } function headCommit() { try { const args = ['-C', REPO_ROOT, 'rev-parse', '--short', 'HEAD'] return execFileSync('git', args, { encoding: 'utf8' }).trim() } catch { return null } } const emptyFacts = () => ({ clone: 0, fetch: 0, cloneErrors: [], started: 0, notStarted: 0, csp: [], }) const addOnce = (list, value) => value && !list.includes(value) && list.push(value) /** Per engine: Module clones, URL fallbacks, workers ready, CSP violations. */ function workerFacts(runs) { const facts = {} const measured = runs.filter( (r) => r.section === 'import' || r.section === 'hostile', ) for (const run of measured) { const f = (facts[run.engine] ??= emptyFacts()) if (run.transfer === 'clone') f.clone += 1 if (run.transfer === 'fetch') f.fetch += 1 addOnce(f.cloneErrors, run.cloneError) if (Number.isFinite(run.startMs)) f.started += 1 else f.notStarted += 1 } for (const run of runs) { const f = (facts[run.engine] ??= emptyFacts()) const violations = [...(run.cspViolations ?? []), ...(run.workerCsp ?? [])] for (const v of violations) addOnce(f.csp, v) } return facts } function conditions(server, playwrightCore, module) { return { harness: 'scripts/browser/r4-import.mjs', startedAt: new Date().toISOString(), watchdog: watchdogFacts(), host: hostFacts(), module: { ...module, headCommit: headCommit() }, server: { bind: '127.0.0.1', csp: server.csp, mounts: Object.keys(R4_MOUNTS), page: '/r4/ (scripts/browser/r4-import-page)', }, page: { worker: 'module worker /r4/worker.js, one per import, terminated', timers: { startMs: 10_000, taskMs: 120_000 }, limits: { pdfBytes: 50 * 1024 * 1024, copyBytes: 16 * 1024 * 1024 }, routing: 'HomeRoute: first KiB (%PDF-, JSON object, .json name)', }, perimeter: 'same files, groups, RSS metrics, watchdog and engines as ' + 'import-baseline.mjs; the page is the measurement page, not the ' + 'application: no hydrate, no navigation, the module compiled at ' + 'page load (compileMs) instead of during the import', playwrightCore, } } function writeResult(out, result) { mkdirSync(dirname(out), { recursive: true }) writeFileSync(out, `${resultsJson(result)}\n`) log(`results written to ${out}`) console.log(r4Summary(result)) } function sectionsOf(runs, prepared) { const of = (section) => runs.filter((r) => r.section === section) const { inputs, hostile, classes, proofsSkipped } = prepared return { import: { timeoutMs: 120_000, files: inputs.length, hostile: { listedBy: hostile.listedBy, present: hostile.inputs.length, missing: hostile.missing, }, runs: of('import'), }, hostileClasses: { source: 'packages/core-wasm/tests/pdf-import.budget.test.ts', classes: classes.map(({ id, bytes }) => ({ id, bytes })), runs: of('hostile'), }, repeat: { imports: REPEATS, runs: of('repeat') }, proofs: { skipped: proofsSkipped, runs: of('proofs') }, } } /** The served module copies, their facts, the hostile files on disk. */ async function prepareFiles(opts, bundle, scratch) { const builds = snapshotBuilds(scratch.dir) if (!builds.pkg) throw new Error('packages/core-wasm/pkg is not built') const needClasses = opts.sections.some((s) => s !== 'import') const written = needClasses ? await writeHostileClasses({ bundle, dir: scratch.dir, pkgDir: builds.pkg, wanted: opts.classes, }) : { classes: [], remove: () => undefined } const cleanup = () => { written.remove() builds.remove() scratch.remove() } return { builds, classes: written.classes, cleanup } } /** Module facts, the bundle, the inputs, the plan. */ async function prepare(opts) { const bundle = draftBundle() const scratch = scratchDir(opts.scratch) const { builds, classes, cleanup } = await prepareFiles(opts, bundle, scratch) const module = moduleFacts(builds) const selected = importInputs(opts.groups) const inputs = opts.sections.includes('import') ? selected.inputs : [] const planned = planOf(opts.sections, inputs, classes, module) const mounts = { ...R4_MOUNTS, '/pkg/': builds.pkg, '/pkg-proofs/': builds.proofs ?? R4_MOUNTS['/pkg-proofs/'], } const files = { inputs, hostile: selected.hostile, classes } const base = { module, bundle, mounts, scratch: scratch.dir, cleanup } return { ...base, ...files, ...planned } } async function main() { const opts = options() const { pw, version } = loadPlaywright() const prepared = await prepare(opts) const server = await startServer({ mounts: prepared.mounts }) try { const result = conditions(server, version, prepared.module) const context = { ...prepared, baseUrl: server.url } const runs = [] for (const engine of opts.engines) { runs.push(...(await engineRuns(pw, engine, context))) } result.host.memAvailableBytesAtEnd = memAvailableBytes() result.endedAt = new Date().toISOString() result.engines = Object.fromEntries( runs.map((r) => [r.engine, r.engineVersion]), ) result.workerFacts = workerFacts(runs) writeResult(opts.out, { ...result, ...sectionsOf(runs, prepared) }) } finally { server.close() prepared.cleanup() } } await main()