#!/usr/bin/env node // Repeated warmed touch switches over the full-app rich-chat fixture. // Use a Flutter --profile --source-maps build of search_selection_probe_entry.dart: // node tool/perf/mobile_motion_bench.mjs --build build/probe-motion-profile \ // --out /tmp/motion --style cardStack --style-label 'Card deck' --repeat 3 // Records Flutter frame timings, V8 CPU samples, browser cadence and long tasks. // Chrome uses software rasterization: relative comparisons, not device FPS. // The saved style is checked in Settings before every measured chat run. import { mkdir, writeFile } from 'node:fs/promises'; 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'; import { flutterPage } from '../capture/page.mjs'; import { attribute, loadSourceMap } from './cpu_attribution.mjs'; import { frameMetrics, quantile, round } from './mobile_motion_metrics.mjs'; const { values: options } = parseArgs({ options: { build: { type: 'string' }, style: { type: 'string', default: 'tape' }, 'style-label': { type: 'string' }, 'cpu-rate': { type: 'string', default: '1' }, out: { type: 'string' }, repeat: { type: 'string', default: '3' }, switches: { type: 'string', default: '10' }, cadence: { type: 'string', default: '850' }, platform: { type: 'string', default: 'material' }, messages: { type: 'string', default: '120' }, }, }); if (!options.build || !options.out) throw new Error('--build and --out required'); // The peek always identifies the other retained pane after each switch. const peekTarget = `eval:(() => { const element = document.querySelector('[flt-semantics-identifier^="chat-peek-"]'); if (!element) return null; const rect = element.getBoundingClientRect(); return [rect.x + rect.width / 2, rect.y + rect.height / 2]; })()`; // Supports both the previous plain picker and the native rows' richer labels. const secondChatTarget = `eval:(() => { const nodes = [...document.querySelectorAll('flt-semantics, [aria-label]')]; const hits = nodes.filter((element) => { const label = element.getAttribute('aria-label') || element.innerText || ''; const rect = element.getBoundingClientRect(); return /Fixture chat 2(?:\\s|$)/.test(label) && rect.width > 0 && rect.height > 0 && rect.top < innerHeight; }).sort((a, b) => { const ra = a.getBoundingClientRect(), rb = b.getBoundingClientRect(); return ra.width * ra.height - rb.width * rb.height; }); if (!hits.length) return null; const rect = hits[0].getBoundingClientRect(); return [rect.x + rect.width / 2, rect.y + rect.height / 2]; })()`; await mkdir(options.out, { recursive: true }); const lookup = await loadSourceMap(options.build); const server = await serveBuild(options.build); const browser = await openBrowser(); const page = flutterPage(browser, () => {}, { touch: true }); const frames = []; const runs = []; browser.on('Runtime.consoleAPICalled', (event) => { const line = event.args.map((argument) => argument.value ?? '').join(' '); if (line.startsWith('PERF_FRAME ')) { frames.push(line.split(' ').slice(1).map(Number)); } }); try { await browser.send('Page.enable'); await browser.send('Runtime.enable'); await browser.send('Page.addScriptToEvaluateOnNewDocument', { source: `try { localStorage.clear(); sessionStorage.clear(); localStorage.setItem('flutter.pipelines.quick-switch-animation.v1', JSON.stringify(${JSON.stringify(options.style)})); } catch (_) {}`, }); for (let repetition = 0; repetition < Number(options.repeat); repetition++) { const url = new URL('/', server.origin); const fixture = { route: '/chat/perf-chat-0', platform: options.platform, theme: 'light', chats: '30', msgs: options.messages, rich: '1', perf: '1', }; for (const [key, value] of Object.entries(fixture)) { url.searchParams.set(key, value); } const settingsUrl = new URL(url); settingsUrl.searchParams.set('route', '/settings'); await browser.send('Emulation.setTouchEmulationEnabled', { enabled: false }); await page.open(settingsUrl.href, { width: 560, height: 1000, theme: 'light' }); const styleLabel = options['style-label'] ?? {tape: 'Tape', swap: 'Swap', cardStack: 'Card stack'}[options.style]; if (styleLabel) { try { await page.waitFor(styleLabel, 300); } catch (_) { await page.scroll(650); await delay(350); await page.waitFor(styleLabel); } } await browser.send('Emulation.setTouchEmulationEnabled', { enabled: false }); await page.open(url.href, { width: 560, height: 1000, theme: 'light' }); await page.click('More actions'); await delay(200); await page.click('Add chat pane'); await delay(250); await page.click(secondChatTarget); await delay(900); // Warm both retained transcripts before recording. for (let index = 0; index < 2; index++) { await page.click(peekTarget); await delay(500); } // Drain late timing batches from navigation and both warm-up switches. await delay(1200); for (let index = 0; index < 2; index++) { await page.evaluate('window.__perfKick?.();true'); await delay(250); } await browser.send('Profiler.enable'); await browser.send('Profiler.setSamplingInterval', { interval: 1000 }); await browser.send('Emulation.setCPUThrottlingRate', { rate: Number(options['cpu-rate']) }); await page.evaluate(`(() => { window.__paneFrames = []; window.__paneLongTasks = []; window.__paneRecording = true; window.__paneObserver = new PerformanceObserver((list) => { if (window.__paneRecording) { window.__paneLongTasks.push(...list.getEntries().map((entry) => entry.duration)); } }); window.__paneObserver.observe({ type: 'longtask', buffered: false }); let last = performance.now(); const tick = (time) => { if (!window.__paneRecording) return; window.__paneFrames.push(time - last); last = time; requestAnimationFrame(tick); }; requestAnimationFrame(tick); return true; })()`); frames.length = 0; await browser.send('Profiler.start'); const start = performance.now(); const selections = []; for (let index = 0; index < Number(options.switches); index++) { const expected = await page.evaluate(`(() => { const peek = document.querySelector('[flt-semantics-identifier^="chat-peek-"]'); return peek?.getAttribute('flt-semantics-identifier').replace('chat-peek-', 'chat-pane-'); })()`); if (!expected) throw new Error('No inactive chat preview to switch'); const at = await page.waitFor(peekTarget); await browser.send('Input.dispatchTouchEvent', { type: 'touchStart', touchPoints: [{x: at.x, y: at.y, id: 0}] }); await browser.send('Input.dispatchTouchEvent', { type: 'touchEnd', touchPoints: [] }); await delay(Number(options.cadence)); const selected = await page.evaluate(`Boolean(document.querySelector( '[flt-semantics-identifier="' + ${JSON.stringify(expected)} + '"]' ))`); selections.push({ expected, selected }); if (!selected) throw new Error(`Switch ${index + 1} did not activate ${expected}`); } await delay(300); const elapsed = performance.now() - start; const raf = await page.evaluate(` window.__paneRecording = false; window.__paneObserver.disconnect(); ({ deltas: window.__paneFrames, longTasks: window.__paneLongTasks }) `); const { profile } = await browser.send('Profiler.stop'); await writeFile( join(options.out, `run-${repetition + 1}.cpuprofile`), JSON.stringify(profile), ); const cpu = attribute(profile, lookup, { n: 15 }); await writeFile( join(options.out, `run-${repetition + 1}-cpu.json`), JSON.stringify(cpu, null, 2), ); // Flush timing batches without counting navigation/setup frames. await delay(500); for (let index = 0; index < 2; index++) { await page.evaluate('window.__perfKick?.();true'); await delay(150); } const useful = frames.filter((frame) => frame[0] + frame[1] > 300); const result = { rep: repetition + 1, style: options.style, cpuRate: Number(options['cpu-rate']), elapsedMs: round(elapsed), switches: Number(options.switches), cadenceMs: Number(options.cadence), confirmedSwitches: selections.filter((item) => item.selected).length, ...frameMetrics(useful, raf), cpu, }; runs.push(result); await writeFile( join(options.out, `run-${repetition + 1}-frames.json`), JSON.stringify({ flutter: useful, raf, selections }, null, 2), ); if (repetition === 0) { await writeFile(join(options.out, 'final.png'), await page.shot()); } console.log(JSON.stringify({ ...result, cpu: undefined })); } const metrics = [ 'buildP50Ms', 'totalP50Ms', 'totalMaxMs', 'rafP50Ms', 'rafMaxMs', 'rafOver33', 'frames', 'buildSumMs', 'buildMaxMs', 'buildP95Ms', 'rasterSumMs', 'rasterP95Ms', 'totalP95Ms', 'over16', 'rafP95Ms', 'longTasks', 'longTaskMs', ]; const medians = Object.fromEntries(metrics.map((metric) => [ metric, round(quantile(runs.map((run) => run[metric]), .5)), ])); const report = { build: options.build, platform: options.platform, style: options.style, cpuRate: Number(options['cpu-rate']), messages: Number(options.messages), size: '560x1000', headlessCpuRaster: true, semanticsEnabled: true, runs, medians, errors: page.errors, }; await writeFile(join(options.out, 'report.json'), JSON.stringify(report, null, 2)); if (page.errors.length) throw new Error('Browser errors: ' + page.errors.join('\n')); console.log('MEDIANS', JSON.stringify(medians)); } finally { await browser.close(); await server.close(); }