test(tests): R4.9 memory bench: linear memory, renderer RSS, live canvases/images/blobs/object URLs, transient peaks and rest windows per export, three engines; raster-only control and Chromium pressure step; first run results
This commit is contained in:
1 parent
6166eba5f1
commit
ecc8699074
4 files changed
+4997
-6
No files matched your search
@@ -0,0 +1,251 @@
|
||||
/**
|
||||
* The memory bench's readings (R4.9): what the export keeps, told apart.
|
||||
* (1) the module's linear memory, read on the `WebAssembly.Memory` the
|
||||
* page instantiated (held through a WeakRef, so the probe retains no dead
|
||||
* instance); (3) the objects the rasteriser and the download create
|
||||
* (canvases, images, bitmaps, blobs, the PDF blob) counted at creation and
|
||||
* at collection through a FinalizationRegistry, the object URLs created
|
||||
* and revoked; Chromium adds the JS heap, the DOM counters and a heap
|
||||
* snapshot census of the same classes. (2) RSS and (4) peaks come from
|
||||
* the watchdog, (5) the post-export windows from the bench's protocol.
|
||||
* `installMemoryProbes` is serialised into the page by Playwright: it must
|
||||
* not reference anything outside its own body.
|
||||
*/
|
||||
|
||||
export const KINDS = ['canvas', 'image', 'bitmap', 'blob', 'pdfBlob']
|
||||
|
||||
export function installMemoryProbes() {
|
||||
const kinds = ['canvas', 'image', 'bitmap', 'blob', 'pdfBlob']
|
||||
const created = Object.fromEntries(kinds.map((k) => [k, 0]))
|
||||
const collected = Object.fromEntries(kinds.map((k) => [k, 0]))
|
||||
const registry = new FinalizationRegistry((kind) => {
|
||||
collected[kind] += 1
|
||||
})
|
||||
const track = (object, kind) => {
|
||||
created[kind] += 1
|
||||
registry.register(object, kind)
|
||||
return object
|
||||
}
|
||||
const memories = []
|
||||
const urls = { created: 0, revoked: 0 }
|
||||
// The raster control replays two view drawings (a logo is under 64 KB).
|
||||
const svgSamples = []
|
||||
const VIEW_SVG_MIN_CHARS = 64 * 1024
|
||||
const keepSvg = (parts, options) => {
|
||||
const text = parts?.[0]
|
||||
if (svgSamples.length >= 2 || typeof text !== 'string') return
|
||||
const isSvg = options?.type?.startsWith('image/svg+xml')
|
||||
if (isSvg && text.length >= VIEW_SVG_MIN_CHARS) svgSamples.push(text)
|
||||
}
|
||||
|
||||
function keepMemory(result) {
|
||||
const instance =
|
||||
result instanceof WebAssembly.Instance ? result : result?.instance
|
||||
const memory = instance?.exports?.memory
|
||||
if (memory instanceof WebAssembly.Memory) memories.push(new WeakRef(memory))
|
||||
return result
|
||||
}
|
||||
function wrapInstantiation() {
|
||||
const instantiate = WebAssembly.instantiate
|
||||
WebAssembly.instantiate = function (...args) {
|
||||
return instantiate.apply(WebAssembly, args).then(keepMemory)
|
||||
}
|
||||
const streaming = WebAssembly.instantiateStreaming
|
||||
if (!streaming) return
|
||||
WebAssembly.instantiateStreaming = function (...args) {
|
||||
return streaming.apply(WebAssembly, args).then(keepMemory)
|
||||
}
|
||||
}
|
||||
function wrapElements() {
|
||||
const createElement = Document.prototype.createElement
|
||||
Document.prototype.createElement = function (tag, ...rest) {
|
||||
const element = createElement.call(this, tag, ...rest)
|
||||
const name = String(tag).toLowerCase()
|
||||
if (name === 'canvas') track(element, 'canvas')
|
||||
else if (name === 'img') track(element, 'image')
|
||||
return element
|
||||
}
|
||||
const NativeImage = window.Image
|
||||
window.Image = function Image(...args) {
|
||||
return track(new NativeImage(...args), 'image')
|
||||
}
|
||||
window.Image.prototype = NativeImage.prototype
|
||||
}
|
||||
function wrapBitmaps() {
|
||||
const create = window.createImageBitmap
|
||||
if (!create) return
|
||||
window.createImageBitmap = function (...args) {
|
||||
return create.apply(window, args).then((b) => track(b, 'bitmap'))
|
||||
}
|
||||
}
|
||||
function wrapBlobs() {
|
||||
const NativeBlob = window.Blob
|
||||
window.Blob = class Blob extends NativeBlob {
|
||||
constructor(parts, options) {
|
||||
super(parts, options)
|
||||
keepSvg(parts, options)
|
||||
track(this, options?.type === 'application/pdf' ? 'pdfBlob' : 'blob')
|
||||
}
|
||||
}
|
||||
const create = URL.createObjectURL
|
||||
URL.createObjectURL = function (object) {
|
||||
urls.created += 1
|
||||
return create.call(URL, object)
|
||||
}
|
||||
const revoke = URL.revokeObjectURL
|
||||
URL.revokeObjectURL = function (url) {
|
||||
urls.revoked += 1
|
||||
return revoke.call(URL, url)
|
||||
}
|
||||
}
|
||||
|
||||
wrapInstantiation()
|
||||
wrapElements()
|
||||
wrapBitmaps()
|
||||
wrapBlobs()
|
||||
window.__memBench = {
|
||||
svgSamples,
|
||||
read: () => {
|
||||
const alive = memories.map((ref) => ref.deref()).filter(Boolean)
|
||||
const heap = performance.memory
|
||||
return {
|
||||
linearBytes: alive.reduce((s, m) => s + m.buffer.byteLength, 0),
|
||||
instances: alive.length,
|
||||
instancesEver: memories.length,
|
||||
created: { ...created },
|
||||
collected: { ...collected },
|
||||
urls: { ...urls },
|
||||
jsHeap: heap
|
||||
? { used: heap.usedJSHeapSize, total: heap.totalJSHeapSize }
|
||||
: null,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** The page's counters (structured clone). */
|
||||
export const readMemoryProbe = () => window.__memBench.read()
|
||||
|
||||
// Heap snapshot node names worth a census: V8 names DOM wrappers by class
|
||||
// (detached ones with a `Detached ` prefix) and a wasm memory's bytes sit in
|
||||
// its backing store node. Exact names, on object / native / hidden nodes
|
||||
// only: code and closure nodes carry source text and function names.
|
||||
const CENSUS = new Set([
|
||||
'HTMLCanvasElement',
|
||||
'Detached HTMLCanvasElement',
|
||||
'OffscreenCanvas',
|
||||
'CanvasRenderingContext2D',
|
||||
'HTMLImageElement',
|
||||
'Detached HTMLImageElement',
|
||||
'ImageBitmap',
|
||||
'ImageData',
|
||||
'Blob',
|
||||
'ArrayBuffer',
|
||||
'Uint8Array',
|
||||
'WebAssembly.Memory',
|
||||
'system / JSArrayBufferData',
|
||||
])
|
||||
const CENSUS_TYPES = new Set(['object', 'native', 'hidden'])
|
||||
|
||||
function censusOf(snapshot) {
|
||||
const { node_fields: fields, node_types: types } = snapshot.snapshot.meta
|
||||
const typeIx = fields.indexOf('type')
|
||||
const nameIx = fields.indexOf('name')
|
||||
const sizeIx = fields.indexOf('self_size')
|
||||
const stride = fields.length
|
||||
const typeNames = types[typeIx]
|
||||
const { nodes, strings } = snapshot
|
||||
const census = {}
|
||||
for (let i = 0; i < nodes.length; i += stride) {
|
||||
const name = strings[nodes[i + nameIx]]
|
||||
if (!CENSUS.has(name)) continue
|
||||
const type = typeNames[nodes[i + typeIx]]
|
||||
if (!CENSUS_TYPES.has(type)) continue
|
||||
const key = `${name} (${type})`
|
||||
const entry = (census[key] ??= { count: 0, selfBytes: 0 })
|
||||
entry.count += 1
|
||||
entry.selfBytes += nodes[i + sizeIx]
|
||||
}
|
||||
return { totalNodes: nodes.length / stride, census }
|
||||
}
|
||||
|
||||
/** Chromium: a heap snapshot through CDP, reduced to the census above. */
|
||||
export async function heapCensus(cdp) {
|
||||
const chunks = []
|
||||
const onChunk = (event) => chunks.push(event.chunk)
|
||||
cdp.on('HeapProfiler.addHeapSnapshotChunk', onChunk)
|
||||
try {
|
||||
await cdp.send('HeapProfiler.takeHeapSnapshot', {
|
||||
reportProgress: false,
|
||||
treatGlobalObjectsAsRoots: true,
|
||||
captureNumericValue: false,
|
||||
})
|
||||
} finally {
|
||||
cdp.off('HeapProfiler.addHeapSnapshotChunk', onChunk)
|
||||
}
|
||||
const text = chunks.join('')
|
||||
return { snapshotBytes: text.length, ...censusOf(JSON.parse(text)) }
|
||||
}
|
||||
|
||||
const METRICS = [
|
||||
'Documents',
|
||||
'Nodes',
|
||||
'JSEventListeners',
|
||||
'JSHeapUsedSize',
|
||||
'JSHeapTotalSize',
|
||||
]
|
||||
|
||||
/** Chromium: the renderer's DOM and heap counters (`Performance.getMetrics`). */
|
||||
export async function domMetrics(cdp) {
|
||||
const { metrics } = await cdp.send('Performance.getMetrics')
|
||||
const picked = {}
|
||||
for (const { name, value } of metrics) {
|
||||
if (METRICS.includes(name)) picked[name] = value
|
||||
}
|
||||
return picked
|
||||
}
|
||||
|
||||
/**
|
||||
* The raster control: the rasteriser's own steps (browserRasterizer.ts:
|
||||
* size stamped on the root, an SVG Blob URL, a sync-decoded Image, a white
|
||||
* canvas, toBlob, the bytes read) on the two view drawings the init export
|
||||
* drew, at the sizes it drew, `count` times; the bytes are dropped (no core
|
||||
* call). Serialised into the page: self-contained.
|
||||
*/
|
||||
export async function rasterControl({ sizes }) {
|
||||
const samples = window.__memBench.svgSamples
|
||||
if (samples.length === 0) throw new Error('no view drawing captured')
|
||||
const SVG_NS = 'http://www.w3.org/2000/svg'
|
||||
for (let i = 0; i < sizes.length; i += 1) {
|
||||
const { width, height } = sizes[i]
|
||||
const root = new DOMParser().parseFromString(
|
||||
samples[i % samples.length],
|
||||
'image/svg+xml',
|
||||
).documentElement
|
||||
if (root.namespaceURI !== SVG_NS) root.setAttribute('xmlns', SVG_NS)
|
||||
root.setAttribute('width', String(width))
|
||||
root.setAttribute('height', String(height))
|
||||
const svg = new XMLSerializer().serializeToString(root)
|
||||
const url = URL.createObjectURL(
|
||||
new Blob([svg], { type: 'image/svg+xml;charset=utf-8' }),
|
||||
)
|
||||
const img = new Image()
|
||||
img.decoding = 'sync'
|
||||
img.crossOrigin = 'anonymous'
|
||||
await new Promise((resolve, reject) => {
|
||||
img.onload = resolve
|
||||
img.onerror = reject
|
||||
img.src = url
|
||||
}).finally(() => URL.revokeObjectURL(url))
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = width
|
||||
canvas.height = height
|
||||
const ctx = canvas.getContext('2d')
|
||||
ctx.fillStyle = '#ffffff'
|
||||
ctx.fillRect(0, 0, width, height)
|
||||
ctx.drawImage(img, 0, 0, width, height)
|
||||
const blob = await new Promise((r) => canvas.toBlob(r, 'image/png'))
|
||||
await blob.arrayBuffer()
|
||||
}
|
||||
return sizes.length
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
import { readFileSync, 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, tagged, timeoutAfter } from './lib/async.mjs'
|
||||
import { hostFacts } from './lib/host.mjs'
|
||||
@@ -40,13 +41,13 @@ import { deltasOf } from './r4-leak-series.mjs'
|
||||
import { startServer } from './serve.mjs'
|
||||
|
||||
const MINUTE = 60_000
|
||||
const EXPORT_LABEL = 'Exporter en PDF' // fr.ts editor.export
|
||||
export const EXPORT_LABEL = 'Exporter en PDF' // fr.ts editor.export
|
||||
const CANVAS = '.svg-canvas [data-anno-id]'
|
||||
const LOAD_TIMEOUT_MS = 2 * MINUTE
|
||||
const BETWEEN_MS = 2000
|
||||
const FINAL_IDLE_MS = 5000
|
||||
const TEN_MIB = 10 * 1024 * 1024
|
||||
const REPORTS = {
|
||||
export const REPORTS = {
|
||||
demo: { file: 'report-v3.demo.json', timeoutMs: 5 * MINUTE },
|
||||
L: { file: 'r4/reports/L.json', timeoutMs: 30 * MINUTE },
|
||||
}
|
||||
@@ -91,12 +92,14 @@ function draftOf(name) {
|
||||
return JSON.stringify(JSON.parse(text))
|
||||
}
|
||||
|
||||
async function openEditor(session, baseUrl, name) {
|
||||
/** One editor tab seeded with `name`; `init` = extra init scripts (the memory bench). */
|
||||
export async function openEditor(session, baseUrl, name, init = []) {
|
||||
const context = await session.browser.newContext({
|
||||
acceptDownloads: true,
|
||||
locale: 'fr-FR',
|
||||
})
|
||||
const seed = { draft: draftOf(name), locale: 'fr', skipDisclaimer: true }
|
||||
for (const script of init) await context.addInitScript(script)
|
||||
const { page, events } = await probedPage(context, seed)
|
||||
const cdp =
|
||||
session.engine === 'chromium' ? await context.newCDPSession(page) : null
|
||||
@@ -113,12 +116,15 @@ async function reading(session, tab) {
|
||||
}
|
||||
|
||||
/** One export: click, the download, the button enabled again; its timing entry. */
|
||||
async function exportOnce(tab, timeoutMs) {
|
||||
export async function exportOnce(tab, timeoutMs) {
|
||||
const download = tab.page.waitForEvent('download', { timeout: 0 })
|
||||
download.catch(() => undefined)
|
||||
const started = performance.now()
|
||||
const end = await Promise.race([
|
||||
tagged('downloaded', tab.button.click().then(() => download)),
|
||||
tagged(
|
||||
'downloaded',
|
||||
tab.button.click().then(() => download),
|
||||
),
|
||||
timeoutAfter(timeoutMs),
|
||||
])
|
||||
if (end.kind !== 'downloaded') {
|
||||
@@ -270,4 +276,4 @@ async function main() {
|
||||
console.log(table(runs))
|
||||
}
|
||||
|
||||
await main()
|
||||
if (import.meta.url === pathToFileURL(process.argv[1]).href) await main()
|
||||
@@ -0,0 +1,368 @@
|
||||
// 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 <file>]
|
||||
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()
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user