From 1e9b1d458b6ac25e9f44e9ffcc1946eaf78f82f1 Mon Sep 17 00:00:00 2001 From: Antoine <20756073+antt0n@users.noreply.github.com> Date: Wed, 4 Nov 2026 14:09:22 +0100 Subject: [PATCH] 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. --- scripts/lib/gpu-bench-client.js | 113 ++++++++++++++++++++++++-------- scripts/perf-bench.js | 5 +- scripts/perf-browser.js | 16 +++-- tests/gpu-bench.test.js | 80 ++++++++++++++++++++++ 4 files changed, 181 insertions(+), 33 deletions(-) create mode 100644 tests/gpu-bench.test.js diff --git a/scripts/lib/gpu-bench-client.js b/scripts/lib/gpu-bench-client.js index 7c0f6bb..4fe4d42 100644 --- a/scripts/lib/gpu-bench-client.js +++ b/scripts/lib/gpu-bench-client.js @@ -2,8 +2,13 @@ // graphics card: the save is loaded, then each scenario plays for a few seconds with the 2D map and with the WebGL // tubes, and the frames are timed as they really reach the screen (requestAnimationFrame). Results show in the panel // and go back to the terminal that started the server. Nothing is saved: the page's save() is switched off. +// What each number is: the frame rate and p95 frame are the cadence of requestAnimationFrame (what you see); draw() is +// the main thread's time to build and submit a frame (CPU only: the card may still be drawing when it returns); GPU is +// the card's own time for a WebGL frame, from EXT_disjoint_timer_query_webgl2 where the browser offers it (read back +// frames later, never waited for, and thrown away when the card says its timing was disturbed). (() => { - const CFG = window.__BENCH__ || {}, SECS = CFG.secs || 6, WARM = 1500; + const CFG = window.__BENCH__ || {}, QS = new URLSearchParams(location.search); + const SECS = CFG.secs || 6, WARM = CFG.warm ?? 1500, SETTLE = CFG.settleMs || 5000, SEED = CFG.seed || 777; const SCEN = [ { id: 'x1-far', label: 'Map at x1, zoomed out (0.6)', speed: 1, z: 0.6 }, { id: 'x1-mid', label: 'Map at x1 (zoom 1.4)', speed: 1, z: 1.4 }, @@ -11,75 +16,126 @@ { id: 'pan', label: 'Panning at x1 (zoom 1.4)', speed: 1, z: 1.4, pan: true }, { id: 'x8', label: 'Game at x8 (zoom 1.4)', speed: 8, z: 1.4 }, ]; - // ?only=gl or ?only=2d runs one renderer; ?scen=x1-mid,pan runs only those scenarios; ?dpr=2 below. - const QS = new URLSearchParams(location.search), ONLY = QS.get('only'), PICK = QS.get('scen'); + // ?only=gl or ?only=2d runs one renderer; ?scen=x1-mid,pan runs only those scenarios; ?reps=3 runs each one three + // times (the renderers take turns going first, and the table shows the medians); ?dpr=2 below. + const ONLY = QS.get('only'), PICK = QS.get('scen'), REPS = Math.max(1, Math.min(9, +QS.get('reps') || CFG.reps || 1)); if (PICK) SCEN.splice(0, SCEN.length, ...SCEN.filter((s) => PICK.split(',').includes(s.id))); const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); const box = document.createElement('div'); box.style.cssText = 'position:fixed;top:8px;left:50%;transform:translateX(-50%);z-index:100000;background:rgba(30,38,64,.94);color:#fff;font:13px/1.4 system-ui,sans-serif;padding:12px 14px;border-radius:12px;max-width:94vw;max-height:80vh;overflow:auto;box-shadow:0 6px 24px rgba(0,0,0,.35)'; box.innerHTML = 'Series of Tubes · GPU benchmark
' + MD.replace(/&/g, '&').replace(/';
box.querySelector('#bcp').hidden = false; go.disabled = false; go.textContent = 'Run again';
try { await fetch('/__bench/results', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ env, res, md: MD }) }); } catch (e) { /* the panel has them */ }
}
+ // tests/gpu-bench.test.js drives the pieces without the panel.
+ if (CFG.expose) { window.__bench = { settle, reset, measure, merge, table, setSave: (s) => { SAVE = s; } }; return; }
addEventListener('load', async () => {
document.body.appendChild(box);
while (typeof migrate !== 'function' || typeof GLX === 'undefined') await sleep(50);
@@ -88,7 +144,8 @@
await sleep(800);
try { SAVE = JSON.stringify((await (await fetch('/__bench/save.json')).json()).S || null); } catch (e) { st('Could not load the save: ' + e); return; }
if (SAVE === 'null') { st('The save file has no game in it.'); return; }
- st(`Ready: ${SCEN.length} scenarios × 2 renderers, about ${Math.ceil(SCEN.length * 2 * (SECS + 3) / 60)} minutes. Keep this tab in front and do not touch the map.`);
+ const runs = SCEN.length * (ONLY ? 1 : 2) * REPS;
+ st(`Ready: ${SCEN.length} scenarios × ${ONLY ? 1 : 2} renderers${REPS > 1 ? ` × ${REPS}` : ''}, about ${Math.ceil(runs * (SECS + 3) / 60)} minutes. Keep this tab in front and do not touch the map.`);
const go = box.querySelector('#bgo'); go.disabled = false; go.onclick = () => run().catch((e) => st('Failed: ' + (e && e.stack || e)));
box.querySelector('#bcp').onclick = () => navigator.clipboard.writeText(MD).then(() => st('Copied.'), () => st('Copy failed: select the table instead.'));
if (/[?&]auto\b/.test(location.search)) go.onclick();
diff --git a/scripts/perf-bench.js b/scripts/perf-bench.js
index 272e9ac..2732cf2 100644
--- a/scripts/perf-bench.js
+++ b/scripts/perf-bench.js
@@ -98,7 +98,10 @@ md += `### Headless (jsdom)\n\nOur JavaScript only: jsdom has no layout and its
const slow = base ? slowIn(res, 2) : [];
const { PROFILES } = browser ? require('./perf-browser') : { PROFILES: [] };
for (const p of PROFILES) {
- md += `\n### Chromium ${browser.version}: ${p.key === 'phone' ? 'phone' : 'desktop'}\n\n${p.key === 'phone' ? 'A 390×844 screen at 3× pixel density, with the CPU slowed down 4× like a mid-range Android.' : 'A 1280×800 window at full speed.'} Real DOM, layout and canvas.\n\n` + table(browser.profiles[p.key], 2 * p.rate);
+ // The map row is the whole frame, up to a pixel read back from the surface that drew it; say which one that was.
+ const mp = browser.profiles[p.key].maps, mapName = (m) => (!m || !m.length ? 'no map' : m.map((x) => (x === 'gl' ? 'WebGL' : '2D canvas')).join(' then '));
+ const drew = mp ? ` The map was drawn with ${base ? `${mapName(mp.base)} (base) and ${mapName(mp.head)} (this PR)` : mapName(mp.head)}; its row waits for that surface to finish.` : '';
+ md += `\n### Chromium ${browser.version}: ${p.key === 'phone' ? 'phone' : 'desktop'}\n\n${p.key === 'phone' ? 'A 390×844 screen at 3× pixel density, with the CPU slowed down 4× like a mid-range Android.' : 'A 1280×800 window at full speed.'} Real DOM, layout and canvas.${drew}\n\n` + table(browser.profiles[p.key], 2 * p.rate);
if (base) slow.push(...slowIn(browser.profiles[p.key], 2 * p.rate).map((l) => `${l} (Chromium ${p.key})`));
}
if (base) {
diff --git a/scripts/perf-browser.js b/scripts/perf-browser.js
index a8a7123..01bf39e 100644
--- a/scripts/perf-browser.js
+++ b/scripts/perf-browser.js
@@ -36,10 +36,15 @@ async function openPage(browser, profile, file, save, errors) {
// 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 map canvas).
+ // 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 t = performance.now(); f(); void document.documentElement.offsetHeight; map.getImageData(0, 0, 1, 1); return performance.now() - t; }, [k, body]); };
+ 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 };
}
@@ -54,15 +59,18 @@ async function runBrowser({ head, base, save, metrics, runs, exe }) {
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 = [];
+ 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) { B.push(await b.time(k, body)); H.push(await h.time(k, body)); } else { H.push(await h.time(k, body)); if (b) B.push(await b.time(k, body)); }
+ 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(); }
diff --git a/tests/gpu-bench.test.js b/tests/gpu-bench.test.js
new file mode 100644
index 0000000..d72670c
--- /dev/null
+++ b/tests/gpu-bench.test.js
@@ -0,0 +1,80 @@
+// The GPU benchmark's panel (scripts/lib/gpu-bench-client.js): one trial must never reach into the next. A day the route
+// worker is still routing lands on the save it was routed for, a pan's frame callback stops with its trial, every
+// trial starts from the same random numbers, and the table says which map really drew each run.
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const fs = require('node:fs');
+const path = require('node:path');
+const { load } = require('./harness');
+
+const SAVE = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8');
+const CLIENT = fs.readFileSync(path.join(__dirname, '..', 'scripts', 'lib', 'gpu-bench-client.js'), 'utf8');
+
+function open(cfg = {}) {
+ const h = load({ seed: 777 });
+ h.ev(`window.__BENCH__=${JSON.stringify({ expose: true, secs: 0.15, warm: 0, settleMs: 300, ...cfg })};`);
+ h.ev(CLIENT);
+ h.ev(`__bench.setSave(${JSON.stringify(SAVE)});save=()=>{};`);
+ return h;
+}
+const wait = (ms) => new Promise((r) => setTimeout(r, ms));
+
+// A day in flight in the worker when the trial ends: rwDone() would run its dayPost() whenever the answer comes.
+const IN_FLIGHT = `window.__old=S;RW.seq=41;RW.job={seq:41,d:201,gen:RW.gen-1,t:0};window.__posts=[];
+ dayPost=function(d){__posts.push({d,old:S===__old});};`;
+
+test('a late answer from the route worker lands on the trial it belongs to, not the next one', async () => {
+ const { ev, errors } = open();
+ await ev('__bench.reset()');
+ ev(IN_FLIGHT);
+ ev('setTimeout(()=>rwDone({seq:41}),60)');
+ await ev('__bench.reset()');
+ assert.deepEqual(JSON.parse(ev('JSON.stringify(__posts)')), [{ d: 201, old: true }], 'its day ran on the old save, before the new one was loaded');
+ assert.equal(ev('S!==__old&&RW.job===null'), true);
+ assert.deepEqual(errors, []);
+});
+
+test('an answer that never comes is dropped, and coming late it changes nothing', async () => {
+ const { ev, errors } = open();
+ await ev('__bench.reset()');
+ ev(IN_FLIGHT);
+ ev('console.warn=()=>{}');
+ await ev('__bench.reset()');
+ assert.equal(ev('RW.job'), null, 'the next trial does not wait for it forever');
+ ev('rwDone({seq:41})');
+ assert.deepEqual(JSON.parse(ev('JSON.stringify(__posts)')), [], 'nothing ran on the new save');
+ assert.deepEqual(errors, []);
+});
+
+test('a pan stops with its trial: the next trial’s camera is left alone', async () => {
+ const { ev, errors } = open();
+ const m = JSON.parse(await ev('__bench.measure({id:"pan",pan:true,z:1.4,speed:0},60,"2d").then(JSON.stringify)'));
+ assert.ok(m.fps > 0, 'the pan ran');
+ ev('cam.x=0');
+ await wait(120);
+ assert.equal(ev('cam.x'), 0, 'no frame of the pan came after it');
+ const x8 = await ev('(async()=>{const p=__bench.measure({id:"x8",z:1.4,speed:8},60,"2d");await new Promise(r=>setTimeout(r,20));const x=cam.x;await p;return [x,cam.x];})()');
+ assert.equal(x8[0], x8[1], 'the next scenario keeps its camera where it put it');
+ assert.deepEqual(errors, []);
+});
+
+test('every trial starts from the same random numbers and an empty clock', async () => {
+ const { ev, errors } = open();
+ const draws = async () => { await ev('acc=12345;animT=999;__bench.reset()'); return ev('JSON.stringify([acc,animT,Math.random(),Math.random(),Math.random()])'); };
+ const a = await draws(), b = await draws();
+ assert.equal(a, b);
+ assert.deepEqual(JSON.parse(a).slice(0, 2), [0, 0]);
+ assert.deepEqual(errors, []);
+});
+
+test('the table says which map drew each run, and what each column measures', () => {
+ const { ev } = open();
+ const md = ev(`(()=>{const m=(drawn,gl,flat,gpu)=>({fps:60,p95:17,drawMs:1,drawn,glFrames:gl,flatFrames:flat,gpuMs:gpu});
+ const res={'2d':{'x1-mid':m('2d',0,300,null)},gl:{'x1-mid':m('mixed',12,288,0.8)}};
+ return __bench.table(res,{gpu:'stub',ua:'ua',w:1,h:1,dpr:1,hz:60,cores:1,save:'s',reps:1});})()`);
+ assert.match(md, /WebGL GPU/, 'a GPU column when the card timed its frames');
+ assert.match(md, /CPU time on the main thread/, 'draw() is said to be CPU time');
+ assert.match(md, /Not drawn by the map they name: Map at x1 \(zoom 1\.4\) \(WebGL: 288 frames in 2D/, 'a run that fell back to 2D is flagged');
+ const merged = JSON.parse(ev(`JSON.stringify(__bench.merge([{fps:50,drawn:'gl',hidden:false,slow:[1]},{fps:70,drawn:'gl',hidden:true,slow:[2]},{fps:60,drawn:'2d',hidden:false,slow:[]}]))`));
+ assert.deepEqual(merged, { fps: 60, drawn: 'gl+2d', hidden: true, slow: [1, 2], runs: 3 });
+});