Something went wrong. Try again.
doll.systems community website
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171(() => { if (window.__renderBenchmarkInstalled) return; window.__renderBenchmarkInstalled = true; const nativeFrame = window.requestAnimationFrame.bind(window); let sample; let nextNavigation = false; let output; let report = 'Production build; local instrumentation only.'; const publish = (text) => { report = text; if (output) output.textContent = text; }; const count = (name) => { if (sample) sample.calls[name] = (sample.calls[name] || 0) + 1; }; const draws = (gl) => { const label = gl.canvas.classList.contains('display-noise') ? 'dust' : 'art'; if (sample) { const times = sample.surfaces.get(gl.canvas) || { label, frames: [] }; const now = performance.now(); // Dust and its occasional spark share a canvas and a display frame. if (!times.frames.length || now - times.frames.at(-1) > 1) times.frames.push(now); sample.surfaces.set(gl.canvas, times); } return `${label}Draws`; }; const wrap = (prototype, name, label) => { const original = prototype[name]; prototype[name] = function (...args) { count(typeof label === 'function' ? label(this) : label); return original.apply(this, args); }; }; window.requestAnimationFrame = (callback) => nativeFrame((time) => { const active = sample; const start = performance.now(); try { callback(time); } finally { if (active && active === sample) active.callbacks.push(performance.now() - start); } }); wrap(Element.prototype, 'getBoundingClientRect', 'elementRectReads'); wrap(Range.prototype, 'getClientRects', 'textRectReads'); wrap(WebGLRenderingContext.prototype, 'drawArrays', draws); wrap(WebGLRenderingContext.prototype, 'texImage2D', 'textureDefinitions'); wrap(WebGLRenderingContext.prototype, 'texSubImage2D', 'textureUpdates'); wrap(CanvasRenderingContext2D.prototype, 'getImageData', 'pixelReadbacks'); wrap(HTMLCanvasElement.prototype, 'toDataURL', 'canvasEncodes'); wrap(CanvasRenderingContext2D.prototype, 'fill', 'canvasFills'); wrap(CanvasRenderingContext2D.prototype, 'drawImage', 'canvasImages'); const longTasksSupported = PerformanceObserver.supportedEntryTypes.includes('longtask'); if (longTasksSupported) new PerformanceObserver((list) => { if (sample) sample.longTasks.push( ...list .getEntries() .filter((entry) => entry.startTime >= sample.start) .map((entry) => entry.duration), ); }).observe({ type: 'longtask', buffered: false }); const stats = (values) => { const ordered = [...values].sort((a, b) => a - b); const at = (percentile) => ordered[Math.min(ordered.length - 1, Math.floor(ordered.length * percentile))] || 0; const round = (value) => Math.round(value * 100) / 100; return { count: ordered.length, median: round(at(0.5)), p95: round(at(0.95)), max: round(at(1)), total: round(ordered.reduce((a, b) => a + b, 0)), }; }; function start(mode = 'settled') { if (sample) return; const run = (sample = { start: performance.now(), mode, frames: [], callbacks: [], longTasks: [], calls: {}, surfaces: new Map(), }); const button = document.querySelector('#render-benchmark button'); if (button) button.disabled = true; publish('Measuring for 8 seconds…'); let previous; function frame(time) { if (sample !== run) return; if (previous !== undefined) run.frames.push(time - previous); previous = time; if (time - run.start < 8000) { nativeFrame(frame); return; } sample = undefined; const result = { mode: run.mode, path: location.pathname + location.hash, viewport: [innerWidth, innerHeight], scrollY, artwork: [...document.querySelectorAll('.layout-art .dither')].map((element) => ({ top: element.getBoundingClientRect().top, bottom: element.getBoundingClientRect().bottom, })), duration: Math.round(time - run.start), visibility: document.visibilityState, frameMs: stats(run.frames), surfaceFrameMs: [...run.surfaces.values()].map(({ label, frames }) => ({ label, firstDrawMs: Math.round(frames[0] - run.start), ...stats(frames.slice(1).map((time, index) => time - frames[index])), })), framesOver50ms: run.frames.filter((time) => time > 50).length, callbackMs: stats(run.callbacks), longTasks: longTasksSupported ? stats(run.longTasks) : 'unsupported', calls: run.calls, }; publish(JSON.stringify(result, null, 2)); const button = document.querySelector('#render-benchmark button'); if (button) button.disabled = false; } nativeFrame(frame); } function mount() { if (document.querySelector('#render-benchmark')) return; const panel = document.createElement('aside'); panel.id = 'render-benchmark'; panel.style.cssText = 'position:fixed;z-index:10000;right:8px;bottom:8px;width:280px;background:#111;border:1px solid #888;padding:12px;color:white;font:12px/1.3 monospace;text-shadow:none'; const button = document.createElement('button'); button.textContent = 'Measure rendering · 8 seconds'; button.disabled = !!sample; button.style.cssText = 'font:inherit;color:white;border:1px solid #888;padding:8px;cursor:pointer'; button.addEventListener('click', () => start()); const navigation = document.createElement('button'); navigation.textContent = 'Measure next navigation'; navigation.style.cssText = button.style.cssText + ';margin-top:8px'; navigation.addEventListener('click', () => { nextNavigation = true; publish('Follow an internal link to measure the next page swap.'); }); output = document.createElement('pre'); output.style.cssText = 'white-space:pre-wrap;max-height:200px;overflow:auto;margin:8px 0 0'; output.textContent = report; const baseline = document.createElement('a'); baseline.href = '/__baseline/'; baseline.textContent = 'Browser baseline'; baseline.style.cssText = 'display:block;color:#c79fef;margin-top:8px'; panel.append(button, navigation, baseline, output); document.body.append(panel); } document.addEventListener('astro:before-swap', () => { sample = undefined; publish('Production build; local instrumentation only.'); }); document.addEventListener('DOMContentLoaded', mount); document.addEventListener('astro:after-swap', () => { if (!nextNavigation) return; nextNavigation = false; start('navigation'); }); document.addEventListener('astro:page-load', mount); if (new URL(location.href).searchParams.get('benchmark') === 'startup') start('startup');})();