Files
bchanot 49591c53cd test(tests): R4.3a browser re-measure of large files, leak series, WebKit repetition (stream sink, BYOB)
Streaming into FileBytes removes the file's JavaScript copy in Chromium and Firefox
(50 MiB: 65 and 67 MiB summed growth against 56 and 79 for the TypeScript); WebKit keeps
about 180 MiB for any file read inside a worker (272 against 76), a main-thread BYOB read
feeding the worker's sink measured at 106. Corpus 666/678 on the stream path. Leak series
(3 engines × 3 documents × 3 repetitions): Chromium ≤ 0.5 MiB over the last 5, Firefox
within ±3 (idle only), WebKit copy +13 then level (12-import series −2.3). 20 dense
imports in WebKit: no worker error event.
2026-10-18 18:50:47 +02:00

293 lines
10 KiB
JavaScript

// 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, streamed into a `FileBytes` sink then `import_pdf_bytes` or
// `restore_copy_bytes` (R4.3a; `--read arrayBuffer`: the R4.2e path, one
// ArrayBuffer, `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 <id,...>] \
// [--read stream|arrayBuffer] [--scratch <dir>] [--out <file>]
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { parseArgs } from 'node:util'
import { memAvailableBytes } from './lib/host.mjs'
import { importInputs } from './lib/import-inputs.mjs'
import { RESULTS_DIR, relativeToRepo } from './lib/paths.mjs'
import { ENGINES, loadPlaywright } from './lib/playwright.mjs'
import { r4Summary } from './lib/r4-summary.mjs'
import {
engineRuns,
listOf,
oneRun,
pageOutcome,
prepareModuleRun,
raceDrive,
runConditions,
settledLevel,
today,
writeResult,
} from './lib/r4-runs.mjs'
import { startServer } from './serve.mjs'
const SECTIONS = ['import', 'hostile', 'repeat', 'proofs']
const READ_PATHS = ['stream', 'arrayBuffer']
const CAP_OFF_INPUT = 'dense-numbers-16mib'
const REPEAT_CLASSES = ['L-envelope', 'dense-numbers-16mib']
const REPEATS = 3
function options() {
const { values } = parseArgs({
options: {
engines: { type: 'string', default: ENGINES.join(',') },
sections: { type: 'string', default: SECTIONS.join(',') },
groups: { type: 'string' },
classes: { type: 'string' },
read: { type: 'string', default: 'stream' },
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 (!READ_PATHS.includes(values.read)) bad.push(values.read)
if (bad.length) throw new Error(`unknown engines, sections or read: ${bad}`)
const groups = listOf(values.groups)
const classes = listOf(values.classes)
return { ...values, engines, sections, groups, classes }
}
/**
* 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 end = await raceDrive(session, tab, item)
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]),
}
}
/** 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'
}
/** The plan for one engine: its settle mode and the run's read path. */
function enginePlan(engine, context) {
return context.plan.map((planned) => ({
...planned,
readPath: context.readPath,
settle: settleMode(engine, planned.section),
}))
}
function runItem(context) {
return (session, item) => {
const measureOf = item.section === 'repeat' ? measureRepeat : undefined
return oneRun(session, item, context, measureOf)
}
}
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 }
}
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, readPath) {
const harness = 'scripts/browser/r4-import.mjs'
return {
...runConditions({ harness, server, playwrightCore, module }),
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)',
readPath,
},
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',
}
}
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') },
}
}
/** Module facts, the bundle, the inputs, the plan. */
async function prepare(opts) {
const needClasses = opts.sections.some((s) => s !== 'import')
const run = await prepareModuleRun({
scratchParent: opts.scratch,
classes: needClasses ? opts.classes : false,
})
const selected = importInputs(opts.groups)
const inputs = opts.sections.includes('import') ? selected.inputs : []
const planned = planOf(opts.sections, inputs, run.classes, run.module)
return { ...run, inputs, hostile: selected.hostile, ...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 { module } = prepared
const result = conditions(server, version, module, opts.read)
const context = { ...prepared, baseUrl: server.url, readPath: opts.read }
const runs = []
for (const engine of opts.engines) {
const plan = enginePlan(engine, context)
runs.push(...(await engineRuns(pw, engine, plan, runItem(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)
const full = { ...result, ...sectionsOf(runs, prepared) }
writeResult(opts.out, full, r4Summary)
} finally {
server.close()
prepared.cleanup()
}
}
await main()