247 lines
9.0 KiB
JavaScript
247 lines
9.0 KiB
JavaScript
#!/usr/bin/env node
|
|
// Repeated real pointer switches over the full-app rich-chat fixture.
|
|
// Uses the generic capture browser and Flutter frame timing hook; never a live server.
|
|
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';
|
|
|
|
const { values: options } = parseArgs({
|
|
options: {
|
|
build: { type: 'string' },
|
|
out: { type: 'string' },
|
|
repeat: { type: 'string', default: '3' },
|
|
switches: { type: 'string', default: '10' },
|
|
cadence: { type: 'string', default: '400' },
|
|
platform: { type: 'string', default: 'material' },
|
|
messages: { type: 'string', default: '120' },
|
|
},
|
|
});
|
|
if (!options.build || !options.out) throw new Error('--build and --out required');
|
|
|
|
function quantile(values, proportion) {
|
|
const sorted = [...values].sort((a, b) => a - b);
|
|
return sorted.length
|
|
? sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * proportion))]
|
|
: 0;
|
|
}
|
|
const round = (value) => Math.round(value * 100) / 100;
|
|
|
|
// 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, () => {});
|
|
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(); } 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);
|
|
}
|
|
await page.open(url.href, { width: 560, height: 1000, theme: 'light' });
|
|
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: 100 });
|
|
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');
|
|
await page.click(peekTarget);
|
|
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 sum = (index) => round(
|
|
useful.reduce((total, frame) => total + frame[index], 0) / 1000,
|
|
);
|
|
const max = (index) => round(
|
|
Math.max(0, ...useful.map((frame) => frame[index])) / 1000,
|
|
);
|
|
const p95 = (index) => round(quantile(
|
|
useful.map((frame) => frame[index] / 1000),
|
|
.95,
|
|
));
|
|
const result = {
|
|
rep: repetition + 1,
|
|
elapsedMs: round(elapsed),
|
|
switches: Number(options.switches),
|
|
cadenceMs: Number(options.cadence),
|
|
confirmedSwitches: selections.filter((item) => item.selected).length,
|
|
frames: useful.length,
|
|
buildSumMs: sum(0),
|
|
buildMaxMs: max(0),
|
|
buildP95Ms: p95(0),
|
|
rasterSumMs: sum(1),
|
|
rasterMaxMs: max(1),
|
|
rasterP95Ms: p95(1),
|
|
totalP95Ms: p95(2),
|
|
over16: useful.filter((frame) => frame[2] > 16667).length,
|
|
rafP95Ms: round(quantile(raf.deltas, .95)),
|
|
rafMaxMs: round(Math.max(0, ...raf.deltas)),
|
|
longTasks: raf.longTasks.length,
|
|
longTaskMs: round(raf.longTasks.reduce((total, duration) => total + duration, 0)),
|
|
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 = [
|
|
'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,
|
|
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();
|
|
}
|