#!/usr/bin/env node // Frame-time bench: scrolls a long list behind the frosted bars of the // full-app fixture (tool/search_selection_probe_entry.dart) and reports the // browser's frame cadence. // // node tool/perf/glass_scroll_bench.mjs --build build/probe-app \ // --out /tmp/perf-glass/before [--gpu] [--size 560x900] [--seconds 6] \ // [--repeat 3] [--platform material|cupertino] [--theme light|dark] \ // [--shots] [--only runs-list|run-timeline] [--flags=""] // // Per scenario (the Runs list: 3 glass pieces; a run's Timeline tab: 5) it // opens the route, wheels the list one notch per animation frame for // --seconds, and records Flutter's frames: the interval between them, the // main-thread time each takes (build, layout, paint and rasterisation, up to // the next task) and how many 60 Hz vsyncs the intervals would miss. Chrome // runs with vsync and its frame-rate limit off, so an interval is a frame's // cost rather than a display tick. Without --gpu there is no WebGL and // CanvasKit rasterises on the CPU, where the blur cost shows (a proxy for a // weak GPU); with --gpu a desktop GPU usually hides it. It writes a // screenshot per scenario and `bench-*.json`, and prints the backdrop census // (tool/perf/backdrop_census.dart). --shots skips timing and writes a still at // a fixed scroll offset per scenario, for comparing the bars' pixels with // tool/perf/compare_shots.mjs. import { spawn } from 'node:child_process'; import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { parseArgs } from 'node:util'; import { setTimeout as delay } from 'node:timers/promises'; import { openBrowser } from '../capture/browser.mjs'; import { serveBuild } from '../capture/serve.mjs'; const { values: opts } = parseArgs({ options: { build: { type: 'string' }, out: { type: 'string' }, gpu: { type: 'boolean' }, size: { type: 'string', default: '560x900' }, seconds: { type: 'string', default: '6' }, repeat: { type: 'string', default: '3' }, platform: { type: 'string', default: 'material' }, theme: { type: 'string', default: 'light' }, only: { type: 'string' }, flags: { type: 'string', default: '' }, shots: { type: 'boolean' }, }, }); if (!opts.build || !opts.out) throw new Error('usage: --build --out [--gpu]'); const [width, height] = opts.size.split('x').map(Number); // Scenario: an app route plus the filler rows that make it long. const SCENARIOS = [ { name: 'runs-list', route: '/runs', extra: { runs: '400' } }, // The Timeline tab (second page tab, Material layout at 560 wide). { name: 'run-timeline', route: '/runs/7', extra: { steps: '400' }, click: [396, 853] }, ]; // Installed before the app starts. Flutter's own animation-frame callbacks // are timed (main-thread frame work); the driver wheels the list from the // page itself, one notch per frame, and notes each frame's timestamp, so the // cadence is the browser's and not a DevTools round trip. const RECORDER = `(() => { const raf = window.requestAnimationFrame.bind(window); const rec = { on: false, work: [], starts: [], ticks: [] }; window.__bench = rec; // Flutter rasterises after its frame callback returns (in microtasks), so a // frame's work runs until a message posted at the callback's end arrives. const channel = new MessageChannel(); const pending = []; channel.port1.onmessage = () => { const t0 = pending.shift(); if (rec.on) rec.work.push(performance.now() - t0); }; window.requestAnimationFrame = (cb) => raf((ts) => { const t0 = performance.now(); if (rec.on) rec.starts.push(t0); try { cb(ts); } finally { pending.push(t0); channel.port2.postMessage(0); } }); rec.drive = (ms, x, y, dy) => new Promise((done) => { rec.work = []; rec.starts = []; rec.ticks = []; rec.on = true; const end = performance.now() + ms; const tick = (ts) => { rec.ticks.push(ts); if (ts > end) { rec.on = false; return done({ ticks: rec.ticks, work: rec.work, starts: rec.starts }); } const target = document.elementFromPoint(x, y); target.dispatchEvent(new WheelEvent('wheel', { deltaY: dy, deltaMode: 0, clientX: x, clientY: y, screenX: x, screenY: y, bubbles: true, cancelable: true, composed: true })); raf(tick); }; raf(tick); }); })();`; async function launchChrome() { const profile = await mkdtemp(join(tmpdir(), 'pipelines-bench-chrome-')); const args = ['--headless=new', '--no-first-run', '--no-default-browser-check', '--hide-scrollbars', '--mute-audio', '--disable-frame-rate-limit', '--disable-gpu-vsync', '--disable-renderer-backgrounding', '--disable-background-timer-throttling', '--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank']; if (!opts.gpu) args.splice(3, 0, '--disable-gpu'); args.splice(3, 0, ...opts.flags.split(' ').filter(Boolean)); const chrome = spawn(process.env.CHROME_PATH ?? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', args, { stdio: ['ignore', 'ignore', 'pipe'] }); let stderr = ''; chrome.stderr.on('data', (d) => { stderr += d; }); for (let i = 0; i < 150 && !/DevTools listening on ws:\/\/127\.0\.0\.1:(\d+)/.test(stderr); i++) await delay(100); const port = stderr.match(/DevTools listening on ws:\/\/127\.0\.0\.1:(\d+)/)?.[1]; const close = async () => { chrome.kill('SIGKILL'); await rm(profile, { recursive: true, force: true }).catch(() => {}); }; if (!port) { await close(); throw new Error(`Chrome did not start: ${stderr.slice(-500)}`); } return { cdp: `http://127.0.0.1:${port}`, close }; } const pct = (sorted, p) => sorted[Math.min(sorted.length - 1, Math.floor(p * sorted.length))]; function summarise({ ticks, work, starts }) { const intervals = ticks.slice(1).map((t, i) => t - ticks[i]).sort((a, b) => a - b); const seconds = (ticks.at(-1) - ticks[0]) / 1000; const flutterIntervals = starts.slice(1).map((t, i) => t - starts[i]).sort((a, b) => a - b); work = [...work].sort((a, b) => a - b); const r = (x) => Math.round(x * 100) / 100; const stats = (xs) => ({ p50: r(pct(xs, 0.5)), p90: r(pct(xs, 0.9)), p99: r(pct(xs, 0.99)) }); return { frames: ticks.length, interval: stats(intervals), work: stats(work), flutterFrames: starts.length, flutterFps: r(starts.length / seconds), flutterInterval: stats(flutterIntervals), // Each extra vsync an interval spans is one dropped frame. dropped: intervals.reduce((n, d) => n + Math.max(0, Math.round(d / 16.67) - 1), 0), }; } await mkdir(opts.out, { recursive: true }); const server = await serveBuild(opts.build); const chrome = await launchChrome(); const browser = await openBrowser({ cdp: chrome.cdp }); const { send } = browser; const evaluate = async (expression) => (await send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true })).result.value; const results = { build: opts.build, gpu: Boolean(opts.gpu), size: opts.size, platform: opts.platform, theme: opts.theme, scenarios: {} }; try { await send('Page.enable'); await send('Runtime.enable'); await send('Page.bringToFront'); await send('Page.addScriptToEvaluateOnNewDocument', { source: RECORDER }); await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: false }); await send('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: opts.theme }] }); results.renderer = await evaluate(`(() => { const gl = document.createElement('canvas').getContext('webgl2'); return gl ? gl.getParameter(gl.getExtension('WEBGL_debug_renderer_info').UNMASKED_RENDERER_WEBGL) : 'none (CPU)'; })()`); console.log('renderer:', results.renderer); for (const s of SCENARIOS.filter((s) => !opts.only || s.name === opts.only)) { const runs = []; for (let rep = 0; rep < Number(opts.repeat); rep++) { const url = new URL('/', server.origin); url.searchParams.set('route', s.route); url.searchParams.set('platform', opts.platform); url.searchParams.set('theme', opts.theme); for (const [k, v] of Object.entries(s.extra)) url.searchParams.set(k, v); await send('Page.navigate', { url: url.href }); for (let i = 0; i < 300 && !(await evaluate("Boolean(document.querySelector('flt-glass-pane'))")); i++) await delay(100); await delay(2500); const x = width / 2; const y = height / 2; if (s.click) { for (const type of ['mouseMoved', 'mousePressed', 'mouseReleased']) { await send('Input.dispatchMouseEvent', { type, x: s.click[0], y: s.click[1], button: 'left', clickCount: 1 }); } await delay(1500); } await send('Input.dispatchMouseEvent', { type: 'mouseMoved', x, y }); await delay(300); const census = await evaluate('window.__backdropCensus?.()'); if (rep === 0) console.log(`${s.name} backdrops on screen:`, JSON.stringify(census)); if (opts.shots) { // A still at a fixed scroll offset, for before/after comparison. await send('Input.dispatchMouseEvent', { type: 'mouseWheel', x, y, deltaX: 0, deltaY: 250 }); await send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: 1, y: height / 2 }); await delay(1500); const still = (await send('Page.captureScreenshot', { format: 'png' })).data; await writeFile(join(opts.out, `${s.name}-${opts.platform}-${opts.theme}-still.png`), Buffer.from(still, 'base64')); break; } const frames = await evaluate(`window.__bench.drive(${Number(opts.seconds) * 1000}, ${x}, ${y}, 6)`); runs.push({ ...summarise(frames), census }); await delay(500); const shot = rep === 0 ? (await send('Page.captureScreenshot', { format: 'png' })).data : null; if (shot) await writeFile(join(opts.out, `${s.name}-${opts.platform}-${opts.theme}.png`), Buffer.from(shot, 'base64')); } results.scenarios[s.name] = runs; console.log(s.name, JSON.stringify(runs)); } if (!opts.shots) await writeFile(join(opts.out, `bench-${opts.gpu ? 'gpu' : 'sw'}-${opts.platform}-${opts.theme}.json`), JSON.stringify(results, null, 2)); } finally { await browser.close().catch(() => {}); await chrome.close(); await server.close(); }