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
Loading…
'; const st = (t) => { box.querySelector('#bst').textContent = t; }; - let SAVE = null, MD = ''; + let SAVE = null, MD = '', TRIAL = 0; + // Ends what the last trial left running before the next one starts, so nothing of one lands on the other: the clock + // stops, its frame callback is cancelled (measure() does that, and TRIAL turns away one already queued), and a day + // still being routed in the worker is waited for. Its answer runs dayPost() on the old save; one that never comes is + // dropped (rwDone() ignores an answer to no job). Then the clock's leftovers are cleared. + async function settle() { + UI.speed = 0; TRIAL++; + const t0 = performance.now(); + while (RW.job && performance.now() - t0 < SETTLE) await sleep(10); + if (RW.job) { console.warn('[bench] the route worker did not answer: its day is dropped'); RW.job = null; } + acc = 0; animT = 0; + } + // The save again, with the same random numbers every time (S.rng makes Math.random() follow it, as in a scenario). function fresh() { - S = migrate(JSON.parse(SAVE)); UI.started = true; UI.pitched = true; UI.sel = null; UI.anim = 'on'; applyAnim(); + S = migrate(JSON.parse(SAVE)); S.rng = SEED; UI.started = true; UI.pitched = true; UI.sel = null; UI.anim = 'on'; applyAnim(); closeModal(); compute(); renderAll(); const h = CITY[S.home] || CITIES[0]; cam.x = h.x; cam.y = h.y; } + async function reset() { await settle(); fresh(); } const q = (s, x) => (s.length ? s[Math.min(s.length - 1, Math.floor(s.length * x))] : 0); - async function measure(sc, hz) { - fresh(); cam.z = sc.z; UI.speed = sc.speed; WL.key = null; + async function measure(sc, hz, r) { + await reset(); cam.z = sc.z; UI.speed = sc.speed; WL.key = null; + const trial = TRIAL; await sleep(WARM); - const fr = [], dr = [], day0 = S.day; let last = 0, stop = false, hidden = false, long = 0, panX = cam.x; + const fr = [], dr = [], gpu = [], pend = [], day0 = S.day; let last = 0, stop = false, hidden = false, long = 0, panX = cam.x, raf = 0, glF = 0, flatF = 0, disjoint = 0; + // The card's own time, where the browser lets us read it: a timer around each WebGL draw(), read when it is ready. + const gl = r === 'gl' ? GLX.gl : null; let tq = null; try { tq = gl && gl.getExtension('EXT_disjoint_timer_query_webgl2'); } catch (e) { /* none */ } + const poll = () => { while (pend.length) { const x = pend[0]; if (!gl.getQueryParameter(x, gl.QUERY_RESULT_AVAILABLE)) break; pend.shift(); + const ns = gl.getQueryParameter(x, gl.QUERY_RESULT); gl.deleteQuery(x); if (gl.getParameter(tq.GPU_DISJOINT_EXT)) disjoint++; else gpu.push(ns / 1e6); } }; const D0 = draw; - draw = function (t) { const a = performance.now(); try { return D0(t); } finally { dr.push(performance.now() - a); } }; + draw = function (t) { let x = null; if (tq && GLX.gl === gl && pend.length < 16) { x = gl.createQuery(); gl.beginQuery(tq.TIME_ELAPSED_EXT, x); } + const a = performance.now(); + try { return D0(t); } finally { dr.push(performance.now() - a); if (x) { gl.endQuery(tq.TIME_ELAPSED_EXT); pend.push(x); } if (GLX.shown) glF++; else flatF++; } }; let po = null; try { po = new PerformanceObserver((l) => { long += l.getEntries().length; }); po.observe({ type: 'longtask' }); } catch (e) { /* not in this browser */ } // Frames slower than three refreshes, with the day they fell on and their draw(): where the stutters are. const t00 = performance.now(), slow = []; - const tick = (t) => { if (last) { fr.push(t - last); if (t - last > 50) slow.push({ at: Math.round(t - t00), ms: Math.round(t - last), day: S.day, draw: +(dr[dr.length - 1] || 0).toFixed(1) }); } last = t; if (document.hidden) hidden = true; + const tick = (t) => { if (stop || trial !== TRIAL) return; + if (last) { fr.push(t - last); if (t - last > 50) slow.push({ at: Math.round(t - t00), ms: Math.round(t - last), day: S.day, draw: +(dr[dr.length - 1] || 0).toFixed(1) }); } last = t; if (document.hidden) hidden = true; if (sc.pan) { panX += 0.18 * (t - (tick.p || t)) / cam.z; cam.x = panX; tick.p = t; } - if (!stop) requestAnimationFrame(tick); }; - requestAnimationFrame(tick); - const t0 = performance.now(); await sleep(SECS * 1000); stop = true; const T = (performance.now() - t0) / 1000; + if (tq) poll(); + raf = requestAnimationFrame(tick); }; + raf = requestAnimationFrame(tick); + const t0 = performance.now(); await sleep(SECS * 1000); stop = true; cancelAnimationFrame(raf); const T = (performance.now() - t0) / 1000; draw = D0; if (po) po.disconnect(); UI.speed = 0; - const s = fr.slice().sort((a, b) => a - b), d = dr.slice().sort((a, b) => a - b), budget = 1000 / hz; + if (tq) { poll(); for (const x of pend) gl.deleteQuery(x); } + const s = fr.slice().sort((a, b) => a - b), d = dr.slice().sort((a, b) => a - b), g = gpu.slice().sort((a, b) => a - b), budget = 1000 / hz; return { fps: +(fr.length / T).toFixed(1), p50: +q(s, 0.5).toFixed(1), p95: +q(s, 0.95).toFixed(1), p99: +q(s, 0.99).toFixed(1), max: +q(s, 1).toFixed(1), dropped: +(100 * fr.filter((x) => x > budget * 1.5).length / Math.max(1, fr.length)).toFixed(1), drawMs: +(d.reduce((a, x) => a + x, 0) / Math.max(1, d.length)).toFixed(2), drawP95: +q(d, 0.95).toFixed(2), + gpuMs: g.length ? +(g.reduce((a, x) => a + x, 0) / g.length).toFixed(2) : null, gpuP95: g.length ? +q(g, 0.95).toFixed(2) : null, gpuN: g.length, disjoint, + // Which map really drew the frames: a WebGL run that fell back to 2D (or a 2D run on the card) is not what it says. + drawn: glF && flatF ? 'mixed' : glF ? 'gl' : flatF ? '2d' : 'none', glFrames: glF, flatFrames: flatF, daysPerSec: +((S.day - day0) / T).toFixed(2), longTasks: long, hidden, drawMax: +q(d, 1).toFixed(1), slow }; } + // Several runs of one scenario: the median of each number; the flags and the stutters of all of them. + function merge(ms) { + if (ms.length === 1) return ms[0]; + const o = {}, med = (a) => { const s = a.slice().sort((x, y) => x - y); return s[(s.length - 1) >> 1]; }; + for (const k of Object.keys(ms[0])) { + const v = ms.map((m) => m[k]); + o[k] = k === 'slow' ? v.flat() : k === 'drawn' ? [...new Set(v)].join('+') : typeof v[0] === 'boolean' ? v.some(Boolean) : v.every((x) => typeof x === 'number') ? med(v) : v[0]; + } + o.runs = ms.length; return o; + } async function refresh() { const a = []; let l = 0; await new Promise((r) => { const f = (t) => { if (l) a.push(t - l); l = t; if (a.length < 90) requestAnimationFrame(f); else r(); }; requestAnimationFrame(f); }); a.sort((x, y) => x - y); return Math.round(1000 / a[a.length >> 1]); } function table(res, env) { - const rows = SCEN.map((sc) => { const a = res['2d'][sc.id], b = res.gl && res.gl[sc.id]; - const f = (m, k, u = '') => (m ? `${m[k]}${u}` : '-'); - return `| ${sc.label} | ${f(a, 'fps')} | ${f(b, 'fps')} | ${f(a, 'p95', ' ms')} | ${f(b, 'p95', ' ms')} | ${f(a, 'drawMs', ' ms')} | ${f(b, 'drawMs', ' ms')} |`; }); - return [`**${env.gpu || 'unknown GPU'}** · ${env.ua}`, `${env.w}×${env.h} at ${env.dpr}× · ${env.hz} Hz screen · ${env.cores} threads · ${env.save}`, '', - '| Scenario | 2D fps | WebGL fps | 2D p95 frame | WebGL p95 frame | 2D draw() | WebGL draw() |', - '|---|--:|--:|--:|--:|--:|--:|', ...rows, - '', `p95 frame: the slowest 5% of frames. draw(): CPU time per frame on the main thread.${env.invalid ? ' ⚠️ The tab was hidden during a run: those numbers are not valid.' : ''}`].join('\n'); + const gpu = res.gl && SCEN.some((sc) => res.gl[sc.id] && res.gl[sc.id].gpuMs != null); + const rows = SCEN.map((sc) => { const a = res['2d'] && res['2d'][sc.id], b = res.gl && res.gl[sc.id]; + const f = (m, k, u = '') => (m && m[k] != null ? `${m[k]}${u}` : '-'); + return `| ${sc.label} | ${f(a, 'fps')} | ${f(b, 'fps')} | ${f(a, 'p95', ' ms')} | ${f(b, 'p95', ' ms')} | ${f(a, 'drawMs', ' ms')} | ${f(b, 'drawMs', ' ms')} |${gpu ? ` ${f(b, 'gpuMs', ' ms')} |` : ''}`; }); + // A run is only what it says if its frames were drawn by the map it names. + const off = []; for (const [r, want] of [['2d', '2d'], ['gl', 'gl']]) for (const sc of SCEN) { const m = res[r] && res[r][sc.id]; if (m && m.drawn !== want && m.drawn !== 'none') off.push(`${sc.label} (${r === 'gl' ? 'WebGL' : '2D'}: ${m.flatFrames} frames in 2D, ${m.glFrames} on the card)`); } + return [`**${env.gpu || 'unknown GPU'}** · ${env.ua}`, `${env.w}×${env.h} at ${env.dpr}× · ${env.hz} Hz screen · ${env.cores} threads · ${env.save}${env.reps > 1 ? ` · median of ${env.reps} runs` : ''}`, '', + `| Scenario | 2D fps | WebGL fps | 2D p95 frame | WebGL p95 frame | 2D draw() | WebGL draw() |${gpu ? ' WebGL GPU |' : ''}`, + `|---|--:|--:|--:|--:|--:|--:|${gpu ? '--:|' : ''}`, ...rows, + '', `fps and p95 frame: requestAnimationFrame's cadence (p95: the slowest 5% of frames). draw(): CPU time on the main thread to build and submit a frame; the card may still be working when it returns, so it is not the cost of drawing.${gpu ? ' WebGL GPU: the card\'s own time per frame (EXT_disjoint_timer_query_webgl2).' : ' No GPU timer in this browser: the card\'s own time is not measured.'}${env.invalid ? ' ⚠️ The tab was hidden during a run: those numbers are not valid.' : ''}`, + ...(off.length ? ['', `⚠️ Not drawn by the map they name: ${off.join('; ')}.`] : [])].join('\n'); } async function run() { const go = box.querySelector('#bgo'); go.disabled = true; save = () => {}; // An incident pauses the game (autoPause): the clock must keep running for the timings to mean anything. autoPause = () => false; - fresh(); UI.speed = 0; st('Measuring the screen refresh…'); await sleep(500); + await reset(); st('Measuring the screen refresh…'); await sleep(500); const hz = await refresh(); - const want0 = GLX.want, glOk = mapGl(true, false) && glInit(); const env = { gpu: GLX.gpu, ua: navigator.userAgent.replace(/^Mozilla\/5\.0 /, ''), w: innerWidth, h: innerHeight, dpr: devicePixelRatio, hz, cores: navigator.hardwareConcurrency, save: `${Object.keys(S.pops).length} POPs · ${S.links.length} links` }; + const want0 = GLX.want, glOk = mapGl(true, false) && glInit(); const env = { gpu: GLX.gpu, ua: navigator.userAgent.replace(/^Mozilla\/5\.0 /, ''), w: innerWidth, h: innerHeight, dpr: devicePixelRatio, hz, cores: navigator.hardwareConcurrency, save: `${Object.keys(S.pops).length} POPs · ${S.links.length} links`, reps: REPS }; mapGl(false, false); - const res = { '2d': {}, gl: glOk ? {} : null }; - for (const r of (glOk ? ['2d', 'gl'] : ['2d']).filter((x) => !ONLY || x === ONLY)) { mapGl(r === 'gl', false); - for (const sc of SCEN) { st(`${r === 'gl' ? 'WebGL' : '2D'} · ${sc.label} (${SECS}s)… keep this tab in front`); const m = await measure(sc, hz); res[r][sc.id] = m; if (m.hidden) env.invalid = true; } } - mapGl(want0, false); fresh(); UI.speed = 0; + const rends = (glOk ? ['2d', 'gl'] : ['2d']).filter((x) => !ONLY || x === ONLY), all = {}; + // Scenario by scenario, both renderers in turn, the first one changing every time: neither always runs on a cold or + // a tired browser. + let n = 0; + for (let rep = 0; rep < REPS; rep++) for (const [i, sc] of SCEN.entries()) for (const r of (rep + i) % 2 ? rends.slice().reverse() : rends) { + mapGl(r === 'gl', false); n++; + st(`${r === 'gl' ? 'WebGL' : '2D'} · ${sc.label}${REPS > 1 ? ` · run ${rep + 1}/${REPS}` : ''} (${SECS}s, ${n}/${REPS * SCEN.length * rends.length})… keep this tab in front`); + const m = await measure(sc, hz, r); ((all[r] = all[r] || {})[sc.id] = all[r][sc.id] || []).push(m); if (m.hidden) env.invalid = true; + } + const res = { '2d': null, gl: null }; + for (const r of Object.keys(all)) { res[r] = {}; for (const id of Object.keys(all[r])) res[r][id] = merge(all[r][id]); } + mapGl(want0, false); await reset(); MD = table(res, env); st(glOk ? 'Done. The results are also in the terminal.' : 'Done (no WebGL2 here: 2D only).'); box.querySelector('#bout').innerHTML = '
' + 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 });
+});