Files
faunosteo/scripts/browser/r4-app-leak-series.mjs
bchanot 08f01ef363 test(tests): R4.3d browser proofs on the production build: app import baseline on the Rust path, CSP with and without the keyword, leak series, WebKit repetition
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.
2026-10-18 19:48:31 +02:00

601 lines
21 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Leak series of the application's import (R4.3d, BDR-058 leak criterion),
// through the real Home page and its disposable import worker:
// leak per engine, per document (the L envelope, the 16 MiB
// dense numbers refused by the value budget, a rescue copy
// of the demo report), `--reps` times, a fresh browser per
// repetition and ONE tab: Home, the start level, then one
// import (init cost, counted apart) and `--imports` more,
// each by `page.goto('/')` and the pick button; when the
// app asks « Remplacer le bilan en cours ? », « Remplacer »
// opens the picker. 2 s idle after each import, 5 s at the
// end, the content RSS read after each. Chromium: forced GC
// (CDP) + 1 s before each reading; Firefox and WebKit: idle
// only (no forced GC there, indicative). Recurring growth =
// L7 − L2 (the last 5 imports for 6), tolerance 10 MiB.
// webkit-repeat WebKit, one Home page (no navigation), `--webkit-imports`
// consecutive picks of the 16 MiB dense document, a worker
// per import: the inline lines, page errors, console errors
// (page and workers), workers created and closed.
// control (opt-in) the series without the pick: the same tab,
// `page.goto('/')` steps, idle and readings, to tell the
// navigations' own cost from the imports'.
// The browser runs under the 4 GiB RSS watchdog (not a guaranteed limit).
//
// PLAYWRIGHT_NODE_MODULES=... node scripts/browser/r4-app-leak-series.mjs \
// [--engines chromium,firefox,webkit] \
// [--sections leak,webkit-repeat,control] \
// [--reps 3] [--imports 6] [--webkit-imports 20] [--scratch <dir>] \
// [--out <file>]
import { copyFileSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { basename, join } from 'node:path'
import { parseArgs } from 'node:util'
import { messageKeys } from './compare-imports.mjs'
import {
IMPORT_ERROR,
PICK_LABEL,
pageMsOf,
settle,
} from './import-baseline.mjs'
import { checkBuild } from './lib/app-build.mjs'
import {
delay,
firstLine,
isStop,
tagged,
timeoutAfter,
untilStopped,
} from './lib/async.mjs'
import { PKG_DIR, draftBundle } from './lib/core-module.mjs'
import { writeHostileClasses } from './lib/hostile-classes.mjs'
import { hostFacts, memAvailableBytes } from './lib/host.mjs'
import { scratchDir } from './lib/import-inputs.mjs'
import { sha256 } from './lib/inputs.mjs'
import { countErrorLines, probedPage, readProbe } from './lib/page-probes.mjs'
import { FIXTURES_DIR, RESULTS_DIR } from './lib/paths.mjs'
import {
ENGINES,
closeEngine,
launchEngine,
loadPlaywright,
} from './lib/playwright.mjs'
import { leakTable } from './lib/r4-memory-summary.mjs'
import { IDLE_SETTLE_MS, forcedGc, levelNow } from './lib/r4-runs.mjs'
import { resultsJson } from './lib/results-json.mjs'
import { spread } from './lib/stats.mjs'
import { watchdogFacts } from './lib/watchdog.mjs'
import { deltasOf } from './r4-leak-series.mjs'
import { startServer } from './serve.mjs'
const MiB = 1024 * 1024
const SECTIONS = ['leak', 'webkit-repeat', 'control']
const DEFAULT_SECTIONS = ['leak', 'webkit-repeat']
const CONTROL_ID = 'navigation-only'
const HOSTILE_IDS = ['L-envelope', 'dense-numbers-16mib']
const COPY_ID = 'rescue-copy-demo'
const COPY_SOURCE = join(FIXTURES_DIR, 'report-v3.demo.json')
const COPY_NAME = 'copie-de-secours-demo.json'
const WEBKIT_DOCUMENT = 'dense-numbers-16mib'
const REPLACE_LABEL = 'Remplacer' // fr.ts session.replace.confirm
const REPLACE_TITLE = 'Remplacer le bilan en cours ?' // session.replace.title
const IMPORT_TIMEOUT_MS = 120_000 // import-baseline.mjs
const STEP_TIMEOUT_MS = 30_000
const BETWEEN_MS = 2000
const FINAL_IDLE_MS = 5000
const MAX_CONSOLE = 40
// The footer's external logo, blocked by img-src (the known CSP violation).
const FOOTER_IMAGE_HOST = 'zenquality.fr'
const READINGS =
'Readings: content RSS of the browser tree after each import, 2 s idle ' +
'after its end (editor, alert or inline line; the worker is terminated ' +
'by then); 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 = Home loaded, before any pick (GC + ' +
'1 s, or 5 s idle); final = after 5 s more idle. Init Δ = L1 − L0; ' +
'recurring = L7 − L2 (the last 5 imports). A reading after an import ' +
'that opened the editor includes the editor page (its own core instance).'
const today = () => new Date().toISOString().slice(0, 10)
const log = (line) => console.error(`[${new Date().toISOString()}] ${line}`)
const listOf = (text) => text.split(',').filter(Boolean)
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: DEFAULT_SECTIONS.join(',') },
reps: { type: 'string', default: '3' },
imports: { type: 'string', default: '6' },
'webkit-imports': { type: 'string', default: '20' },
scratch: { type: 'string', default: tmpdir() },
out: {
type: 'string',
default: join(RESULTS_DIR, `${today()}-r4-app-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 (bad.length) throw new Error(`unknown engines or sections: ${bad}`)
return {
...values,
engines,
sections,
reps: count(values.reps, 'reps'),
imports: count(values.imports, 'imports'),
webkitImports: count(values['webkit-imports'], 'webkit-imports'),
}
}
/** The L envelope and the dense PDF (budget-suite builders), the copy. */
async function writeDocuments(dir) {
const hostile = await writeHostileClasses({
bundle: draftBundle(),
dir,
pkgDir: PKG_DIR,
wanted: HOSTILE_IDS,
})
const copyPath = join(dir, COPY_NAME)
copyFileSync(COPY_SOURCE, copyPath)
const copyBytes = readFileSync(copyPath).length
const docs = [
...hostile.classes,
{ id: COPY_ID, path: copyPath, bytes: copyBytes },
]
const facts = docs.map((d) => ({
id: d.id,
name: basename(d.path),
bytes: d.bytes,
sha256: sha256(readFileSync(d.path)),
}))
const remove = () => {
hostile.remove()
unlinkSync(copyPath)
}
return { docs, facts, remove }
}
/** The module that built the L envelope, against the one the build ships. */
function envelopeModule(build) {
const pkg = sha256(readFileSync(join(PKG_DIR, 'faunosteo_wasm_bg.wasm')))
const dist = build.wasm.sha256
return { pkgWasmSha256: pkg, distWasmSha256: dist, same: pkg === dist }
}
/** One tab for the whole series; console errors and workers recorded. */
async function openTab(session, baseUrl) {
const context = await session.browser.newContext({ locale: 'fr-FR' })
const seed = { draft: null, locale: 'fr', skipDisclaimer: false }
const { page, events } = await probedPage(context, seed)
const seen = { consoleErrors: [], workers: { created: 0, closed: 0 } }
page.on('console', (m) => {
if (m.type() !== 'error') return
const text = m.text().slice(0, 300)
seen.consoleErrors.push({ worker: m.worker() !== null, text })
})
page.on('worker', (w) => {
seen.workers.created += 1
w.on('close', () => (seen.workers.closed += 1))
})
const chromium = session.engine === 'chromium'
const cdp = chromium ? await context.newCDPSession(page) : null
return { context, page, events, seen, cdp, baseUrl }
}
const pickButton = (page) => page.getByRole('button', { name: PICK_LABEL })
async function goHome(tab) {
await tab.page.goto(`${tab.baseUrl}/`)
const enabled = pickButton(tab.page).and(tab.page.locator(':enabled'))
await enabled.waitFor({ timeout: STEP_TIMEOUT_MS })
}
/**
* Clicks the pick button; the picker opens at once, or the replace dialog
* shows first and « Remplacer » opens it (HomeRoute, ReplaceSessionDialog).
*/
async function openChooser(page) {
const wait = { timeout: STEP_TIMEOUT_MS }
const chooser = page.waitForEvent('filechooser', wait)
await pickButton(page).click(wait)
const replace = page.getByRole('button', { name: REPLACE_LABEL, exact: true })
const first = await Promise.race([
tagged('chooser', chooser),
tagged('replace', replace.waitFor(wait)),
])
if (first.kind === 'error') throw new Error(first.error)
if (first.kind === 'chooser') return { chooser: first.value, replaced: false }
const titled = await page.getByText(REPLACE_TITLE).isVisible()
if (!titled) throw new Error(`no « ${REPLACE_TITLE} » beside the button`)
await replace.click(wait)
return { chooser: await chooser, replaced: true }
}
/** The first end of one import: editor, alert, inline line, or timeout. */
function importEnd(page, lineShown) {
const wait = { timeout: IMPORT_TIMEOUT_MS + 5000 }
return Promise.race([
tagged('editor', page.waitForURL('**/editor', wait)),
tagged('dialog', page.waitForEvent('dialog', wait)),
tagged('error_text', lineShown(wait)),
timeoutAfter(IMPORT_TIMEOUT_MS),
])
}
const harnessEnd = (end) =>
isStop(end) || end.kind === 'timeout' || end.kind === 'error'
/** One pick of `path` on the current Home, raced, then its row. */
async function pickAndWait(session, tab, path, lineShown) {
const { events } = tab
const before = {
dialogs: events.dialogs.length,
pageErrors: events.pageErrors.length,
}
const { chooser, replaced } = await openChooser(tab.page)
const ends = importEnd(tab.page, lineShown)
const start = performance.now()
await chooser.setFiles(path)
const end = await untilStopped(session, ends)
const durationMs = end.at - start
if (harnessEnd(end)) {
return { harness: true, outcome: end.kind, error: end.error, durationMs }
}
await settle(tab, end)
return { replaced, durationMs, before }
}
/** What the import showed: path, inline line (keyed), alert lines. */
async function importRow(tab, picked, keys) {
if (picked.harness) return picked
const { page, events } = tab
const errorText = await page
.locator(IMPORT_ERROR)
.textContent({ timeout: 300 })
.catch(() => null)
const finalPath = new URL(page.url()).pathname
const dialogs = events.dialogs.slice(picked.before.dialogs)
let outcome = dialogs.length ? 'dialog' : 'none'
if (errorText) outcome = 'error'
if (finalPath === '/editor') outcome = 'editor'
return {
outcome,
replaced: picked.replaced,
durationMs: picked.durationMs,
pageMs: pageMsOf(await page.evaluate(readProbe)),
key: errorText && (keys.find((k) => k.re.test(errorText))?.key ?? '?'),
errorText,
alert: dialogs.map((d) => d.message).join(' | ') || null,
pageErrors: events.pageErrors.slice(picked.before.pageErrors),
}
}
const labelOf = (r) =>
r.harness
? r.outcome
: `${r.outcome}${r.replaced ? ' after Remplacer' : ''}` +
(r.key ? ` ${r.key}` : '')
/** One import by `page.goto('/')` then the pick button. */
async function navigatedImport(session, tab, path, keys) {
await goHome(tab)
const line = tab.page.locator(IMPORT_ERROR)
const picked = await pickAndWait(session, tab, path, (w) => line.waitFor(w))
return importRow(tab, picked, keys)
}
async function reading(session, tab) {
if (tab.cdp) await forcedGc(tab)
return levelNow(session)
}
/** A control step: Home loaded again, nothing picked. */
async function navigationOnly(tab) {
await goHome(tab)
return { outcome: 'navigation' }
}
/** ONE tab: start level, the init step, `item.imports` more, final. */
async function measureSeries(session, tab, item, step) {
await goHome(tab)
if (!tab.cdp) await delay(IDLE_SETTLE_MS)
const levels = [await reading(session, tab)]
await session.watchdog.resetPeak()
const imports = []
for (let i = 0; i <= item.imports; i += 1) {
const row = await step()
imports.push(row)
if (row.harness) return seriesCut(tab, row, imports)
await delay(BETWEEN_MS)
levels.push(await reading(session, tab))
}
await delay(FINAL_IDLE_MS)
levels.push(await reading(session, tab))
return seriesRow(session, tab, item, { levels, imports })
}
/** Each section's measure of one item in its tab. */
const MEASURES = {
leak: (session, tab, item, keys) =>
measureSeries(session, tab, item, () =>
navigatedImport(session, tab, item.path, keys),
),
control: (session, tab, item) =>
measureSeries(session, tab, item, () => navigationOnly(tab)),
'webkit-repeat': (session, tab, item, keys) =>
measureRepetition(session, tab, item, keys),
}
const seenOf = (tab) => ({
workers: tab.seen.workers,
consoleErrors: tab.seen.consoleErrors.slice(0, MAX_CONSOLE),
consoleErrorCount: tab.seen.consoleErrors.length,
})
function seriesCut(tab, row, imports) {
const outcomes = imports.map(labelOf)
return { outcome: row.outcome, error: row.error, outcomes, ...seenOf(tab) }
}
function seriesRow(session, tab, item, { levels, imports }) {
const peak = session.watchdog.snapshot()
const content = levels.map((l) => l.rssContentBytes)
const summed = levels.map((l) => l.rssBytes)
return {
outcome: 'series',
reading: item.reading,
outcomes: imports.map(labelOf),
imports,
levels: { content, summed },
deltas: {
content: deltasOf(content, item.imports),
summed: deltasOf(summed, item.imports),
},
peakBytes: peak.peakBytes,
peakContentBytes: peak.peakContentBytes,
...seenOf(tab),
}
}
/** WebKit: consecutive picks on one Home page, no navigation, no idle. */
async function measureRepetition(session, tab, item, keys) {
await goHome(tab)
await tab.page.evaluate(countErrorLines)
await session.watchdog.resetPeak()
const imports = []
const content = []
for (let i = 0; i < item.imports; i += 1) {
const shown = await tab.page.evaluate(() => window.__errorLines)
const lineShown = (wait) =>
tab.page.waitForFunction((n) => window.__errorLines > n, shown, wait)
const picked = await pickAndWait(session, tab, item.path, lineShown)
const row = await importRow(tab, picked, keys)
imports.push(row)
if (row.harness) break
content.push(levelNow(session).rssContentBytes)
}
return repetitionRow(session, tab, item, { imports, content })
}
/** Distinct inline lines with their counts. */
function linesOf(imports) {
const counts = new Map()
for (const r of imports) {
const text = r.errorText ?? `(${r.outcome})`
counts.set(text, (counts.get(text) ?? 0) + 1)
}
return [...counts].map(([text, n]) => ({ text, count: n }))
}
function repetitionRow(session, tab, item, { imports, content }) {
const peak = session.watchdog.snapshot()
return {
outcome: 'repeated',
imports: item.imports,
outcomes: imports.map(labelOf),
durationsMs: imports.map((r) => Math.round(r.durationMs)),
lines: linesOf(imports),
pageErrors: imports.flatMap((r) => r.pageErrors ?? []),
contentLevels: content,
contentAfter: {
first: content[0],
min: Math.min(...content),
max: Math.max(...content),
last: content[content.length - 1],
},
peakBytes: peak.peakBytes,
peakContentBytes: peak.peakContentBytes,
...seenOf(tab),
}
}
/** A fresh browser and tab per item (per repetition). */
async function itemRun(pw, engine, item, context) {
const session = await launchEngine(pw, engine)
const head = { engine, engineVersion: session.version, ...headOf(item) }
const measureOf = MEASURES[item.section]
let tab = null
try {
tab = await openTab(session, context.baseUrl)
return { ...head, ...(await measureOf(session, tab, item, context.keys)) }
} catch (error) {
const stopped = session.watchdog.snapshot().stopped
const outcome = stopped ? 'stopped_by_ceiling' : 'error'
return { ...head, outcome, error: firstLine(error) }
} finally {
await tab?.context.close().catch(() => undefined)
await closeEngine(session)
}
}
const headOf = (item) => ({
section: item.section,
class: item.id,
fileBytes: item.bytes,
...(item.rep ? { rep: item.rep } : {}),
})
function enginePlan(engine, opts, docs) {
const reading = engine === 'chromium' ? 'gc + 1 s' : 'idle only'
const plan = []
if (opts.sections.includes('leak')) {
for (const doc of docs) {
for (let rep = 1; rep <= opts.reps; rep += 1) {
plan.push({
section: 'leak',
...doc,
rep,
reading,
imports: opts.imports,
})
}
}
}
if (opts.sections.includes('control')) {
for (let rep = 1; rep <= opts.reps; rep += 1) {
const step = { id: CONTROL_ID, bytes: 0, rep, reading }
plan.push({ section: 'control', ...step, imports: opts.imports })
}
}
if (engine === 'webkit' && opts.sections.includes('webkit-repeat')) {
const doc = docs.find((d) => d.id === WEBKIT_DOCUMENT)
plan.push({ section: 'webkit-repeat', ...doc, imports: opts.webkitImports })
}
return plan
}
function conditions({ build, server, version, documents, opts }) {
return {
harness: 'scripts/browser/r4-app-leak-series.mjs',
startedAt: new Date().toISOString(),
watchdog: watchdogFacts(),
host: hostFacts(),
build,
server: { bind: '127.0.0.1', csp: server.csp, envJs: 'local tool' },
playwrightCore: version,
documents: documents.facts,
envelopeModule: envelopeModule(build),
flow:
'leak: page.goto("/"), the pick button, « Remplacer » when the ' +
'replace dialog shows, the picker (filechooser) set to the file; ' +
'webkit-repeat: the same pick on one Home page, no navigation',
page: {
imports: { init: 1, then: opts.imports, reps: opts.reps },
betweenMs: BETWEEN_MS,
finalIdleMs: FINAL_IDLE_MS,
webkitImports: opts.webkitImports,
browser: 'relaunched for every repetition',
},
readings: READINGS,
}
}
function repetitionTable(runs) {
const line = (cells) => `| ${cells.join(' | ')} |`
const header = [
'Engine',
'Document',
'Imports',
'Inline lines',
'Page errors',
'Console errors (worker, footer image)',
'Workers created / closed',
'Content after import MiB first / min / max / last',
'Series peak content MiB',
'Node ms median (min–max)',
]
const mb = (v) => (v === undefined ? '–' : (v / MiB).toFixed(1))
const rows = runs.map((r) => {
const c = r.contentAfter ?? {}
const d = spread(r.durationsMs ?? []) ?? {}
const errors = r.consoleErrors ?? []
const image = errors.filter((e) => e.text.includes(FOOTER_IMAGE_HOST))
const consoleCell = r.consoleErrors
? `${r.consoleErrorCount} (worker ${errors.filter((e) => e.worker).length}` +
`, footer image ${image.length})`
: '–'
return line([
r.engine,
r.class,
String(r.outcomes?.length ?? '–'),
(r.lines ?? []).map((l) => `${l.count} × ${l.text}`).join('; ') ||
`${r.outcome} ${r.error ?? ''}`,
String(r.pageErrors?.length ?? '–'),
consoleCell,
r.workers ? `${r.workers.created} / ${r.workers.closed}` : '–',
[c.first, c.min, c.max, c.last].map(mb).join(' / '),
mb(r.peakContentBytes),
`${d.median ?? '–'} (${d.min ?? '–'}–${d.max ?? '–'})`,
])
})
return [line(header), line(header.map(() => '---')), ...rows].join('\n')
}
function summary(result) {
const parts = [result.readings]
if (result.leak.runs.length) {
parts.push('Leak series (content RSS):', leakTable(result.leak.runs))
}
if (result.control.runs.length) {
parts.push('Navigation-only control (content RSS, no pick):')
parts.push(leakTable(result.control.runs))
}
if (result.webkitRepeat.runs.length) {
parts.push('WebKit repetition, one Home page:')
parts.push(repetitionTable(result.webkitRepeat.runs))
}
return parts.join('\n\n')
}
async function main() {
const opts = options()
const build = checkBuild()
const { pw, version } = loadPlaywright()
const keys = await messageKeys()
const scratch = scratchDir(opts.scratch)
const documents = await writeDocuments(scratch.dir)
const server = await startServer()
try {
const result = conditions({ build, server, version, documents, opts })
const context = { baseUrl: server.url, keys }
const runs = []
for (const engine of opts.engines) {
for (const item of enginePlan(engine, opts, documents.docs)) {
const run = await itemRun(pw, engine, item, context)
runs.push(run)
log(
`${engine} ${run.class} ${run.rep ?? ''}: ${run.outcome} ` +
`${run.error ?? (run.outcomes ?? []).join(', ')}`,
)
}
}
result.endedAt = new Date().toISOString()
result.host.memAvailableBytesAtEnd = memAvailableBytes()
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'),
}
result.control = { runs: runs.filter((r) => r.section === 'control') }
writeFileSync(opts.out, `${resultsJson(result)}\n`)
log(`results written to ${opts.out}`)
console.log(summary(result))
} finally {
server.close()
documents.remove()
scratch.remove()
}
}
await main()