Same perimeter as the TS baseline (226 files, 3 engines, real Home page): 666/678 outcome and message agreements, the 12 others the 4 declared files; W5 0 long tasks (TS 91 and 173 ms); 50 MiB file in WebKit 145/126 MiB against 324 with the worker read and 76 for the TS. CSP: editor opens with the keyword, the engine message without, in 3 engines, the worker's wasm-eval refusal reported by Chromium and Firefox. Leak series on the app: Chromium ≤ 2 MiB per import after init, Firefox GC sawtooth reclaimed, WebKit grows with same-tab navigations (control too) and levels off on one page; 20/20 WebKit workers closed.
337 lines
11 KiB
JavaScript
337 lines
11 KiB
JavaScript
// Leak series of the Rust import worker (R4.3a, BDR-058 leak criterion):
|
||
// leak per engine, per document (the L envelope, the 16 MiB dense
|
||
// numbers refused by the value budget, the 16 MiB rescue
|
||
// copy), `--reps` times: ONE page, the file set once, then
|
||
// one import (init cost, counted apart) and `--imports`
|
||
// more, the worker terminated after each, 2 s idle between
|
||
// them, 5 s idle at the end; the content RSS read after
|
||
// each. Chromium: a forced GC (CDP) + 1 s before each
|
||
// reading; Firefox and WebKit: idle only (no forced GC
|
||
// there, so their levels are indicative).
|
||
// Recurring growth = level after the last import minus the
|
||
// level after the first import past init (L7 − L2 for 6),
|
||
// tolerance 10 MiB.
|
||
// webkit-repeat WebKit, one page, `--webkit-imports` consecutive imports
|
||
// of the 16 MiB dense document (a worker per import), every
|
||
// worker `error` / `messageerror` event kept with its text.
|
||
// The browser runs under the 4 GiB RSS watchdog (not a guaranteed limit).
|
||
//
|
||
// PLAYWRIGHT_NODE_MODULES=... node scripts/browser/r4-leak-series.mjs \
|
||
// [--engines chromium,firefox,webkit] [--sections leak,webkit-repeat] \
|
||
// [--reps 3] [--imports 6] [--webkit-imports 20] \
|
||
// [--read stream|arrayBuffer] [--scratch <dir>] [--out <file>]
|
||
import { tmpdir } from 'node:os'
|
||
import { join } from 'node:path'
|
||
import { pathToFileURL } from 'node:url'
|
||
import { parseArgs } from 'node:util'
|
||
import { delay } from './lib/async.mjs'
|
||
import { memAvailableBytes } from './lib/host.mjs'
|
||
import { RESULTS_DIR } from './lib/paths.mjs'
|
||
import { ENGINES, loadPlaywright } from './lib/playwright.mjs'
|
||
import { leakSummary } from './lib/r4-memory-summary.mjs'
|
||
import {
|
||
IDLE_SETTLE_MS,
|
||
engineRuns,
|
||
forcedGc,
|
||
levelNow,
|
||
listOf,
|
||
oneRun,
|
||
pageOutcome,
|
||
prepareModuleRun,
|
||
raceDrive,
|
||
runConditions,
|
||
today,
|
||
writeResult,
|
||
} from './lib/r4-runs.mjs'
|
||
import { startServer } from './serve.mjs'
|
||
|
||
const SECTIONS = ['leak', 'webkit-repeat']
|
||
const READ_PATHS = ['stream', 'arrayBuffer']
|
||
const DOCUMENTS = [
|
||
'L-envelope',
|
||
'dense-numbers-16mib',
|
||
'dense-numbers-copy-16mib',
|
||
]
|
||
const WEBKIT_DOCUMENT = 'dense-numbers-16mib'
|
||
const BETWEEN_MS = 2000
|
||
const FINAL_IDLE_MS = 5000
|
||
const READINGS =
|
||
'Readings: content RSS of the browser tree after each import, 2 s idle ' +
|
||
'after the worker is terminated; Chromium adds a forced GC (CDP ' +
|
||
'HeapProfiler.collectGarbage) + 1 s before each reading, Firefox and ' +
|
||
'WebKit are read after idle only (no forced GC: indicative). Start = ' +
|
||
'after the file is set (GC + 1 s, or 5 s idle); final = after 5 s more ' +
|
||
'idle. Init Δ = L1 − L0; recurring = L7 − L2 (the last 5 imports).'
|
||
|
||
const count = (text, name) => {
|
||
const value = Number(text)
|
||
if (!Number.isInteger(value) || value < 1) throw new Error(`bad --${name}`)
|
||
return value
|
||
}
|
||
|
||
function options() {
|
||
const { values } = parseArgs({
|
||
options: {
|
||
engines: { type: 'string', default: ENGINES.join(',') },
|
||
sections: { type: 'string', default: SECTIONS.join(',') },
|
||
reps: { type: 'string', default: '3' },
|
||
imports: { type: 'string', default: '6' },
|
||
'webkit-imports': { type: 'string', default: '20' },
|
||
read: { type: 'string', default: 'stream' },
|
||
scratch: { type: 'string', default: tmpdir() },
|
||
out: {
|
||
type: 'string',
|
||
default: join(RESULTS_DIR, `${today()}-r4-leak-series.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}`)
|
||
return {
|
||
...values,
|
||
engines,
|
||
sections,
|
||
reps: count(values.reps, 'reps'),
|
||
imports: count(values.imports, 'imports'),
|
||
webkitImports: count(values['webkit-imports'], 'webkit-imports'),
|
||
}
|
||
}
|
||
|
||
/** One reading: forced GC + 1 s first in Chromium, the level as it is else. */
|
||
async function reading(session, tab) {
|
||
if (tab.cdp) await forcedGc(tab)
|
||
return levelNow(session)
|
||
}
|
||
|
||
/** The start level: GC + 1 s (Chromium) or 5 s idle, the file already set. */
|
||
async function startLevel(session, tab, item) {
|
||
await tab.page.setInputFiles('#file', item.path)
|
||
if (!tab.cdp) await delay(IDLE_SETTLE_MS)
|
||
return reading(session, tab)
|
||
}
|
||
|
||
/** Init, per-import, recurring and final deltas of one level list. */
|
||
export function deltasOf(levels, imports) {
|
||
const last = imports + 1
|
||
const at = (i) => levels[i]
|
||
const perImport = []
|
||
for (let i = 2; i <= last; i += 1) perImport.push(at(i) - at(i - 1))
|
||
return {
|
||
init: at(1) - at(0),
|
||
perImport,
|
||
last5: at(last) - at(Math.max(2, last - 5)),
|
||
finalIdle: at(last + 1) - at(last),
|
||
last5AtFinal: at(last + 1) - at(Math.max(2, last - 5)),
|
||
}
|
||
}
|
||
|
||
/** A series cut short: the harness end (timeout, stop) and what ran. */
|
||
const stopped = (item, end, outcomes) => ({
|
||
rep: item.rep,
|
||
outcome: end.kind,
|
||
error: end.error ?? null,
|
||
outcomes,
|
||
})
|
||
|
||
/** One import of the file already set, its row facts. */
|
||
async function importOnce(session, tab, item) {
|
||
const end = await raceDrive(session, tab, { ...item, path: null })
|
||
if (end.kind !== 'done') return { end }
|
||
const r = end.value
|
||
const facts = {
|
||
outcome: pageOutcome(r),
|
||
pageMs: r.pageMs,
|
||
events: r.workerEvents ?? [],
|
||
}
|
||
return { end, facts }
|
||
}
|
||
|
||
/**
|
||
* ONE page: the start level, the init import, `item.imports` more, each
|
||
* read after 2 s idle (+ GC in Chromium), then 5 s idle and a last reading.
|
||
*/
|
||
async function measureSeries(session, tab, item) {
|
||
const levels = [await startLevel(session, tab, item)]
|
||
await session.watchdog.resetPeak()
|
||
const outcomes = []
|
||
const pageMs = []
|
||
const workerEvents = []
|
||
for (let i = 0; i <= item.imports; i += 1) {
|
||
const { end, facts } = await importOnce(session, tab, item)
|
||
if (!facts) return stopped(item, end, outcomes)
|
||
outcomes.push(facts.outcome)
|
||
pageMs.push(facts.pageMs)
|
||
workerEvents.push(...facts.events.map((e) => ({ import: i, ...e })))
|
||
await delay(BETWEEN_MS)
|
||
levels.push(await reading(session, tab))
|
||
}
|
||
await delay(FINAL_IDLE_MS)
|
||
levels.push(await reading(session, tab))
|
||
return seriesRow(session, item, { levels, outcomes, pageMs, workerEvents })
|
||
}
|
||
|
||
function seriesRow(session, item, facts) {
|
||
const peak = session.watchdog.snapshot()
|
||
const content = facts.levels.map((l) => l.rssContentBytes)
|
||
const summed = facts.levels.map((l) => l.rssBytes)
|
||
return {
|
||
rep: item.rep,
|
||
outcome: 'series',
|
||
reading: item.reading,
|
||
outcomes: facts.outcomes,
|
||
pageMs: facts.pageMs,
|
||
workerEvents: facts.workerEvents,
|
||
levels: { content, summed },
|
||
deltas: {
|
||
content: deltasOf(content, item.imports),
|
||
summed: deltasOf(summed, item.imports),
|
||
},
|
||
peakBytes: peak.peakBytes,
|
||
peakContentBytes: peak.peakContentBytes,
|
||
}
|
||
}
|
||
|
||
/** WebKit: `item.imports` consecutive imports in one page, no idle. */
|
||
async function measureRepetition(session, tab, item) {
|
||
await tab.page.setInputFiles('#file', item.path)
|
||
await session.watchdog.resetPeak()
|
||
const outcomes = []
|
||
const pageMs = []
|
||
const workerEvents = []
|
||
const content = []
|
||
for (let i = 0; i < item.imports; i += 1) {
|
||
const { end, facts } = await importOnce(session, tab, item)
|
||
if (!facts) return stopped(item, end, outcomes)
|
||
outcomes.push(facts.outcome)
|
||
pageMs.push(facts.pageMs)
|
||
workerEvents.push(...facts.events.map((e) => ({ import: i, ...e })))
|
||
content.push(levelNow(session).rssContentBytes)
|
||
}
|
||
return repetitionRow(session, item, {
|
||
outcomes,
|
||
pageMs,
|
||
workerEvents,
|
||
content,
|
||
})
|
||
}
|
||
|
||
function repetitionRow(session, item, facts) {
|
||
const peak = session.watchdog.snapshot()
|
||
const { content } = facts
|
||
return {
|
||
outcome: 'repeated',
|
||
imports: item.imports,
|
||
outcomes: facts.outcomes,
|
||
pageMs: facts.pageMs,
|
||
workerEvents: facts.workerEvents,
|
||
contentAfter: {
|
||
first: content[0],
|
||
min: Math.min(...content),
|
||
max: Math.max(...content),
|
||
last: content[content.length - 1],
|
||
},
|
||
peakBytes: peak.peakBytes,
|
||
peakContentBytes: peak.peakContentBytes,
|
||
}
|
||
}
|
||
|
||
const classOf = (classes, id) => classes.find((c) => c.id === id)
|
||
|
||
function leakItems(engine, opts, classes) {
|
||
const reading = engine === 'chromium' ? 'gc + 1 s' : 'idle only'
|
||
return DOCUMENTS.flatMap((id) => {
|
||
const c = classOf(classes, id)
|
||
return Array.from({ length: opts.reps }, (_, i) => ({
|
||
section: 'leak',
|
||
classId: id,
|
||
fileBytes: c.bytes,
|
||
path: c.path,
|
||
readPath: opts.read,
|
||
imports: opts.imports,
|
||
rep: i + 1,
|
||
reading,
|
||
}))
|
||
})
|
||
}
|
||
|
||
function repetitionItem(opts, classes) {
|
||
const c = classOf(classes, WEBKIT_DOCUMENT)
|
||
return {
|
||
section: 'webkit-repeat',
|
||
classId: c.id,
|
||
fileBytes: c.bytes,
|
||
path: c.path,
|
||
readPath: opts.read,
|
||
imports: opts.webkitImports,
|
||
}
|
||
}
|
||
|
||
function enginePlan(engine, opts, classes) {
|
||
const plan = opts.sections.includes('leak')
|
||
? leakItems(engine, opts, classes)
|
||
: []
|
||
if (engine === 'webkit' && opts.sections.includes('webkit-repeat')) {
|
||
plan.push(repetitionItem(opts, classes))
|
||
}
|
||
return plan
|
||
}
|
||
|
||
function conditions(server, playwrightCore, module, opts) {
|
||
const harness = 'scripts/browser/r4-leak-series.mjs'
|
||
return {
|
||
...runConditions({ harness, server, playwrightCore, module }),
|
||
page: {
|
||
worker: 'module worker /r4/worker.js, one per import, terminated',
|
||
readPath: opts.read,
|
||
imports: { init: 1, then: opts.imports, reps: opts.reps },
|
||
betweenMs: BETWEEN_MS,
|
||
finalIdleMs: FINAL_IDLE_MS,
|
||
webkitImports: opts.webkitImports,
|
||
},
|
||
readings: READINGS,
|
||
}
|
||
}
|
||
|
||
async function main() {
|
||
const opts = options()
|
||
const { pw, version } = loadPlaywright()
|
||
const prepared = await prepareModuleRun({
|
||
scratchParent: opts.scratch,
|
||
classes: [...new Set([...DOCUMENTS, WEBKIT_DOCUMENT])],
|
||
})
|
||
const server = await startServer({ mounts: prepared.mounts })
|
||
try {
|
||
const result = conditions(server, version, prepared.module, opts)
|
||
const context = { ...prepared, baseUrl: server.url }
|
||
const runItem = (session, item) => {
|
||
const measureOf =
|
||
item.section === 'leak' ? measureSeries : measureRepetition
|
||
return oneRun(session, item, context, measureOf)
|
||
}
|
||
const runs = []
|
||
for (const engine of opts.engines) {
|
||
const plan = enginePlan(engine, opts, prepared.classes)
|
||
runs.push(...(await engineRuns(pw, engine, plan, runItem)))
|
||
}
|
||
result.host.memAvailableBytesAtEnd = memAvailableBytes()
|
||
result.endedAt = new Date().toISOString()
|
||
result.engines = Object.fromEntries(
|
||
runs.map((r) => [r.engine, r.engineVersion]),
|
||
)
|
||
result.leak = { runs: runs.filter((r) => r.section === 'leak') }
|
||
result.webkitRepeat = {
|
||
runs: runs.filter((r) => r.section === 'webkit-repeat'),
|
||
}
|
||
writeResult(opts.out, result, leakSummary)
|
||
} finally {
|
||
server.close()
|
||
prepared.cleanup()
|
||
}
|
||
}
|
||
|
||
if (import.meta.url === pathToFileURL(process.argv[1]).href) await main()
|