Files
faunosteo/scripts/browser/lib/r4-runs.mjs
T
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

373 lines
12 KiB
JavaScript

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 }
}