test(tests): R4.1e browser harness and TS baselines (Chromium, Firefox, WebKit)

Playwright harness under scripts/browser: 127.0.0.1 server with the DEPLOY
CSP, export and import baselines of the TypeScript engine under a 4 GiB RSS
watchdog (not a guaranteed memory limit), O9 browser columns in
inflate.browsers.json (26 rows agree with Node in all three engines).
Results of 2026-10-18 committed; the O9 Node test no longer expects browser
columns in inflate.json.
This commit is contained in:
bchanot committed 2026-10-18 15:05:28 +02:00
1 parent 4bdfda59ba
commit 4439fd2420
23 files changed
+20903 -18

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
import { execFileSync } from 'node:child_process'
import { existsSync, readdirSync, statSync } from 'node:fs'
import { join } from 'node:path'
import { DIST_DIR, REPO_ROOT } from './paths.mjs'
/**
* The harness measures a production build that embeds the reference sheet.
* It never builds by itself: the build command is printed instead, so a run
* always names the build it measured (mtime, chunk sizes, HEAD).
*/
export const BUILD_COMMAND =
'SHEET_BUNDLE=draft corepack pnpm --filter @faunosteo/app build'
// A build without the sheet (SHEET_BUNDLE=none) leaves a 40-byte stub chunk.
const MIN_SHEET_CHUNK_BYTES = 10_000
function assetSize(pattern) {
const assets = join(DIST_DIR, 'assets')
const name = readdirSync(assets).find((file) => pattern.test(file))
return name ? { name, bytes: statSync(join(assets, name)).size } : null
}
function headCommit() {
try {
const args = ['-C', REPO_ROOT, 'rev-parse', '--short', 'HEAD']
return execFileSync('git', args, { encoding: 'utf8' }).trim()
} catch {
return null
}
}
export function checkBuild() {
const index = join(DIST_DIR, 'index.html')
if (!existsSync(index)) {
throw new Error(`no build in apps/app/dist: run ${BUILD_COMMAND}`)
}
const sheet = assetSize(/^_virtual_faunosteo-sheet-.*\.js$/)
if (!sheet || sheet.bytes < MIN_SHEET_CHUNK_BYTES) {
throw new Error(`the build carries no sheet: run ${BUILD_COMMAND}`)
}
return {
distDir: 'apps/app/dist',
builtAt: statSync(index).mtime.toISOString(),
sheetChunk: sheet,
entryChunk: assetSize(/^index-.*\.js$/),
wasm: assetSize(/\.wasm$/),
headCommit: headCommit(),
}
}
+41
View File
@@ -0,0 +1,41 @@
/** Promise helpers for races whose losers must never surface as unhandled. */
export const delay = (ms) => new Promise((done) => setTimeout(done, ms))
export const firstLine = (error) =>
String(error?.message ?? error).split('\n')[0]
/** `{ kind, value, at }` once `promise` resolves; rejected: kind 'error'. */
export function tagged(kind, promise) {
return promise.then(
(value) => ({ kind, value, at: performance.now() }),
(error) => ({
kind: 'error',
error: firstLine(error),
at: performance.now(),
}),
)
}
/** A race arm that never keeps the process alive on its own. */
export function timeoutAfter(ms) {
return new Promise((done) => {
setTimeout(
() => done({ kind: 'timeout', at: performance.now() }),
ms,
).unref()
})
}
/** Races `promise` with the watchdog: a killed browser ends the wait. */
export function untilStopped(session, promise) {
const stopped = session.watchdog.stopped.then((s) => ({
kind: s.type === 'ceiling' ? 'stopped_by_ceiling' : 'watchdog_failed',
error: s.error ?? null,
at: performance.now(),
}))
return Promise.race([promise, stopped])
}
export const isStop = (end) =>
end.kind === 'stopped_by_ceiling' || end.kind === 'watchdog_failed'
+107
View File
@@ -0,0 +1,107 @@
import { mkdirSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { parseArgs } from 'node:util'
import { checkBuild } from './app-build.mjs'
import { hostFacts, memAvailableBytes } from './host.mjs'
import { RESULTS_DIR } from './paths.mjs'
import { ENGINES, loadPlaywright } from './playwright.mjs'
import { markdownSummary } from './summary.mjs'
import { watchdogFacts } from './watchdog.mjs'
import { startServer } from '../serve.mjs'
/**
* Shared CLI of the baseline scripts: checks the build, serves it, runs the
* given sections (`{ export: fn, import: fn }`) engine after engine, writes
* one results file and prints the summary tables.
*/
const today = () => new Date().toISOString().slice(0, 10)
const log = (line) => console.error(`[${new Date().toISOString()}] ${line}`)
const listOf = (text) => (text ? text.split(',').filter(Boolean) : null)
/** --reports and --groups narrow a run (null = all): reruns, smoke tests. */
function options(defaultName) {
const { values } = parseArgs({
options: {
engines: { type: 'string', default: ENGINES.join(',') },
out: { type: 'string', default: join(RESULTS_DIR, defaultName) },
reports: { type: 'string' },
groups: { type: 'string' },
},
})
const engines = listOf(values.engines)
const unknown = engines.filter((e) => !ENGINES.includes(e))
if (unknown.length) throw new Error(`unknown engines: ${unknown.join(', ')}`)
const filters = {
reports: listOf(values.reports),
groups: listOf(values.groups),
}
return { engines, out: values.out, filters }
}
/** Engine versions as the runs recorded them. */
function enginesOf(sections) {
const versions = {}
for (const section of Object.values(sections)) {
for (const run of section.runs ?? []) {
if (run.engineVersion) versions[run.engine] = run.engineVersion
}
}
return versions
}
async function runSections(sections, context) {
const results = {}
for (const [name, run] of Object.entries(sections)) {
log(`section ${name}: ${context.engines.join(', ')}`)
results[name] = await run({ ...context, log })
}
return results
}
/** What a results file states about the conditions of its runs. */
function conditionsOf(build, playwrightCore, server) {
return {
harness: 'scripts/browser',
startedAt: new Date().toISOString(),
watchdog: watchdogFacts(),
host: hostFacts(),
build,
server: { bind: '127.0.0.1', csp: server.csp, envJs: 'local tool' },
playwrightCore,
}
}
function writeResult(out, result) {
mkdirSync(dirname(out), { recursive: true })
writeFileSync(out, `${JSON.stringify(result, null, 2)}\n`)
log(`results written to ${out}`)
console.log(markdownSummary(result))
}
export async function runHarnessCli(sections, defaultName) {
const sectionNames = Object.keys(sections).join('-')
const { engines, out, filters } = options(
defaultName ?? `${today()}-${sectionNames}.json`,
)
const build = checkBuild()
const { pw, version } = loadPlaywright()
const server = await startServer()
try {
const conditions = conditionsOf(build, version, server)
const context = { pw, baseUrl: server.url, engines, filters }
const results = await runSections(sections, context)
conditions.host.memAvailableBytesAtEnd = memAvailableBytes()
const endedAt = new Date().toISOString()
const engineVersions = enginesOf(results)
writeResult(out, {
...conditions,
endedAt,
engines: engineVersions,
...results,
})
} finally {
server.close()
}
}
+27
View File
@@ -0,0 +1,27 @@
import { readFileSync } from 'node:fs'
import { arch, cpus, release, totalmem } from 'node:os'
/** The host facts a reader of the results needs to compare devices. */
export function memAvailableBytes() {
const info = readFileSync('/proc/meminfo', 'utf8')
const match = /^MemAvailable:\s+(\d+) kB$/m.exec(info)
return match ? Number(match[1]) * 1024 : null
}
function cpuModel() {
const info = readFileSync('/proc/cpuinfo', 'utf8')
return /^model name\s*:\s*(.+)$/m.exec(info)?.[1] ?? cpus()[0]?.model
}
export function hostFacts() {
return {
cpuModel: cpuModel(),
logicalCpus: cpus().length,
ramBytes: totalmem(),
memAvailableBytesAtStart: memAvailableBytes(),
kernel: release(),
arch: arch(),
node: process.version,
}
}
+52
View File
@@ -0,0 +1,52 @@
import { createHash } from 'node:crypto'
import { readFileSync, readdirSync, statSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
/**
* Fixture files, and the R4 oracle inputs committed as generator
* descriptions (`<slug>.gen.json`, generator `concat/1`, written by
* packages/pdf/tests/helpers/r4-reader-io.ts for inputs over 2 MiB).
*/
export const sha256 = (bytes) =>
createHash('sha256').update(bytes).digest('hex')
/** Files under `dir`, sorted, recursively when asked. */
export function filesUnder(dir, recursive = false) {
const files = []
for (const name of readdirSync(dir).sort()) {
const path = join(dir, name)
if (statSync(path).isDirectory()) {
if (recursive) files.push(...filesUnder(path, true))
} else {
files.push(path)
}
}
return files
}
export const isGenerator = (path) => path.endsWith('.gen.json')
function partBytes(part, dir) {
if ('latin1' in part) return Buffer.from(part.latin1, 'latin1')
if ('fill' in part) return Buffer.alloc(part.count, part.fill)
const source = readFileSync(resolve(dir, part.slice))
return source.subarray(part.start, part.end)
}
/** The bytes a description stands for, checked: length and digest. */
export function expandGenerator(path) {
const description = JSON.parse(readFileSync(path, 'utf8'))
if (description.generator !== 'concat/1') {
throw new Error(`${path}: unknown generator ${description.generator}`)
}
const parts = description.parts.map((p) => partBytes(p, dirname(path)))
const bytes = Buffer.concat(parts)
if (bytes.length !== description.length) {
throw new Error(`${path}: expands to ${bytes.length} bytes`)
}
if (sha256(bytes) !== description.sha256) {
throw new Error(`${path}: digest differs from the description`)
}
return bytes
}
+144
View File
@@ -0,0 +1,144 @@
/**
* Init scripts (run in the page before its own scripts). They record what
* the measures need and call through to every function they wrap, so the
* application behaves as without them. Each function is serialised by
* Playwright: it must not reference anything outside its own body.
*/
/** Seeds the tab as the app leaves it (persistence.ts, EditorRoute). */
export function seedTab({ draft, locale, skipDisclaimer }) {
if (location.origin === 'null') return
if (draft !== null && sessionStorage.getItem('osteo:draft') === null) {
sessionStorage.setItem('osteo:draft', draft)
}
if (skipDisclaimer) sessionStorage.setItem('osteo:disclaimer-shown', '1')
localStorage.setItem('osteo:locale', locale)
}
export function installProbes() {
const probe = {
png: [],
dataUrls: 0,
clickAt: null,
pdfBlobAt: null,
changeAt: null,
navAt: null,
alertAt: null,
longTaskSupported: false,
longTasks: [],
csp: [],
}
window.__harness = probe
const now = () => performance.now()
// Every PNG the browser rasteriser encodes goes through toBlob.
function wrapCanvas() {
const proto = HTMLCanvasElement.prototype
const toBlob = proto.toBlob
proto.toBlob = function (callback, ...rest) {
const size = { width: this.width, height: this.height }
const record = (blob) => {
probe.png.push({ ...size, bytes: blob ? blob.size : null })
callback(blob)
}
return toBlob.call(this, record, ...rest)
}
const toDataURL = proto.toDataURL
proto.toDataURL = function (...args) {
probe.dataUrls += 1
return toDataURL.apply(this, args)
}
}
// download.ts hands the finished PDF to URL.createObjectURL.
function wrapObjectUrl() {
const create = URL.createObjectURL
URL.createObjectURL = function (object) {
if (object instanceof Blob && object.type === 'application/pdf') {
probe.pdfBlobAt = now()
}
return create.call(URL, object)
}
}
// HomeRoute ends an import with an alert, a navigation, or both.
function wrapNavigationAndAlert() {
const push = history.pushState
history.pushState = function (...args) {
if (probe.navAt === null) probe.navAt = now()
return push.apply(this, args)
}
const alert = window.alert
window.alert = function (message) {
if (probe.alertAt === null) probe.alertAt = now()
return alert.call(window, message)
}
}
function observeLongTasks() {
const types = PerformanceObserver.supportedEntryTypes ?? []
if (!types.includes('longtask')) return
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
probe.longTasks.push({ start: entry.startTime, ms: entry.duration })
}
})
observer.observe({ type: 'longtask' })
probe.longTaskSupported = true
}
function listenToEvents() {
// The first click only: download.ts clicks its own anchor afterwards.
window.addEventListener(
'click',
() => {
if (probe.clickAt === null) probe.clickAt = now()
},
true,
)
window.addEventListener(
'change',
(e) => {
if (e.target?.type === 'file' && probe.changeAt === null) {
probe.changeAt = now()
}
},
true,
)
document.addEventListener('securitypolicyviolation', (e) =>
probe.csp.push(`${e.effectiveDirective} ${e.blockedURI}`),
)
}
wrapCanvas()
wrapObjectUrl()
wrapNavigationAndAlert()
observeLongTasks()
listenToEvents()
}
/** Reads the probe back (a structured-clone copy). */
export const readProbe = () => window.__harness
/**
* A page of a fresh context with the probes and the tab seed, dialogs
* accepted and recorded (the app's alerts block its script until then).
*/
export async function probedPage(context, seed) {
await context.addInitScript(seedTab, seed)
await context.addInitScript(installProbes)
const page = await context.newPage()
const events = { dialogs: [], pageErrors: [], consoleErrors: [] }
let firstDialog
events.firstDialog = new Promise((done) => (firstDialog = done))
page.on('dialog', (dialog) => {
events.dialogs.push({ type: dialog.type(), message: dialog.message() })
firstDialog(dialog.message())
dialog.accept().catch(() => undefined)
})
page.on('pageerror', (e) => events.pageErrors.push(String(e.message)))
page.on('console', (m) => {
if (m.type() === 'error') events.consoleErrors.push(m.text().slice(0, 300))
})
return { page, events }
}
+10
View File
@@ -0,0 +1,10 @@
import { resolve } from 'node:path'
export const HARNESS_DIR = resolve(import.meta.dirname, '..')
export const REPO_ROOT = resolve(HARNESS_DIR, '../..')
export const DIST_DIR = resolve(REPO_ROOT, 'apps/app/dist')
export const FIXTURES_DIR = resolve(REPO_ROOT, 'apps/app/tests/fixtures')
export const ORACLES_R4_DIR = resolve(REPO_ROOT, 'crates/core/tests/oracles/r4')
export const RESULTS_DIR = resolve(HARNESS_DIR, 'results')
export const relativeToRepo = (path) => path.slice(REPO_ROOT.length + 1)
+74
View File
@@ -0,0 +1,74 @@
import { createRequire } from 'node:module'
import { join } from 'node:path'
import { delay } from './async.mjs'
import { childrenOf, killAll, treeOf } from './proc.mjs'
import { startWatchdog } from './watchdog.mjs'
/**
* Playwright stays outside the repository: PLAYWRIGHT_NODE_MODULES names a
* node_modules that holds playwright-core (no project dependency), as for
* docker/csp-proof/app-image.sh. Browsers come from its own cache
* (~/.cache/ms-playwright).
*/
export const ENGINES = ['chromium', 'firefox', 'webkit']
const CLOSE_TIMEOUT_MS = 15_000
function modulesDir() {
const dir = process.env.PLAYWRIGHT_NODE_MODULES
if (!dir) {
throw new Error(
'set PLAYWRIGHT_NODE_MODULES to a node_modules holding playwright-core',
)
}
return dir
}
export function loadPlaywright() {
const require = createRequire(import.meta.url)
const dir = modulesDir()
const pw = require(join(dir, 'playwright-core'))
const { version } = require(join(dir, 'playwright-core/package.json'))
return { pw, version }
}
/** The one child process `launch` started: the root of the browser's tree. */
function newChild(before) {
const fresh = childrenOf(process.pid).filter((pid) => !before.has(pid))
if (fresh.length !== 1) {
throw new Error(`expected one new browser process, found ${fresh.length}`)
}
return fresh[0]
}
/**
* Launches one headless browser under its watchdog. Fails closed: a browser
* whose process tree cannot be identified is closed, never run unwatched.
*/
export async function launchEngine(pw, engine) {
const before = new Set(childrenOf(process.pid))
const browser = await pw[engine].launch({ headless: true })
let rootPid
try {
rootPid = newChild(before)
} catch (error) {
await browser.close()
throw error
}
const watchdog = startWatchdog({ rootPid })
await watchdog.ready
return { engine, browser, rootPid, version: browser.version(), watchdog }
}
/** Closes the browser; one that hangs is killed, its own tree only. */
export async function closeEngine(session) {
const closed = session.browser.close().then(
() => true,
() => true,
)
const timedOut = delay(CLOSE_TIMEOUT_MS).then(() => false)
if (!(await Promise.race([closed, timedOut]))) {
killAll(treeOf(session.rootPid))
}
await session.watchdog.stop()
}
+92
View File
@@ -0,0 +1,92 @@
import { readFileSync, readdirSync } from 'node:fs'
/** Process facts from /proc (Linux only): the watchdog's only source. */
function readOrEmpty(path) {
try {
return readFileSync(path, 'utf8')
} catch {
return ''
}
}
function listOrEmpty(path) {
try {
return readdirSync(path)
} catch {
return []
}
}
const numbersOf = (text) => text.split(/\s+/).filter(Boolean).map(Number)
/** Direct children of `pid`, over all its threads; [] once it is gone. */
export function childrenOf(pid) {
return listOrEmpty(`/proc/${pid}/task`).flatMap((tid) =>
numbersOf(readOrEmpty(`/proc/${pid}/task/${tid}/children`)),
)
}
/** `root` and every process below it, breadth first. */
export function treeOf(root) {
const tree = [root]
for (let i = 0; i < tree.length; i += 1) {
for (const child of childrenOf(tree[i])) {
if (!tree.includes(child)) tree.push(child)
}
}
return tree
}
/** VmRSS in bytes; 0 for a process that is gone or a zombie. */
export function rssBytesOf(pid) {
const status = readOrEmpty(`/proc/${pid}/status`)
const match = /^VmRSS:\s+(\d+) kB$/m.exec(status)
return match ? Number(match[1]) * 1024 : 0
}
/**
* A process that renders web content: Chromium's renderers, Firefox's
* "Web Content" and "Isolated Web Content" (comm is cut at 15 bytes),
* WebKit's web process. Read at every sample: names change after exec.
*/
export function isContentProcess(pid) {
const comm = readOrEmpty(`/proc/${pid}/comm`).trim()
if (comm.startsWith('WPEWebProcess') || comm.startsWith('WebKitWebProc')) {
return true
}
if (comm.startsWith('Web Content') || comm.startsWith('Isolated Web')) {
return true
}
// Chromium rewrites its argv as one space-separated title.
const cmdline = readOrEmpty(`/proc/${pid}/cmdline`)
return /(^|[\0 ])--type=renderer([\0 ]|$)/.test(cmdline)
}
/** Summed, content-process and largest single VmRSS of the tree. */
export function treeRss(root) {
const pids = treeOf(root)
const sizes = pids.map(rssBytesOf)
const content = pids.filter((pid, i) => sizes[i] > 0 && isContentProcess(pid))
const sum = (list) => list.reduce((total, size) => total + size, 0)
return {
pids,
totalBytes: sum(sizes),
contentBytes: sum(content.map((pid) => sizes[pids.indexOf(pid)])),
maxProcessBytes: Math.max(0, ...sizes),
}
}
/**
* SIGKILL to each pid, the first one (the tree's root) first so it cannot
* respawn a child. Callers pass only a tree they launched.
*/
export function killAll(pids) {
for (const pid of pids) {
try {
process.kill(pid, 'SIGKILL')
} catch {
// Already gone.
}
}
}
+21
View File
@@ -0,0 +1,21 @@
/** Small descriptive statistics for the summary tables. */
const sorted = (values) => [...values].sort((a, b) => a - b)
export function median(values) {
const list = sorted(values)
if (list.length === 0) return null
const mid = Math.floor(list.length / 2)
return list.length % 2 ? list[mid] : (list[mid - 1] + list[mid]) / 2
}
/** Median and range of the finite numbers of `values` (nulls skipped). */
export function spread(values) {
const list = sorted(values.filter((v) => Number.isFinite(v)))
if (list.length === 0) return null
const min = list[0]
const max = list[list.length - 1]
return { n: list.length, median: median(list), min, max }
}
export const sum = (values) => values.reduce((total, v) => total + v, 0)
+266
View File
@@ -0,0 +1,266 @@
import { spread } from './stats.mjs'
/**
* Markdown tables from a results file (the shape lib/harness.mjs writes),
* ready for docs/development/RUST.md §10.
*/
const MB = 1024 * 1024
const orDash = (format) => (value) =>
value === null || value === undefined ? '–' : format(value)
const mb = orDash((bytes) => (bytes / MB).toFixed(1))
const kb = orDash((bytes) => (bytes / 1024).toFixed(0))
const ms = orDash((value) => value.toFixed(0))
const s = orDash((value) => (value / 1000).toFixed(1))
/** "median (min–max)" or the single value. */
function range(values, format) {
const stats = spread(values)
if (!stats) return '–'
if (stats.n === 1) return format(stats.median)
return `${format(stats.median)} (${format(stats.min)}–${format(stats.max)})`
}
function table(header, rows) {
const line = (cells) => `| ${cells.join(' | ')} |`
return [line(header), line(header.map(() => '---')), ...rows.map(line)].join(
'\n',
)
}
function groupBy(items, keyOf) {
const groups = new Map()
for (const item of items) {
const key = keyOf(item)
groups.set(key, [...(groups.get(key) ?? []), item])
}
return groups
}
function outcomes(runs) {
const counts = groupBy(runs, (r) => r.outcome)
return [...counts]
.map(([outcome, list]) => `${outcome} ${list.length}`)
.join(', ')
}
const pick = (runs, path) =>
runs.map((r) => path.split('.').reduce((v, k) => v?.[k], r))
/** Where the watchdog stopped a run: summed RSS and PNG blobs encoded. */
function stopsOf(runs) {
const stopped = runs.filter((r) => r.outcome === 'stopped_by_ceiling')
const at = (r) =>
`${mb(r.stoppedAtBytes)} MB after ${r.rastersBeforeEnd} PNGs`
return stopped.map(at).join('; ') || '–'
}
function exportTimeRow([key, runs]) {
const [engine, report] = key.split(' ')
const done = runs.filter((r) => r.outcome === 'downloaded')
return [
engine,
report,
outcomes(runs),
range(pick(done, 'exportMs'), s),
range(pick(done, 'pdfBytes'), mb),
range(pick(done, 'rasters.count'), String),
range(pick(done, 'rasters.views.totalBytes'), mb),
range(pick(done, 'rasters.views.minBytes'), kb),
range(pick(done, 'rasters.views.maxBytes'), kb),
stopsOf(runs),
]
}
function exportMemoryRow([key, runs]) {
const [engine, report] = key.split(' ')
const done = runs.filter((r) => r.outcome === 'downloaded')
return [
engine,
report,
range(pick(runs, 'before.rssBytes'), mb),
range(pick(runs, 'rssPeakBytes'), mb),
range(pick(done, 'after.rssBytes'), mb),
range(pick(runs, 'before.rssContentBytes'), mb),
range(pick(runs, 'rssPeakContentBytes'), mb),
range(pick(done, 'after.rssContentBytes'), mb),
range(pick(runs, 'before.jsHeapUsedBytes'), mb),
range(pick(done, 'after.jsHeapUsedBytes'), mb),
]
}
export function exportTables(section) {
const groups = [...groupBy(section.runs, (r) => `${r.engine} ${r.report}`)]
const time = [
'Engine',
'Report',
'Outcome',
'Export s',
'PDF MB',
'PNG blobs',
'View PNG sum MB',
'View PNG min KB',
'View PNG max KB',
'Stopped at',
]
const memory = [
'Engine',
'Report',
'RSS before MB',
'RSS peak MB',
'RSS after MB',
'Content before MB',
'Content peak MB',
'Content after MB',
'JS heap before MB',
'JS heap after MB',
]
return [
table(time, groups.map(exportTimeRow)),
table(memory, groups.map(exportMemoryRow)),
].join('\n\n')
}
function importGroupRow([key, runs]) {
const [engine, group] = key.split(' ')
const growth = runs.map(
(r) => (r.rssPeakBytes ?? 0) - (r.rssBeforeBytes ?? 0),
)
const content = runs.map(
(r) => (r.rssPeakContentBytes ?? 0) - (r.rssBeforeContentBytes ?? 0),
)
return [
engine,
group,
String(runs.length),
outcomes(runs),
range(pick(runs, 'durationMs'), ms),
ms(Math.max(...pick(runs, 'durationMs').filter(Number.isFinite))),
mb(Math.max(...growth)),
mb(Math.max(...content)),
range(pick(runs, 'longTasks.maxMs'), ms),
range(pick(runs, 'longTasks.totalMs'), ms),
]
}
export function importGroupTable(section) {
const header = [
'Engine',
'Group',
'Files',
'Outcome',
'Duration ms',
'Max ms',
'Max RSS growth MB',
'Max content growth MB',
'Long task max ms',
'Long tasks total ms',
]
const groups = groupBy(section.runs, (r) => `${r.engine} ${r.group}`)
return table(header, [...groups].map(importGroupRow))
}
const short = (text) => (text ?? '').replace(/\s+/g, ' ').slice(0, 70)
const messageOf = (run) =>
short(run.errorText ?? run.dialogs?.[0] ?? run.error ?? '')
/** Reference files: one row per file, one cell per engine. */
export function importFileTable(section, groups, engines) {
const runs = section.runs.filter((r) => groups.includes(r.group))
const byFile = groupBy(runs, (r) => r.file)
const cell = (list, engine) => {
const run = list.find((r) => r.engine === engine)
return run ? `${run.outcome} ${ms(run.durationMs)} ms` : '–'
}
const rows = [...byFile].map(([file, list]) => [
file.replace('apps/app/tests/fixtures/', ''),
...engines.map((engine) => cell(list, engine)),
messageOf(list[0]),
])
return table(['File', ...engines, 'Message (first engine)'], rows)
}
/** Files whose outcome or message differs between engines. */
export function importDisagreements(section) {
const byFile = groupBy(section.runs, (r) => r.file)
const rows = []
for (const [file, list] of byFile) {
const verdicts = list.map((r) => `${r.outcome}: ${messageOf(r)}`)
if (new Set(verdicts).size < 2) continue
rows.push([
file.replace('apps/app/tests/fixtures/', ''),
...list.map((r, i) => `${r.engine} ${verdicts[i]}`),
])
}
if (rows.length === 0) return 'No file differs between engines.'
return rows.map((r) => `- ${r[0]}: ${r.slice(1).join(' / ')}`).join('\n')
}
/** The slowest and the most memory-hungry imports. */
export function importTop(section, count = 10) {
const growth = (r) => (r.rssPeakBytes ?? 0) - (r.rssBeforeBytes ?? 0)
const row = (r) => [
r.engine,
r.file.replace('apps/app/tests/fixtures/', ''),
r.outcome,
ms(r.durationMs),
mb(growth(r)),
short(messageOf(r)),
]
const header = [
'Engine',
'File',
'Outcome',
'Duration ms',
'RSS growth MB',
'Message',
]
const slow = [...section.runs]
.sort((a, b) => (b.durationMs ?? 0) - (a.durationMs ?? 0))
.slice(0, count)
const heavy = [...section.runs]
.sort((a, b) => growth(b) - growth(a))
.slice(0, count)
return [
'Slowest:',
table(header, slow.map(row)),
'Largest RSS growth:',
table(header, heavy.map(row)),
].join('\n\n')
}
function hostLine(host) {
const ram = `${mb(host.ramBytes)} MB RAM`
return `Host: ${host.cpuModel}, ${host.logicalCpus} threads, ${ram}.`
}
function enginesLine(result, engines) {
const list = engines.map((e) => `${e} ${result.engines[e]}`).join(', ')
return `Engines: ${list} (playwright-core ${result.playwrightCore}).`
}
export function markdownSummary(result) {
const engines = Object.keys(result.engines)
const parts = [
hostLine(result.host),
enginesLine(result, engines),
`Watchdog: ${result.watchdog.caveat}`,
]
if (result.export)
parts.push('Export (TS engine, fr):', exportTables(result.export))
if (result.import) {
parts.push('Import by group:', importGroupTable(result.import))
parts.push(
'Reference files:',
importFileTable(result.import, ['base', 'r3', 'step6'], engines),
)
parts.push('Engines that disagree:', importDisagreements(result.import))
parts.push(importTop(result.import))
if (result.import.hostile.missing.length) {
parts.push(
`Hostile inputs missing: ${result.import.hostile.missing.join(', ')}`,
)
}
}
return parts.join('\n\n')
}
+51
View File
@@ -0,0 +1,51 @@
import { parentPort, workerData } from 'node:worker_threads'
import { killAll, treeOf, treeRss } from './proc.mjs'
import { SAMPLE_INTERVAL_MS, SLOT } from './watchdog.mjs'
/** Sampler thread of lib/watchdog.mjs; writes the shared slots only. */
const { rootPid, ceilingBytes, shared } = workerData
const stats = new Int32Array(shared)
const toKib = (bytes) => Math.round(bytes / 1024)
const PEAKS = [
[SLOT.currentKib, SLOT.peakKib],
[SLOT.currentMaxKib, SLOT.peakMaxKib],
[SLOT.currentContentKib, SLOT.peakContentKib],
]
/** Current values, then each peak (restarted when the main thread asked). */
function record(values) {
for (const [slot, value] of values) Atomics.store(stats, slot, value)
const reset = Atomics.load(stats, SLOT.resetRequested) === 1
for (const [current, peak] of PEAKS) {
const value = Atomics.load(stats, current)
if (reset || value > Atomics.load(stats, peak)) {
Atomics.store(stats, peak, value)
}
}
if (reset) Atomics.store(stats, SLOT.resetRequested, 0)
Atomics.add(stats, SLOT.samples, 1)
}
/** A fresh walk at kill time: children forked since the sample are included. */
function stopBrowser(timer, totalBytes) {
clearInterval(timer)
killAll(treeOf(rootPid))
Atomics.store(stats, SLOT.stoppedAtKib, toKib(totalBytes))
Atomics.store(stats, SLOT.stopped, 1)
parentPort.postMessage({ type: 'ceiling', totalBytes })
}
function sample(timer) {
const rss = treeRss(rootPid)
record([
[SLOT.currentKib, toKib(rss.totalBytes)],
[SLOT.currentMaxKib, toKib(rss.maxProcessBytes)],
[SLOT.currentContentKib, toKib(rss.contentBytes)],
])
if (rss.totalBytes > ceilingBytes) stopBrowser(timer, rss.totalBytes)
}
const timer = setInterval(() => sample(timer), SAMPLE_INTERVAL_MS)
sample(timer)
+125
View File
@@ -0,0 +1,125 @@
import { Worker } from 'node:worker_threads'
import { delay } from './async.mjs'
import { killAll, treeOf } from './proc.mjs'
/**
* Memory ceiling of one launched browser. A worker thread samples the summed
* VmRSS of every process below the browser's root pid every 50 ms and, above
* the ceiling, SIGKILLs that tree (the shared host must never reach the
* kernel OOM killer). The sampler lives in its own thread so a busy main
* event loop cannot delay it.
*/
export const CEILING_BYTES = 4 * 1024 ** 3
export const SAMPLE_INTERVAL_MS = 50
export const WATCHDOG_CAVEAT =
'The 4 GiB RSS watchdog is not a guaranteed memory limit: it samples ' +
'every 50 ms, so a faster allocation can overshoot it before the kill; ' +
'summed VmRSS counts shared pages once per process; a process outside ' +
"the browser's tree is not seen."
/** Shared Int32 slots, sizes in KiB. */
export const SLOT = {
currentKib: 0,
currentMaxKib: 1,
peakKib: 2,
peakMaxKib: 3,
samples: 4,
stopped: 5,
resetRequested: 6,
stoppedAtKib: 7,
currentContentKib: 8,
peakContentKib: 9,
}
const SLOT_COUNT = 10
const RESET_WAIT_MS = 1000
const POLL_MS = 5
export const watchdogFacts = () => ({
ceilingBytes: CEILING_BYTES,
sampleIntervalMs: SAMPLE_INTERVAL_MS,
measure:
'summed VmRSS (/proc/<pid>/status) of the browser process tree; ' +
'content = renderer / Web Content / WPEWebProcess processes only',
onCeiling: 'SIGKILL of the tree, run recorded as stopped_by_ceiling',
caveat: WATCHDOG_CAVEAT,
})
const kib = (stats, slot) => Atomics.load(stats, slot) * 1024
function snapshotOf(stats) {
return {
currentBytes: kib(stats, SLOT.currentKib),
currentMaxProcessBytes: kib(stats, SLOT.currentMaxKib),
peakBytes: kib(stats, SLOT.peakKib),
peakMaxProcessBytes: kib(stats, SLOT.peakMaxKib),
currentContentBytes: kib(stats, SLOT.currentContentKib),
peakContentBytes: kib(stats, SLOT.peakContentKib),
samples: Atomics.load(stats, SLOT.samples),
stopped: Atomics.load(stats, SLOT.stopped) === 1,
stoppedAtBytes: kib(stats, SLOT.stoppedAtKib),
}
}
async function waitUntil(test, timeoutMs, what) {
const end = performance.now() + timeoutMs
while (!test()) {
if (performance.now() > end) throw new Error(`watchdog: ${what}`)
await delay(POLL_MS)
}
}
/** Starts a peak window at the next sample (the sampler owns the write). */
async function resetPeak(stats) {
Atomics.store(stats, SLOT.resetRequested, 1)
const done = () => Atomics.load(stats, SLOT.resetRequested) === 0
await waitUntil(done, RESET_WAIT_MS, 'peak reset not acknowledged')
}
/**
* `stopped` resolves when the sampler killed the tree, or when the sampler
* itself died: the tree is then killed here, so no browser runs unwatched.
*/
function stoppedPromise(worker, rootPid, stats, isStopping) {
return new Promise((resolve) => {
let settled = false
const settle = (message) => {
settled = true
resolve(message)
}
const failed = (error) => {
if (settled || isStopping()) return
if (Atomics.load(stats, SLOT.stopped) === 1) {
return settle({
type: 'ceiling',
totalBytes: kib(stats, SLOT.stoppedAtKib),
})
}
killAll(treeOf(rootPid))
settle({ type: 'watchdog_failed', error: String(error) })
}
worker.on('message', (m) => m.type === 'ceiling' && settle(m))
worker.on('error', failed)
worker.on('exit', (code) => failed(`sampler exited (${code})`))
})
}
export function startWatchdog({ rootPid, ceilingBytes = CEILING_BYTES }) {
const shared = new SharedArrayBuffer(SLOT_COUNT * 4)
const stats = new Int32Array(shared)
const worker = new Worker(new URL('./watchdog-worker.mjs', import.meta.url), {
workerData: { rootPid, ceilingBytes, shared },
})
let stopping = false
const sampled = () => Atomics.load(stats, SLOT.samples) > 0
return {
ready: waitUntil(sampled, RESET_WAIT_MS, 'no first sample'),
stopped: stoppedPromise(worker, rootPid, stats, () => stopping),
snapshot: () => snapshotOf(stats),
resetPeak: () => resetPeak(stats),
stop: () => {
stopping = true
return worker.terminate()
},
}
}