Merge perf bench scenarios

This commit is contained in:
2026-11-11 01:41:44 +00:00
2 changed files with 74 additions and 12 deletions
+7 -4
View File
@@ -11,11 +11,14 @@ import { setTimeout as delay } from 'node:timers/promises';
const CALL_TIMEOUT_MS = 30000;
const DEFAULT_CHROME ='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
async function launch(chromePath) {
async function launch(chromePath, gpu) {
const profile = await mkdtemp(join(tmpdir(), 'pipelines-capture-chrome-'));
const chrome = spawn(chromePath, [
'--headless=new', '--no-first-run', '--no-default-browser-check',
'--disable-gpu', '--hide-scrollbars', '--mute-audio',
// Without a GPU CanvasKit rasterises on the CPU; `gpu` keeps WebGL (Metal
// through ANGLE on a Mac) for benches that measure raster time.
...(gpu ? ['--use-angle=metal', '--enable-gpu-rasterization', '--ignore-gpu-blocklist'] : ['--disable-gpu']),
'--hide-scrollbars', '--mute-audio',
'--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank',
], { stdio: ['ignore', 'ignore', 'pipe'] });
let stderr = '';
@@ -49,8 +52,8 @@ async function attach(cdp) {
* talks to the page session; `on(method, fn)` subscribes to its events and
* returns the unsubscribe function; `newPage()` opens a scratch page.
*/
export async function openBrowser({ cdp, chromePath = process.env.CHROME_PATH ?? DEFAULT_CHROME } = {}) {
const { ws: endpoint, cleanup } = cdp ? await attach(cdp) : await launch(chromePath);
export async function openBrowser({ cdp, gpu = false, chromePath = process.env.CHROME_PATH ?? DEFAULT_CHROME } = {}) {
const { ws: endpoint, cleanup } = cdp ? await attach(cdp) : await launch(chromePath, gpu);
const ws = new WebSocket(endpoint);
try {
await new Promise((ok, fail) => { ws.onopen = ok; ws.onerror = () => fail(new Error(`Cannot open ${endpoint}`)); });
+67 -8
View File
@@ -15,12 +15,13 @@
// build and raster time; --shots writes a screenshot after each scenario.
//
// node tool/perf/interaction_bench.mjs --build build/probe-app --out /tmp/bench \
// [--repeat 5] [--only name,name] [--size 560x900] [--shots] [--cpuprofile] [--frames]
// [--repeat 5] [--only name,name] [--size 560x900] [--shots] [--cpuprofile] [--frames] [--gpu]
//
// Clicks are points (CSS px at 560x900, Material, light), so Flutter's
// semantics tree stays off as for most users. Headless Chrome without a GPU:
// CanvasKit rasterises on the CPU, so compare builds with each other rather
// than predicting a device.
// than predicting a device. --gpu keeps the GPU (WebGL), which is what a
// desktop user has: use it to judge raster time.
import { mkdir, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { parseArgs } from 'node:util';
@@ -32,19 +33,26 @@ import { attribute, loadSourceMap } from './cpu_attribution.mjs';
const { values: opts } = parseArgs({
options: {
build: { type: 'string' }, out: { type: 'string' }, repeat: { type: 'string', default: '5' },
only: { type: 'string' }, cpuprofile: { type: 'boolean' }, frames: { type: 'boolean' }, shots: { type: 'boolean' }, size: { type: 'string', default: '560x900' },
only: { type: 'string' }, cpuprofile: { type: 'boolean' }, frames: { type: 'boolean' }, shots: { type: 'boolean' }, gpu: { type: 'boolean' }, size: { type: 'string', default: '560x900' },
},
});
if (!opts.build || !opts.out) throw new Error('usage: --build <dir> --out <dir>');
const [width, height] = opts.size.split('x').map(Number);
// Bottom tab bar and top-bar points at 560x900 (Material).
const TAB = { first: [337, 854], second: [396, 854], third: [455, 854], fourth: [514, 854] };
const TOP_RIGHT = [524, 32];
const FIRST_ROW = [280, 138];
// A rich two-chat fixture at 1280x800, and the steps that put the second
// chat in a floating window (More actions, Add chat pane, the chat, Alt+Y).
const DESKTOP_CHAT = { size: '1280x800', route: '/chat/perf-chat-0', q: { chats: '2', header: '1', rich: '1' } };
const FLOAT_CHAT = [{ click: [1240, 40] }, { click: [1198, 96] }, { click: [650, 245] }, { key: 'Alt+y' }, { wait: 800 }];
// `setup` runs unrecorded after load; `act` is recorded. A step is
// `{ click: [x, y] }`, `{ type: 'text' }` (one character per 120 ms), `{ wait }`.
// `{ click: [x, y] }`, `{ type: 'text' }` (one character per 120 ms), `{ wait }`,
// `{ key: 'Alt+y' }`, `{ drag: [x, y, dx, dy] }` (40 moves, 16 ms apart) or
// `{ wheel: [x, y, deltaY, count] }` (one notch per 16 ms). A scenario's `size`
// overrides --size.
const SCENARIOS = [
{ name: 'startup', route: '/runs', act: [] },
{ name: 'tab:runs->pipelines', route: '/runs', act: [{ click: TAB.first }] },
@@ -71,6 +79,24 @@ const SCENARIOS = [
{ name: 'pipeline tab:->source', route: '/pipelines/24', act: [{ click: TAB.fourth }] },
{ name: 'chat list->open chat', route: '/chat', q: { chats: '30' }, act: [{ click: FIRST_ROW }] },
{ name: 'pipelines->settings', route: '/pipelines', act: [{ click: TOP_RIGHT }] },
// Desktop (1280x800): floating windows and the chat beside its list.
{ name: 'desktop: drag a chat window', ...DESKTOP_CHAT, setup: FLOAT_CHAT,
act: [{ drag: [300, 116, 500, 120] }] },
{ name: 'desktop: resize a chat window', ...DESKTOP_CHAT, setup: FLOAT_CHAT,
act: [{ drag: [591, 647, 300, 80] }] },
{ name: 'desktop: hide and show chat windows', ...DESKTOP_CHAT, setup: FLOAT_CHAT,
act: [{ key: 'Alt+y' }, { wait: 600 }, { key: 'Alt+y' }] },
{ name: 'desktop: float a chat', ...DESKTOP_CHAT, setup: FLOAT_CHAT.slice(0, 3),
act: [{ key: 'Alt+y' }] },
{ name: 'desktop: drag a probe window', size: '1280x800', route: '/runs', q: { windows: '3' },
act: [{ drag: [250, 164, 500, 120] }] },
{ name: 'desktop: scroll a rich chat', ...DESKTOP_CHAT,
act: [{ wheel: [860, 400, -240, 30] }] },
{ name: 'desktop: type in the composer', ...DESKTOP_CHAT, setup: [{ click: [860, 748] }],
act: [{ type: 'hello world, typing' }] },
{ name: 'desktop: switch chat', ...DESKTOP_CHAT, act: [{ click: [220, 260] }] },
{ name: 'desktop: open the terminal panel', ...DESKTOP_CHAT, q: { chats: '2', header: '1', terminals: '1' },
act: [{ key: 'Alt+t' }] },
];
const median = (xs) => { const s = [...xs].sort((a, b) => a - b); return s[Math.floor(s.length / 2)]; };
@@ -78,7 +104,7 @@ const r1 = (x) => Math.round(x * 10) / 10;
const lookup = opts.cpuprofile ? await loadSourceMap(opts.build) : null;
const server = await serveBuild(opts.build);
const browser = await openBrowser();
const browser = await openBrowser({ gpu: Boolean(opts.gpu) });
const { send, on } = browser;
const evaluate = async (expression) => (await send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true })).result?.value;
let frames = [];
@@ -102,8 +128,40 @@ async function run(steps) {
if (step.click) { await tap(step.click); await delay(1200); }
if (step.type) for (const ch of step.type) { await send('Input.insertText', { text: ch }); await delay(120); }
if (step.wait) await delay(step.wait);
if (step.key) { await key(step.key); await delay(800); }
if (step.drag) { await drag(step.drag); await delay(400); }
if (step.wheel) {
const [x, y, deltaY, count] = step.wheel;
for (let i = 0; i < count; i++) {
await send('Input.dispatchMouseEvent', { type: 'mouseWheel', x, y, deltaX: 0, deltaY });
await delay(16);
}
await delay(400);
}
}
}
const MODIFIERS = { Alt: 1, Control: 2, Meta: 4, Shift: 8 };
async function key(chord) {
const parts = chord.split('+');
const name = parts.pop();
const modifiers = parts.reduce((bits, part) => bits | MODIFIERS[part], 0);
const code = /^[a-z]$/i.test(name) ? `Key${name.toUpperCase()}` : name;
const base = { modifiers, key: name, code, windowsVirtualKeyCode: name.toUpperCase().charCodeAt(0) };
for (const part of parts) await send('Input.dispatchKeyEvent', { type: 'rawKeyDown', key: part, code: `${part}Left`, modifiers });
await send('Input.dispatchKeyEvent', { type: 'rawKeyDown', ...base });
await send('Input.dispatchKeyEvent', { type: 'keyUp', ...base });
for (const part of parts) await send('Input.dispatchKeyEvent', { type: 'keyUp', key: part, code: `${part}Left`, modifiers: 0 });
}
async function drag([x, y, dx, dy]) {
const steps = 40;
await send('Input.dispatchMouseEvent', { type: 'mouseMoved', x, y, button: 'none' });
await send('Input.dispatchMouseEvent', { type: 'mousePressed', x, y, button: 'left', clickCount: 1 });
for (let i = 1; i <= steps; i++) {
await send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: x + (dx * i) / steps, y: y + (dy * i) / steps, button: 'left', buttons: 1 });
await delay(16);
}
await send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: x + dx, y: y + dy, button: 'left', clickCount: 1 });
}
// The engine hands timings over in batches; two late frames flush them.
async function settle() {
await delay(1500);
@@ -115,10 +173,11 @@ const results = {};
try {
await send('Page.enable');
await send('Runtime.enable');
await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: false });
await send('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: 'light' }] });
const only = opts.only?.split(',');
for (const s of SCENARIOS.filter((s) => !only || only.includes(s.name))) {
const [w, h] = (s.size ?? opts.size).split('x').map(Number);
await send('Emulation.setDeviceMetricsOverride', { width: w, height: h, deviceScaleFactor: 1, mobile: false });
const reps = [];
for (let rep = 0; rep < Number(opts.repeat); rep++) {
const url = new URL('/', server.origin);
@@ -167,7 +226,7 @@ try {
console.log(s.name.padEnd(36), JSON.stringify(results[s.name]));
}
if (errors.length) console.log('page errors (first 5):', [...new Set(errors)].slice(0, 5));
await writeFile(join(opts.out, 'interaction-bench.json'), JSON.stringify({ build: opts.build, size: opts.size, results }, null, 2));
await writeFile(join(opts.out, 'interaction-bench.json'), JSON.stringify({ build: opts.build, size: opts.size, gpu: Boolean(opts.gpu), results }, null, 2));
} finally {
await browser.close().catch(() => {});
await server.close();