seriesoftubes/scripts/perf-browser.js
Antoine 1e9b1d458b
GPU benchmark: isolate trials, time the card, say which map drew
- A trial now ends before the next starts: the clock stops, the frame
  callback is cancelled (and a queued one turned away), a day still in the
  route worker is waited for (or dropped), the clock's leftovers are cleared,
  and every trial starts from the same random numbers.
- The renderers take turns going first, scenario by scenario; ?reps=N runs
  each N times and reports medians.
- draw() is labelled as CPU submission time; the card's own time comes from
  EXT_disjoint_timer_query_webgl2 where available (read frames later, never
  waited for, disjoint results dropped); a run drawn by the other map is
  flagged.
- npm run perf --browser reads its sync pixel back from the surface the map
  was drawn on (the WebGL canvas waits for the card) and says which map each
  version drew with.
2026-11-04 22:37:13 +01:00

80 lines
5.8 KiB
JavaScript
Raw 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.

// The perf-bench workloads in real Chromium, for what jsdom cannot show: real DOM speed (jsdom is about 30×
// slower at it), layout (jsdom has none, so it cannot see a forced reflow) and canvas drawing. Two profiles: a
// desktop at full speed, and a phone (390×844, 3× pixel density, CPU slowed 4× like a mid-range Android).
// Used by scripts/perf-bench.js --browser. Needs Chromium: a local `playwright` with its browser, or
// `playwright-core` (a dev dependency) plus a Chrome binary in PERF_CHROME or --chrome (CI uses the runner's Chrome).
const { pathToFileURL } = require('node:url');
const PROFILES = [
{ key: 'desktop', label: 'Desktop', rate: 1, ctx: { viewport: { width: 1280, height: 800 }, deviceScaleFactor: 1 } },
{ key: 'phone', label: 'Phone (4× slower CPU)', rate: 4, ctx: { viewport: { width: 390, height: 844 }, deviceScaleFactor: 3, isMobile: true, hasTouch: true } },
];
function chromium(exe) {
// With a Chrome binary given, playwright-core is enough; without one, a full playwright brings its own browser.
for (const m of exe ? ['playwright-core', 'playwright'] : ['playwright', 'playwright-core']) {
try { return require(m).chromium; } catch (e) { /* try the next one */ }
}
throw new Error('--browser needs playwright-core (npm ci) and a Chrome binary in PERF_CHROME, or a local playwright');
}
const median = (a) => { const s = [...a].sort((x, y) => x - y); return s.length % 2 ? s[(s.length - 1) / 2] : (s[s.length / 2 - 1] + s[s.length / 2]) / 2; };
async function openPage(browser, profile, file, save, errors) {
const ctx = await browser.newContext(profile.ctx);
const page = await ctx.newPage();
// Nothing from the network (fonts, stats): both versions see exactly the same page, and no request adds noise.
await page.route('**/*', (r) => (r.request().url().startsWith('file:') ? r.continue() : r.abort()));
// Same as the tests: no animation loop or update checks running between measurements, and seeded randomness.
await page.addInitScript(() => { window.__TUBES_TEST__ = true; let r = 777; Math.random = () => { r = (r * 16807) % 2147483647; return r / 2147483647; }; });
page.on('pageerror', (e) => errors.push(String(e.message || e)));
await page.goto(pathToFileURL(file).href, { waitUntil: 'load' });
await page.evaluate((s) => { S = migrate(JSON.parse(s)); UI.started = true; UI.pitched = true; UI.speed = 0; closeModal(); compute(); renderAll(); }, save);
const cdp = await ctx.newCDPSession(page);
if (profile.rate > 1) await cdp.send('Emulation.setCPUThrottlingRate', { rate: profile.rate });
await cdp.send('HeapProfiler.enable');
// One run of a workload, timed inside the page so the round trip to Node does not count. Garbage is collected
// first, so a run never pays for the one before it (its own garbage still costs it, as in the game).
// The browser leaves layout and canvas drawing for the next frame, so code that changes the DOM or draws the map
// would look free: each run ends by forcing both (reading a size, and one pixel back from the surface the map was
// last drawn on: the WebGL canvas when the card drew it, which waits for the card to finish, else the 2D canvas).
// A run also says which map drew (gl, 2d, or none when it drew nothing).
const time = async (k, body) => { await cdp.send('HeapProfiler.collectGarbage'); return page.evaluate(([k, body]) => {
const F = window.__fx || (window.__fx = {}); const f = F[k] || (F[k] = new Function(body)); const map = document.getElementById('map').getContext('2d');
const G = typeof GLX === 'object' ? GLX : null, px = window.__px || (window.__px = new Uint8Array(4));
const t = performance.now(); f(); void document.documentElement.offsetHeight;
const gl = G && G.shown && G.gl; if (gl) gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, px); else map.getImageData(0, 0, 1, 1);
return { ms: performance.now() - t, map: G && G.shown ? 'gl' : '2d' }; }, [k, body]); };
return { ctx, time };
}
// Returns {version, profiles: {desktop: {compute: {head, base}, ...}, phone: {...}}, errors}.
async function runBrowser({ head, base, save, metrics, runs, exe }) {
// One browser per version: pages of one browser can share a renderer process and its heap, and then one
// version's garbage is collected while the other one is being timed.
const launch = () => chromium(exe).launch(exe ? { executablePath: exe } : {});
const hb = await launch(), bb = base ? await launch() : null;
const errors = [], out = { version: hb.version(), profiles: {}, errors };
try {
for (const p of PROFILES) {
const h = await openPage(hb, p, head, save, errors), b = base ? await openPage(bb, p, base, save, errors) : null;
const res = out.profiles[p.key] = {};
// Which map each version drew with, over all the runs that drew one (a version falling back mid-run shows both).
const maps = res.maps = { head: new Set(), base: new Set() };
for (const [k, , body] of metrics) {
const H = [], B = [], go = async (v, into, m) => { const r = await v.time(k, body); into.push(r.ms); if (k === 'map') m.add(r.map); };
for (const v of [h, b].filter(Boolean)) { await v.time(k, body); await v.time(k, body); } // warm up
for (let i = 0; i < runs; i++) {
// Alternate who goes first, like the jsdom runs.
if (b && i % 2) { await go(b, B, maps.base); await go(h, H, maps.head); } else { await go(h, H, maps.head); if (b) await go(b, B, maps.base); }
}
res[k] = { head: median(H), base: b ? median(B) : null, samples: { head: H.map((x) => +x.toFixed(1)), base: b ? B.map((x) => +x.toFixed(1)) : null } };
}
res.maps = { head: [...maps.head], base: b ? [...maps.base] : null };
await h.ctx.close(); if (b) await b.ctx.close();
}
} finally { await hb.close(); if (bb) await bb.close(); }
return out;
}
module.exports = { runBrowser, PROFILES };