// Memory bench of the application export (R4.9, owner §3: the R4.6 // Chromium reserve is answered by measures, not by the R4.8 cleanup). // ONE editor tab per engine and report; the start level, the init export // (module, fonts, first decode: counted apart), `--exports` more by the // same button, then four rest windows: settled (5 s idle), after the 30 s // `downloadBlob` revocation window, Home (the editor left by its link, the // page kept), Home idle (10 s). Every reading tells apart: // (1) the module's linear memory (`WebAssembly.Memory.buffer.byteLength`), // (2) the content-process RSS and the summed RSS of the browser tree, // (3) the objects alive: canvases, images, bitmaps, blobs, the PDF blobs // (created − collected after a GC) and the object URLs (created − // revoked); Chromium adds the JS heap, the DOM counters and, at three // points, a heap-snapshot census of the same classes, // (4) the transient peak of the content RSS during each export, // (5) what each rest window keeps against the level after the init export; // Chromium ends with a simulated critical memory pressure (CDP), which // asks the renderer to drop its caches and return free pages. // `--mode raster` is the control: after the init export, each step replays // the rasteriser's own steps on the views the init export drew, with no // core call, to tell the browser's raster path apart from the session. // Chromium readings follow a forced GC (CDP) + 1 s; Firefox and WebKit are // read after idle only (no forced GC: the collected counts are indicative). // // PLAYWRIGHT_NODE_MODULES=... node scripts/browser/r4-memory-bench.mjs \ // [--engines chromium,firefox,webkit] [--reports demo,L] [--exports 6] \ // [--mode export|raster] [--no-pressure] [--no-snapshots] [--out ] import { mkdirSync, writeFileSync } from 'node:fs' import { dirname, join } from 'node:path' import { parseArgs } from 'node:util' import { pathToFileURL } from 'node:url' import { checkBuild } from './lib/app-build.mjs' import { delay, firstLine } from './lib/async.mjs' import { readProbe } from './lib/page-probes.mjs' import { hostFacts } from './lib/host.mjs' import { KINDS, domMetrics, heapCensus, installMemoryProbes, rasterControl, readMemoryProbe, } from './lib/memory-probes.mjs' import { RESULTS_DIR } from './lib/paths.mjs' import { ENGINES, closeEngine, launchEngine, loadPlaywright, } from './lib/playwright.mjs' import { forcedGc, levelNow, listOf, log, today } from './lib/r4-runs.mjs' import { watchdogFacts } from './lib/watchdog.mjs' import { REPORTS, exportOnce, openEditor } from './r4-export-series.mjs' import { startServer } from './serve.mjs' const HOME_LINK = 'Accueil' // fr.ts common.home (AppHeader) const BETWEEN_MS = 2000 const SETTLE_MS = 5000 const REVOKE_WINDOW_MS = 31_000 // downloadBlob revokes after 30 s const HOME_IDLE_MS = 10_000 const PRESSURE_SETTLE_MS = 3000 const VIEW_WIDTH_PX = 1200 // the core's raster width for a view const MODES = ['export', 'raster'] const MIB = 1024 * 1024 const READINGS = 'Readings after each step: linear = sum of the live WebAssembly.Memory ' + 'buffers of the page; rssContent / rssSummed = watchdog (VmRSS of the ' + 'renderer processes / the whole tree); live = created − collected per ' + 'kind (FinalizationRegistry, valid after a GC), urlsLive = created − ' + 'revoked; Chromium: jsHeap (performance.memory), dom (Performance.' + 'getMetrics), census (heap snapshot) at start, after the series and at ' + 'Home idle. Chromium readings follow HeapProfiler.collectGarbage + 1 s; ' + 'Firefox and WebKit are read after idle only. peakContent = watchdog ' + 'peak of the content RSS between two readings (the export in between).' function options() { const { values } = parseArgs({ options: { engines: { type: 'string', default: ENGINES.join(',') }, reports: { type: 'string', default: 'demo,L' }, exports: { type: 'string', default: '6' }, mode: { type: 'string', default: 'export' }, 'no-pressure': { type: 'boolean', default: false }, 'no-snapshots': { type: 'boolean', default: false }, out: { type: 'string', default: join(RESULTS_DIR, `${today()}-r4-memory-bench.json`), }, }, }) const reports = listOf(values.reports) for (const r of reports) { if (!REPORTS[r]) throw new Error(`unknown report ${r}`) } if (!MODES.includes(values.mode)) throw new Error(`unknown mode ${values.mode}`) return { engines: listOf(values.engines), reports, exports: Number(values.exports), mode: values.mode, pressure: !values['no-pressure'], snapshots: !values['no-snapshots'], out: values.out, } } /** One reading of every dimension at `step`, after the engine's settle. */ async function reading(session, tab, step, { census }) { if (tab.cdp) await forcedGc(tab) const rss = levelNow(session) const probe = await tab.page.evaluate(readMemoryProbe) const live = Object.fromEntries( KINDS.map((k) => [k, probe.created[k] - probe.collected[k]]), ) const peak = session.watchdog.snapshot() const row = { step, linear: probe.linearBytes, instances: probe.instances, instancesEver: probe.instancesEver, rssContent: rss.rssContentBytes, rssSummed: rss.rssBytes, peakContent: peak.peakContentBytes, created: probe.created, collected: probe.collected, live, urls: probe.urls, urlsLive: probe.urls.created - probe.urls.revoked, jsHeap: probe.jsHeap, dom: tab.cdp ? await domMetrics(tab.cdp) : null, census: tab.cdp && census ? await heapCensus(tab.cdp) : null, } await session.watchdog.resetPeak() return row } /** The view sizes the init export drew (logos excluded). */ async function viewSizes(tab) { const probe = await tab.page.evaluate(readProbe) return probe.png .filter((p) => p.width === VIEW_WIDTH_PX) .map(({ width, height }) => ({ width, height })) } /** One step of the series: an export, or the raster control after the init. */ async function seriesStep(tab, { name, mode, sizes }) { if (mode === 'export' || sizes === null) { return exportOnce(tab, REPORTS[name].timeoutMs) } const started = performance.now() const drawn = await tab.page.evaluate(rasterControl, { sizes }) const ms = Math.round(performance.now() - started) return { outcome: 'downloaded', control: 'raster', drawn, clickToEndMs: ms } } /** The init export and `count` more steps, a reading after each. */ async function exportSeries(session, tab, { name, mode, count }, rows) { const exports = [] let sizes = null for (let i = 0; i <= count; i += 1) { const row = await seriesStep(tab, { name, mode, sizes }) if (i === 0 && mode === 'raster') sizes = await viewSizes(tab) exports.push(row) if (row.outcome !== 'downloaded') return { exports, failed: row } await delay(BETWEEN_MS) const step = i === 0 ? 'init' : `export ${i}` rows.push(await reading(session, tab, step, { census: false })) } return { exports, failed: null } } /** Leaves the editor through its own link: the page and its runtime stay. */ async function goHome(tab) { await tab.page.getByRole('link', { name: HOME_LINK }).click() await tab.page.waitForURL(/\/$/) } /** Chromium: a critical memory pressure notification, then a reading. */ async function underPressure(session, tab, rows) { await tab.cdp.send('Memory.simulatePressureNotification', { level: 'critical', }) await delay(PRESSURE_SETTLE_MS) rows.push(await reading(session, tab, 'pressure', { census: false })) } /** The rest windows after the series. */ async function restWindows(session, tab, rows, { seriesEndAt, census }) { await delay(SETTLE_MS) rows.push(await reading(session, tab, 'settled', { census })) const elapsed = performance.now() - seriesEndAt await delay(Math.max(0, REVOKE_WINDOW_MS - elapsed)) rows.push(await reading(session, tab, 'after revoke', { census: false })) await goHome(tab) await delay(BETWEEN_MS) rows.push(await reading(session, tab, 'home', { census: false })) await delay(HOME_IDLE_MS) rows.push(await reading(session, tab, 'home idle', { census })) } const bytesAt = (rows, step, key) => rows.find((r) => r.step === step)?.[key] const delta = (rows, from, to, key) => bytesAt(rows, to, key) - bytesAt(rows, from, key) /** The derived figures (bytes): init, per export, recurring, transient, kept. */ function derive(rows, count) { const after = rows.filter((r) => r.step.startsWith('export ')) const perExport = (key) => after.map( (r, i) => r[key] - (i === 0 ? bytesAt(rows, 'init', key) : after[i - 1][key]), ) const last5 = (key) => { const from = after[Math.max(0, after.length - 5) - 1] ?? rows.find((r) => r.step === 'init') return after.at(-1)[key] - from[key] } const kept = (step, key) => delta(rows, 'init', step, key) const dims = ['linear', 'rssContent'] const out = { exportsAfterInit: after.length, requested: count } for (const key of dims) { out[key] = { init: delta(rows, 'start', 'init', key), perExport: perExport(key), last5: last5(key), settled: kept('settled', key), afterRevoke: kept('after revoke', key), home: kept('home', key), homeIdle: kept('home idle', key), pressure: bytesAt(rows, 'pressure', key) === undefined ? null : kept('pressure', key), } } // The peak between two readings against the level before the export. out.transientPeak = after.map( (r, i) => r.peakContent - (i === 0 ? bytesAt(rows, 'init', 'rssContent') : after[i - 1].rssContent), ) const homeIdle = rows.find((r) => r.step === 'home idle') out.liveAtHomeIdle = homeIdle?.live ?? null out.urlsLiveAtHomeIdle = homeIdle?.urlsLive ?? null return out } async function benchRun(pw, run) { const { engine, baseUrl, report, count, mode, pressure, snapshots } = run const session = await launchEngine(pw, engine) const head = { engine, engineVersion: session.version, report, mode } const rows = [] try { const tab = await openEditor(session, baseUrl, report, [ installMemoryProbes, ]) if (tab.cdp) await tab.cdp.send('Performance.enable') if (!tab.cdp) await delay(SETTLE_MS) rows.push(await reading(session, tab, 'start', { census: snapshots })) const series = await exportSeries( session, tab, { name: report, mode, count }, rows, ) if (series.failed) { return { ...head, outcome: series.failed.outcome, error: series.failed.error, rows, } } await restWindows(session, tab, rows, { seriesEndAt: performance.now(), census: snapshots, }) if (tab.cdp && pressure) await underPressure(session, tab, rows) return { ...head, outcome: 'bench', exports: series.exports, rows, derived: derive(rows, count), pageErrors: tab.events.pageErrors, consoleErrors: tab.events.consoleErrors.slice(0, 20), } } catch (error) { const stopped = session.watchdog.snapshot().stopped const outcome = stopped ? 'stopped_by_ceiling' : 'error' return { ...head, outcome, error: firstLine(error), rows } } finally { await closeEngine(session) } } const mib = (bytes) => (bytes / MIB).toFixed(1) const list = (values) => values.map(mib).join(' / ') function table(runs) { const lines = [ '| Engine | Report | Mode | Dim | Init | Per step | Last 5 | Settled | After revoke | Home | Home idle | Pressure |', '|---|---|---|---|---|---|---|---|---|---|---|---|', ] for (const run of runs) { if (run.outcome !== 'bench') { lines.push( `| ${run.engine} | ${run.report} | ${run.mode} | ${run.outcome} | | | | | | | | |`, ) continue } for (const dim of ['linear', 'rssContent']) { const d = run.derived[dim] lines.push( `| ${run.engine} | ${run.report} | ${run.mode} | ${dim} | ${mib(d.init)} | ${list(d.perExport)} | ${mib(d.last5)} | ${mib(d.settled)} | ${mib(d.afterRevoke)} | ${mib(d.home)} | ${mib(d.homeIdle)} | ${d.pressure === null ? '' : mib(d.pressure)} |`, ) } const live = run.derived.liveAtHomeIdle lines.push( `| ${run.engine} | ${run.report} | ${run.mode} | transient peak | | ${list(run.derived.transientPeak)} | | | | live at home idle: ${JSON.stringify(live)}, urls ${run.derived.urlsLiveAtHomeIdle} | | |`, ) } return lines.join('\n') } async function main() { const opts = options() const build = checkBuild() const { pw } = loadPlaywright() const server = await startServer({ csp: 'with' }) const runs = [] try { for (const engine of opts.engines) { for (const report of opts.reports) { const run = await benchRun(pw, { engine, baseUrl: server.url, report, count: opts.exports, mode: opts.mode, pressure: opts.pressure, snapshots: opts.snapshots, }) log(`bench ${engine} ${report} ${opts.mode}: ${run.outcome}`) runs.push(run) } } } finally { await server.close() } const result = { harness: 'scripts/browser/r4-memory-bench.mjs', startedAt: new Date().toISOString(), readings: READINGS, watchdog: watchdogFacts(), host: hostFacts(), build, options: opts, runs, } mkdirSync(dirname(opts.out), { recursive: true }) writeFileSync(opts.out, `${JSON.stringify(result, null, 2)}\n`) log(`results written to ${opts.out}`) console.log(table(runs)) } if (import.meta.url === pathToFileURL(process.argv[1]).href) await main()