#!/usr/bin/env node // Streams generated output through the terminal bench build // (tool/perf/terminal_stream_bench.dart) in headless Chrome and prints one // JSON line per workload: Flutter frame build/raster quantiles, rAF // intervals measured in the page, and the throughput reached. // // node tool/perf/terminal_stream_bench.mjs --build build/terminal-bench \ // [--workloads seq,yes,unicode,colors,screen,progress] [--mb 30] [--rate 30] [--size 1280x800] 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: options } = parseArgs({ options: { build: { type: 'string' }, workloads: { type: 'string', default: 'seq,yes,unicode,colors,screen,progress' }, mb: { type: 'string', default: '30' }, rate: { type: 'string', default: '30' }, size: { type: 'string', default: '1280x800' }, timeout: { type: 'string', default: '120000' }, params: { type: 'string', default: '' }, }, }); if (!options.build) throw new Error('--build required'); const [width, height] = options.size.split('x').map(Number); const quantiles = (values) => { const sorted = [...values].sort((a, b) => a - b); const at = (q) => sorted[Math.round(q * (sorted.length - 1))] ?? 0; const r = (v) => Math.round(v * 100) / 100; return { p50: r(at(0.5)), p90: r(at(0.9)), p99: r(at(0.99)), max: r(sorted.at(-1) ?? 0) }; }; const server = await serveBuild(options.build); const browser = await openBrowser(); const evaluate = async (expression) => { const r = await browser.send('Runtime.evaluate', { expression, returnByValue: true }); return r.result.value; }; try { await browser.send('Page.enable'); await browser.send('Runtime.enable'); for (const workload of options.workloads.split(',')) { const url = new URL('/', server.origin); url.searchParams.set('workload', workload); url.searchParams.set('mb', options.mb); url.searchParams.set('rate', options.rate); for (const [k, v] of new URLSearchParams(options.params)) url.searchParams.set(k, v); await browser.send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: false }); await browser.send('Page.navigate', { url: url.href }); await delay(300); await evaluate(`(() => { window.__raf = []; let last = performance.now(); const tick = (t) => { window.__raf.push(t - last); last = t; if (!window.__benchResult) requestAnimationFrame(tick); }; requestAnimationFrame(tick); return true; })()`); const deadline = Date.now() + Number(options.timeout); let result = null; while (!result && Date.now() < deadline) { await delay(500); result = await evaluate('window.__benchResult ?? null'); } if (!result) throw new Error(`${workload}: no result before the timeout`); const raf = await evaluate('window.__raf'); // Skip the start-up frames before streaming starts. console.log(JSON.stringify({ ...JSON.parse(result), pageRaf: quantiles(raf.slice(50)) })); } } finally { await browser.close?.(); await server.close?.(); }