// Release-web frame timings for a long chat transcript, over the capture kit. // // Build the fixture once per variant, from app/: // flutter build web --release --target tool/chat_composer_probe_entry.dart \ // --output build/chat-perf // then: // node tool/chat_perf_probe.mjs build/chat-perf [messages=300] [deltas=200] // // Two phases, each on a fresh page: wheel-scroll the transcript up and back // down, then stream deltas into a new reply. Flutter's own FrameTiming (build, // raster, total) arrives as console lines from tool/chat_perf_fixture.dart. // Prints one JSON report. Headless Chrome rasterises in software, so raster // numbers compare variants rather than predict a device. import { setTimeout as delay } from 'node:timers/promises'; import { openBrowser } from './capture/browser.mjs'; import { flutterPage } from './capture/page.mjs'; import { serveBuild } from './capture/serve.mjs'; const [dir, messagesArg = '300', deltasArg = '200'] = process.argv.slice(2); if (!dir) { console.error('usage: node tool/chat_perf_probe.mjs [messages] [deltas]'); process.exit(2); } const messages = Number(messagesArg); const deltas = Number(deltasArg); function stats(frames) { const pick = (i) => frames.map((f) => f[i]).sort((a, b) => a - b); const summary = (values) => values.length === 0 ? null : { meanMs: +(values.reduce((a, b) => a + b, 0) / values.length / 1000).toFixed(2), p50Ms: +(values[Math.floor(values.length / 2)] / 1000).toFixed(2), p90Ms: +(values[Math.floor(values.length * 0.9)] / 1000).toFixed(2), maxMs: +(values[values.length - 1] / 1000).toFixed(2), }; const total = pick(2); return { frames: frames.length, build: summary(pick(0)), raster: summary(pick(1)), total: summary(total), over16ms: total.filter((t) => t > 16667).length, }; } async function phase(origin, query, drive) { const browser = await openBrowser(); try { const page = flutterPage(browser, (m) => console.error(m)); let recording = false; let frames = []; const marks = []; browser.on('Runtime.consoleAPICalled', (p) => { const text = p.args.map((a) => a.value ?? '').join(' '); if (text.startsWith('PERF_FRAME ')) { if (recording) frames.push(text.split(' ').slice(1).map(Number)); } else if (text.startsWith('PERF_')) { marks.push(text); } }); const opened = Date.now(); try { await page.open(`${origin}/?${query}`, { width: 880, height: 1000 }); } catch (error) { throw new Error(`${error.message}; page errors: ${JSON.stringify(page.errors.slice(0, 5))}`); } const openMs = Date.now() - opened; await delay(1000); const control = { page, marks, start: () => { frames = []; recording = true; }, stop: () => { recording = false; return frames; }, }; const result = await drive(control); return { openMs, ...result, errors: page.errors.slice(0, 5) }; } finally { await browser.close(); } } const server = await serveBuild(dir); try { const scroll = await phase(server.origin, `perf=${messages}`, async ({ page, start, stop }) => { start(); for (const dy of [...Array(60).fill(-400), ...Array(60).fill(400)]) { await page.scroll(dy); await delay(50); } await delay(500); return { scroll: stats(stop()) }; }); const stream = await phase( server.origin, `perf=${messages}&stream=${deltas}`, async ({ start, stop, marks }) => { const begin = Date.now() + 15000; while (!marks.includes('PERF_STREAM_START') && Date.now() < begin) await delay(5); start(); const end = Date.now() + deltas * 40 + 15000; while (!marks.includes('PERF_STREAM_END') && Date.now() < end) await delay(100); await delay(500); return { streamed: marks.includes('PERF_STREAM_END'), stream: stats(stop()) }; }, ); console.log(JSON.stringify({ build: dir, messages, deltas, scroll, stream }, null, 2)); } finally { await server.close(); }