`scripts/browser-probe/` (measurement only; no device or backend behavior changes). The frame-pacing arm of `gpu-probe.html` reported a mean fps and a single worst-frame figure. Neither can answer the question the probe exists for. "120 fps, perfectly even" and "120 fps with occasional stalls" produce the same mean, and one 40 ms stall and forty of them produce the same worst frame. The one measured number carried forward from this rig — Safari at 119.4 fps, worst frame 13 ms — is consistent with both a healthy display and a stuttering one, so it could not settle anything. The loop now keeps every frame delta and summarises the distribution: median/p95/p99, a long-frame count and percentage, dropped refreshes, the worst consecutive run of long frames, and a histogram bucketed in refresh intervals. The run of long frames is reported separately from their count because only a run is visible as a jerk; ten scattered drops and one ten-frame freeze are the same number and not the same defect. Two choices worth stating. The refresh interval is the *median* observed delta, not a hardcoded 1/120: this guest display has run at 60 and at 120, and a fixed budget misreports every frame on the other one. The median rather than the mean because it is the estimator a burst of long frames cannot drag with it. A frame counts as long past 1.5 intervals, the midpoint between one interval and two, which is where a delta stops being jitter around the budget and becomes a skipped vblank. The statistics moved into a DOM-free `summarizeDeltas` so they can be tested. `PROBE_SECONDS` widens the window through `web-gpu-probe.sh` (the settle wait scales with it, or a slow browser is read before it posts and scored silent); 5 s establishes a mean but can miss a stall that happens twice a minute. Tests: `node scripts/browser-probe/pacing-stats-test.mjs` — 20 checks, all passing. It slices the functions out of the page rather than restating them, so it exercises shipped code and not a copy that can drift. Mutation-verified by replacing the median with a hardcoded 120 Hz budget: 5 checks fail, and a healthy 60 Hz stream is then scored as dropping all 300 frames, which is the specific lie the median exists to prevent. Not verified: no live guest run yet — this commit changes the instrument, not the thing measured. Requires `node` for the test only; the probe page itself runs in the guest browser as before. Co-Authored-By: Claude <noreply@anthropic.com>
208 lines
8.9 KiB
HTML
208 lines
8.9 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head><meta charset="utf-8"><title>probe: starting</title></head>
|
|
<body style="margin:0;background:#111;color:#eee;font:13px monospace">
|
|
<pre id="out">running…</pre>
|
|
<canvas id="c" width="512" height="512" style="width:256px;height:256px"></canvas>
|
|
<script>
|
|
// Reports which of WebGL1 / WebGL2 / WebGPU actually come up, what renderer is
|
|
// behind each, and how evenly a trivial animated draw is paced.
|
|
//
|
|
// Pacing is reported as a distribution, not a mean: the question these probes
|
|
// exist to answer is whether frames are dropped *occasionally*, and a mean frame
|
|
// rate is blind to that by construction. `?seconds=N` widens the window.
|
|
//
|
|
// The result is written to document.title as well as to the page, because Safari
|
|
// exposes no debugging protocol here: AppleScript can read a document's name
|
|
// without the "Allow JavaScript from Apple Events" setting, so the title is the
|
|
// only channel that works in all three browsers.
|
|
//
|
|
// The draw loop is deliberately trivial (one clear + one triangle). It is
|
|
// measuring the presentation path, not shader throughput; anything heavier
|
|
// would confound "the compositor drops frames" with "the GPU is busy".
|
|
const lines = [];
|
|
const say = (s) => { lines.push(s); document.getElementById('out').textContent = lines.join('\n'); };
|
|
|
|
function rendererOf(gl) {
|
|
if (!gl) return null;
|
|
const dbg = gl.getExtension('WEBGL_debug_renderer_info');
|
|
return {
|
|
vendor: dbg ? gl.getParameter(dbg.UNMASKED_VENDOR_WEBGL) : gl.getParameter(gl.VENDOR),
|
|
renderer: dbg ? gl.getParameter(dbg.UNMASKED_RENDERER_WEBGL) : gl.getParameter(gl.RENDERER),
|
|
version: gl.getParameter(gl.VERSION),
|
|
maxTexture: gl.getParameter(gl.MAX_TEXTURE_SIZE),
|
|
};
|
|
}
|
|
|
|
const canvas = document.getElementById('c');
|
|
const results = { webgl1: null, webgl2: null, webgpu: null, fps: null };
|
|
|
|
// WebGL contexts are probed on throwaway canvases so the visible canvas stays
|
|
// free for whichever API is used to measure frame rate.
|
|
for (const [key, name] of [['webgl1', 'webgl'], ['webgl2', 'webgl2']]) {
|
|
try {
|
|
const t = document.createElement('canvas');
|
|
const gl = t.getContext(name, { failIfMajorPerformanceCaveat: false });
|
|
results[key] = gl ? rendererOf(gl) : 'NOT_AVAILABLE';
|
|
} catch (e) { results[key] = 'THREW: ' + e.message; }
|
|
}
|
|
say('webgl1: ' + JSON.stringify(results.webgl1));
|
|
say('webgl2: ' + JSON.stringify(results.webgl2));
|
|
|
|
// `failIfMajorPerformanceCaveat: true` is the direct question "is this a
|
|
// software renderer?" — a browser that fell back to software refuses the
|
|
// context outright rather than reporting it in a string.
|
|
try {
|
|
const t = document.createElement('canvas');
|
|
results.noCaveat = !!t.getContext('webgl', { failIfMajorPerformanceCaveat: true });
|
|
} catch (e) { results.noCaveat = false; }
|
|
say('hardware(no perf caveat): ' + results.noCaveat);
|
|
|
|
async function probeWebGPU() {
|
|
if (!navigator.gpu) return 'NO_navigator.gpu';
|
|
try {
|
|
const adapter = await navigator.gpu.requestAdapter();
|
|
if (!adapter) return 'NO_ADAPTER';
|
|
const info = adapter.info || (adapter.requestAdapterInfo ? await adapter.requestAdapterInfo() : {});
|
|
const dev = await adapter.requestDevice();
|
|
return {
|
|
vendor: info.vendor, architecture: info.architecture,
|
|
device: info.device, description: info.description,
|
|
hasDevice: !!dev,
|
|
features: dev ? [...dev.features].slice(0, 12) : [],
|
|
};
|
|
} catch (e) { return 'THREW: ' + e.message; }
|
|
}
|
|
|
|
// A mean frame rate cannot answer the question we actually have. "120 fps with
|
|
// occasional hitches" and "120 fps, perfectly even" produce the same mean, and
|
|
// a single worst-frame number cannot tell one 40 ms stall apart from forty of
|
|
// them. So keep every frame delta and report the distribution.
|
|
//
|
|
// The refresh interval is taken as the MEDIAN observed delta rather than a
|
|
// hardcoded 1/120: this rig's guest display has run at 60 and at 120, and a
|
|
// fixed budget would misreport every frame on the other one. The median is
|
|
// used instead of the mean because it is the one estimator a burst of long
|
|
// frames cannot drag with it.
|
|
//
|
|
// A "long" frame is one that consumed at least two refresh intervals, i.e. at
|
|
// least one frame was not shown. The 1.5x threshold is the midpoint between one
|
|
// interval and two, which is where a delta stops being jitter around the budget
|
|
// and starts being a skipped vblank.
|
|
const LONG_FRAME_INTERVALS = 1.5;
|
|
|
|
function quantile(sorted, q) {
|
|
if (!sorted.length) return 0;
|
|
const pos = (sorted.length - 1) * q;
|
|
const lo = Math.floor(pos);
|
|
const hi = Math.ceil(pos);
|
|
return sorted[lo] + (sorted[hi] - sorted[lo]) * (pos - lo);
|
|
}
|
|
|
|
// Pure: no DOM, no timers. Kept separate from the rAF loop so the statistics
|
|
// can be exercised against synthetic frame streams with a known answer — a
|
|
// summariser that miscounts hitches would otherwise be indistinguishable from a
|
|
// guest that has none.
|
|
function summarizeDeltas(deltas, elapsedSeconds, frames) {
|
|
const sorted = deltas.slice().sort((a, b) => a - b);
|
|
const median = quantile(sorted, 0.5);
|
|
const budget = median * LONG_FRAME_INTERVALS;
|
|
let longFrames = 0, worstRun = 0, run = 0, dropped = 0;
|
|
for (const d of deltas) {
|
|
if (d > budget) {
|
|
longFrames++;
|
|
// How many refreshes went unshown. A delta of exactly two intervals
|
|
// skipped one.
|
|
dropped += Math.max(1, Math.round(d / median) - 1);
|
|
run++;
|
|
if (run > worstRun) worstRun = run;
|
|
} else {
|
|
run = 0;
|
|
}
|
|
}
|
|
// Histogram in units of refresh intervals, so it reads the same at 60 and at
|
|
// 120 Hz. Bucket i counts frames that took i+1 intervals; the last bucket is
|
|
// everything at or beyond 6.
|
|
const hist = [0, 0, 0, 0, 0, 0];
|
|
for (const d of deltas) {
|
|
hist[Math.min(hist.length - 1, Math.max(0, Math.round(d / median) - 1))]++;
|
|
}
|
|
return {
|
|
fps: +(frames / elapsedSeconds).toFixed(1),
|
|
// Retained under its original name: existing callers and the page title
|
|
// read this key.
|
|
worst_frame_ms: +(sorted.length ? sorted[sorted.length - 1] : 0).toFixed(1),
|
|
frames,
|
|
seconds: +elapsedSeconds.toFixed(1),
|
|
refresh_hz: +(1000 / median).toFixed(1),
|
|
median_ms: +median.toFixed(2),
|
|
p95_ms: +quantile(sorted, 0.95).toFixed(2),
|
|
p99_ms: +quantile(sorted, 0.99).toFixed(2),
|
|
long_frames: longFrames,
|
|
long_frame_pct: +(100 * longFrames / (deltas.length || 1)).toFixed(2),
|
|
dropped_refreshes: dropped,
|
|
worst_hitch_frames: worstRun,
|
|
hist_intervals: hist,
|
|
};
|
|
}
|
|
|
|
function measureFps(seconds) {
|
|
return new Promise((resolve) => {
|
|
const gl = canvas.getContext('webgl2') || canvas.getContext('webgl');
|
|
// Frame deltas, in order. Order matters: a hitch is a RUN of long frames,
|
|
// and a sorted summary alone would report ten scattered stalls and one
|
|
// ten-frame freeze identically, though only the second is visible as a jerk.
|
|
const deltas = [];
|
|
let frames = 0, last = performance.now();
|
|
const t0 = last;
|
|
const step = () => {
|
|
const now = performance.now();
|
|
const dt = now - last; last = now;
|
|
// The first frames carry context setup and first-draw compilation, which
|
|
// is real cost but not a pacing defect; excluding them keeps the tail
|
|
// statistics about steady-state pacing.
|
|
if (frames > 2) deltas.push(dt);
|
|
frames++;
|
|
if (gl) {
|
|
gl.viewport(0, 0, canvas.width, canvas.height);
|
|
gl.clearColor((frames % 60) / 60, 0.2, 0.6, 1);
|
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
}
|
|
if (now - t0 < seconds * 1000) { requestAnimationFrame(step); return; }
|
|
resolve(summarizeDeltas(deltas, (now - t0) / 1000, frames));
|
|
};
|
|
requestAnimationFrame(step);
|
|
});
|
|
}
|
|
|
|
(async () => {
|
|
results.webgpu = await probeWebGPU();
|
|
say('webgpu: ' + JSON.stringify(results.webgpu));
|
|
// Five seconds is enough to establish a mean but not to see an *occasional*
|
|
// hitch; `?seconds=N` buys a longer window when the tail is the question.
|
|
const want = +new URLSearchParams(location.search).get('seconds');
|
|
results.fps = await measureFps(want > 0 ? want : 5);
|
|
say('fps: ' + JSON.stringify(results.fps));
|
|
const r1 = results.webgl1 && results.webgl1.renderer ? results.webgl1.renderer : String(results.webgl1);
|
|
const wg = typeof results.webgpu === 'string' ? results.webgpu : 'OK';
|
|
document.title = 'probe: gl=' + r1 + ' | hw=' + results.noCaveat +
|
|
' | webgpu=' + wg + ' | fps=' + results.fps.fps +
|
|
' | worst=' + results.fps.worst_frame_ms + 'ms' +
|
|
' | long=' + results.fps.long_frames +
|
|
' | hitch=' + results.fps.worst_hitch_frames;
|
|
window.__probe = results;
|
|
// Safari exposes no debugging protocol and AppleScript automation needs a
|
|
// consent click, so the page posts its own result back to the server that
|
|
// served it. Same-origin, so no permission is involved.
|
|
try {
|
|
await fetch('/report', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ ua: navigator.userAgent, results }),
|
|
});
|
|
} catch (e) { say('report failed: ' + e.message); }
|
|
say('DONE');
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|