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

+7
View File
@@ -3,6 +3,7 @@ import {
mkdtempSync,
readFileSync,
rmdirSync,
statSync,
unlinkSync,
writeFileSync,
} from 'node:fs'
@@ -75,6 +76,12 @@ export function importInputs(groups) {
return { inputs, hostile }
}
/** The bytes an input stands for: its size, or its description's length. */
export function inputBytes(path) {
if (!isGenerator(path)) return statSync(path).size
return JSON.parse(readFileSync(path, 'utf8')).length
}
/** A generator description becomes a temporary file, removed after the run. */
export function materialise(input, scratch) {
if (!isGenerator(input.path)) {
+221
View File
@@ -0,0 +1,221 @@
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')
}
+372
View File
@@ -0,0 +1,372 @@
import { execFileSync } from 'node:child_process'
import { mkdirSync, writeFileSync } from 'node:fs'
import { dirname } from 'node:path'
import {
delay,
firstLine,
tagged,
timeoutAfter,
untilStopped,
} from './async.mjs'
import { draftBundle, moduleFacts, snapshotBuilds } from './core-module.mjs'
import { writeHostileClasses } from './hostile-classes.mjs'
import { hostFacts } from './host.mjs'
import { materialise, scratchDir } from './import-inputs.mjs'
import { REPO_ROOT } from './paths.mjs'
import { closeEngine, launchEngine } from './playwright.mjs'
import { resultsJson } from './results-json.mjs'
import { sum } from './stats.mjs'
import { watchdogFacts } from './watchdog.mjs'
import { R4_MOUNTS } from '../serve.mjs'
/**
* The run machinery of the R4 measurement page /r4/, shared by
* r4-import.mjs, r4-large-files.mjs and r4-leak-series.mjs: the prepared
* module copies and hostile files, one page per context, one import raced
* against the page timeout and the watchdog, the settled levels (forced GC
* in Chromium, idle elsewhere), the flat run row, the engine loop that
* relaunches a browser the watchdog stopped.
*/
// The page's own timers are 10 s (start) and 120 s (task); this one only
// catches a page that stopped answering.
export const NODE_TIMEOUT_MS = 140_000
export const GC_SETTLE_MS = 1000
export const IDLE_SETTLE_MS = 5000
const TEN_MIB = 10 * 1024 * 1024
export const today = () => new Date().toISOString().slice(0, 10)
export const log = (line) =>
console.error(`[${new Date().toISOString()}] ${line}`)
export const listOf = (text) => (text ? text.split(',').filter(Boolean) : null)
/** Forced GC through CDP (Chromium tabs only), then `settleMs`. */
export async function forcedGc(tab, settleMs = GC_SETTLE_MS) {
await tab.cdp.send('HeapProfiler.collectGarbage')
await delay(settleMs)
}
/** The watchdog's current summed and content RSS. */
export function levelNow(session) {
const snapshot = session.watchdog.snapshot()
return {
rssBytes: snapshot.currentBytes,
rssContentBytes: snapshot.currentContentBytes,
}
}
/** Settled level: forced GC + 1 s (Chromium), 5 s idle, or none. */
export async function settledLevel(session, tab, mode) {
if (mode === 'none') return null
if (mode === 'gc') await forcedGc(tab)
else await delay(IDLE_SETTLE_MS)
return levelNow(session)
}
export 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 (when `item.path`), then imports. */
export async function drive(tab, item) {
const { page } = tab
if (item.path) await page.setInputFiles('#file', item.path)
if (item.section !== 'proofs') {
return page.evaluate(
(readPath) =>
window.r4Import(window.r4Page.file, window.r4Bundle, 'auto', readPath),
item.readPath ?? null,
)
}
return page.evaluate(
(name) => window.r4Proof(name, window.r4Bundle, window.r4Page.file),
item.proof,
)
}
/** One import raced against the Node timeout and the watchdog. */
export function raceDrive(session, tab, item) {
const race = Promise.race([
tagged('done', drive(tab, item)),
timeoutAfter(NODE_TIMEOUT_MS),
])
return untilStopped(session, race)
}
/** 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,
}
}
export function compact(object) {
const kept = Object.entries(object).filter(
([, v]) =>
v !== null && v !== undefined && !(Array.isArray(v) && !v.length),
)
return Object.fromEntries(kept)
}
/** The page result, flat; nulls dropped to keep the results file small. */
export function resultFields(result) {
const diag = result.diag ?? {}
const fields = {
route: result.route,
readPath: result.readPath,
readVia: diag.readVia,
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,
chunks: diag.chunks,
maxChunkBytes: diag.maxChunkBytes,
linear: {
readyBytes: result.readyLinearBytes,
beforeRunBytes: diag.linearBeforeRun,
afterBytes: diag.linearBytes,
},
transfer: result.transfer,
via: result.via,
cloneError: result.cloneError,
workerCsp: result.workerCsp,
workerEvents: result.workerEvents,
}
return compact(fields)
}
function rssOf(before, peak) {
return {
rssBeforeBytes: before.currentBytes,
rssBeforeContentBytes: before.currentContentBytes,
rssPeakBytes: peak.peakBytes,
rssPeakContentBytes: peak.peakContentBytes,
}
}
/** One import: settled before, RSS before and peak, result, settled after. */
export 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 end = await raceDrive(session, tab, item)
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. */
export function pageOutcome(r) {
const head = r.kind ? `${r.outcome} ${r.kind}` : r.outcome
return r.outcome === 'failed' ? `${head} (${r.phase}: ${r.error})` : head
}
/** A fresh context and page for one run, its temporary file removed after. */
export async function oneRun(session, item, context, measureOf = measure) {
const file = item.input ? materialise(item.input, context.scratch) : null
const run = { ...item, path: file?.path ?? item.path }
let tab = null
try {
tab = await openPage(session, context.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). */
export 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,
readPath: item.readPath,
})
}
/**
* The plan's items, one browser for the engine, relaunched after a stop by
* the watchdog; `runItem(session, item)` measures one item.
*/
export async function engineRuns(pw, engine, plan, runItem) {
let session = await launchEngine(pw, engine)
const runs = []
try {
for (const item of plan) {
const run = await runItem(session, item)
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)
const path = item.readPath ? ` [${item.readPath}]` : ''
log(
`${engine} ${label}${path}: ${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
}
export function headCommit() {
try {
const args = ['-C', REPO_ROOT, 'rev-parse', '--short', 'HEAD']
return execFileSync('git', args, { encoding: 'utf8' }).trim()
} catch {
return null
}
}
/** The common head of an R4 page results file. */
export function runConditions({ harness, server, playwrightCore, module }) {
return {
harness,
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)',
},
playwrightCore,
}
}
/** Writes the results file first, then prints `summarize(result)`. */
export function writeResult(out, result, summarize) {
mkdirSync(dirname(out), { recursive: true })
writeFileSync(out, `${resultsJson(result)}\n`)
log(`results written to ${out}`)
console.log(summarize(result))
}
/**
* The bundle, the served copies of pkg/ and pkg-proofs/ (a rebuild during
* the run cannot mix two modules), their facts, the hostile class files
* (`classes`: ids, null = all, false = none) and the server mounts;
* `cleanup()` removes every file the run wrote.
*/
export async function prepareModuleRun({ scratchParent, classes }) {
const bundle = draftBundle()
const scratch = scratchDir(scratchParent)
const builds = snapshotBuilds(scratch.dir)
if (!builds.pkg) {
builds.remove()
scratch.remove()
throw new Error('packages/core-wasm/pkg is not built')
}
const written =
classes === false
? { classes: [], remove: () => undefined }
: await writeHostileClasses({
bundle,
dir: scratch.dir,
pkgDir: builds.pkg,
wanted: classes,
})
const cleanup = () => {
written.remove()
builds.remove()
scratch.remove()
}
const mounts = {
...R4_MOUNTS,
'/pkg/': builds.pkg,
'/pkg-proofs/': builds.proofs ?? R4_MOUNTS['/pkg-proofs/'],
}
const module = moduleFacts(builds)
const base = { bundle, scratch: scratch.dir, module, mounts, cleanup }
return { ...base, classes: written.classes }
}
+1 -1
View File
@@ -234,7 +234,7 @@ export function w2Line(runs) {
)
}
function headLines(result) {
export function headLines(result) {
const engines = Object.entries(result.engines)
.map(([e, v]) => `${e} ${v}`)
.join(', ')