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.
This commit is contained in:
bchanot committed 2026-10-18 18:50:47 +02:00
1 parent 8924d50edf
commit 49591c53cd
14 files changed
+3002 -387

No files matched your search

+58 -336
View File
@@ -1,7 +1,9 @@
// 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`,
// 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
@@ -23,51 +25,35 @@
// [--engines chromium,firefox,webkit] \
// [--sections import,hostile,repeat,proofs] \
// [--groups base,r3,step6,tolerance,read] [--classes <id,...>] \
// [--scratch <dir>] [--out <file>]
import { execFileSync } from 'node:child_process'
import { mkdirSync, writeFileSync } from 'node:fs'
// [--read stream|arrayBuffer] [--scratch <dir>] [--out <file>]
import { tmpdir } from 'node:os'
import { dirname, join } from 'node:path'
import { 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 { 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 { 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'
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']
// 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 READ_PATHS = ['stream', 'arrayBuffer']
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: {
@@ -75,6 +61,7 @@ function options() {
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',
@@ -86,184 +73,13 @@ function options() {
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}`)
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 }
}
/** 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
@@ -273,11 +89,7 @@ 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)
const end = await raceDrive(session, tab, item)
if (end.kind !== 'done') {
return { outcome: end.kind, error: end.error ?? null, outcomes }
}
@@ -294,67 +106,26 @@ async function measureRepeat(session, tab, item) {
}
}
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)
/** 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)
}
return runs
}
const importItem = (input) => ({
@@ -400,15 +171,6 @@ function planOf(sections, inputs, classes, facts) {
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,
@@ -443,41 +205,25 @@ function workerFacts(runs) {
return facts
}
function conditions(server, playwrightCore, module) {
function conditions(server, playwrightCore, module, readPath) {
const harness = 'scripts/browser/r4-import.mjs'
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)',
},
...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',
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
@@ -502,44 +248,17 @@ function sectionsOf(runs, prepared) {
}
}
/** 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 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, 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 }
const planned = planOf(opts.sections, inputs, run.classes, run.module)
return { ...run, inputs, hostile: selected.hostile, ...planned }
}
async function main() {
@@ -548,11 +267,13 @@ async function main() {
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 { 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) {
runs.push(...(await engineRuns(pw, engine, context)))
const plan = enginePlan(engine, context)
runs.push(...(await engineRuns(pw, engine, plan, runItem(context))))
}
result.host.memAvailableBytesAtEnd = memAvailableBytes()
result.endedAt = new Date().toISOString()
@@ -560,7 +281,8 @@ async function main() {
runs.map((r) => [r.engine, r.engineVersion]),
)
result.workerFacts = workerFacts(runs)
writeResult(opts.out, { ...result, ...sectionsOf(runs, prepared) })
const full = { ...result, ...sectionsOf(runs, prepared) }
writeResult(opts.out, full, r4Summary)
} finally {
server.close()
prepared.cleanup()