test(app): benchmark populated chat Back and document UX review
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
// Measures normal-app Back against a populated loopback chat, never operator data.
|
||||
// PROBE_PLATFORM=android CPU_RATE=4 CHAT_PAIRS=100 node tool/chat_back_latency_probe.mjs <web-build> <out>
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdir, mkdtemp, writeFile } from 'node:fs/promises';
|
||||
import { once } from 'node:events';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join, resolve } from 'node:path';
|
||||
import { performance } from 'node:perf_hooks';
|
||||
import { startFixture } from './runs_probe_server.mjs';
|
||||
import { launchChrome, waitForPort, waitForPage, connect, sleep } from './pipelines_probe_cdp_lib.mjs';
|
||||
import { axNodes, waitFor, capture } from './pipelines_probe_cdp_input.mjs';
|
||||
|
||||
const pairs = Number(process.env.CHAT_PAIRS ?? 100);
|
||||
const text = '# Recorded answer\n\n' +
|
||||
Array.from({ length: 12 }, (_, i) => `Paragraph ${i}: This is recorded fixture content with **formatting**, an [inert link](https://example.test), and enough text to wrap naturally on a phone.\n`).join('\n') +
|
||||
'\n```dart\n' + Array.from({ length: 25 }, (_, i) => `final value${i} = ${i};`).join('\n') + '\n```\n';
|
||||
const turns = Array.from({ length: pairs }, (_, i) => [
|
||||
{ id: `u${i}`, role: 'user', parts: [{ type: 'text', text: `Recorded question ${i}` }] },
|
||||
{ id: `a${i}`, role: 'assistant', parts: [{ id: `p${i}`, type: 'text', text }] },
|
||||
]).flat();
|
||||
const folded = process.env.CHAT_FOLDED === '1';
|
||||
const messages = folded ? [turns[0], ...turns.filter(m => m.role === 'assistant')] : turns;
|
||||
const output = resolve(process.argv[3] ?? 'build/chat-back-latency');
|
||||
await mkdir(output, { recursive: true });
|
||||
const fixture = await startFixture({ root: resolve(process.argv[2]), chatMessages: messages });
|
||||
const profile = await mkdtemp(join(tmpdir(), 'chat-back-latency-'));
|
||||
const chrome = launchChrome('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', profile, 'about:blank');
|
||||
const log = { pairs, folded, streaming: process.env.CHAT_STREAM === '1', cpuRate: Number(process.env.CPU_RATE ?? 1), host: process.env.PROBE_PLATFORM ?? 'cupertino', checks: [], captures: [] };
|
||||
let c;
|
||||
let streamTimer;
|
||||
const requests = new Set();
|
||||
try {
|
||||
c = await connect((await waitForPage(await waitForPort(join(profile, 'DevToolsActivePort')))).webSocketDebuggerUrl);
|
||||
await c.send('Page.enable');
|
||||
if (log.host === 'android') await c.send('Emulation.setUserAgentOverride', {
|
||||
userAgent: 'Mozilla/5.0 (Linux; Android 16; BrowserFixture) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Mobile Safari/537.36', platform: 'Linux armv8l',
|
||||
});
|
||||
if (log.host === 'material') await c.send('Emulation.setUserAgentOverride', {
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36', platform: 'Win32',
|
||||
});
|
||||
await c.send('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true });
|
||||
await c.send('Emulation.setCPUThrottlingRate', { rate: log.cpuRate });
|
||||
const opened = performance.now();
|
||||
await c.send('Page.navigate', { url: fixture.url + '/chat/ses_probechat' });
|
||||
assert.ok(await waitFor(() => c.evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"), 30000));
|
||||
// Mobile semantics activation requires a real pointer at the placeholder's
|
||||
// centre; element.click() at (0,0) does not enable Android web semantics.
|
||||
const rect = await c.evaluate("(() => { const r = document.querySelector('flt-semantics-placeholder').getBoundingClientRect(); return {x:(r.left+r.right)/2,y:(r.top+r.bottom)/2}; })()");
|
||||
await c.send('Input.dispatchMouseEvent', { type: 'mousePressed', x: rect.x, y: rect.y, button: 'left', clickCount: 1 });
|
||||
await c.send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: rect.x, y: rect.y, button: 'left', clickCount: 1 });
|
||||
assert.ok(await waitFor(async () => (await axNodes(c)).some(n => n.name?.value === 'Back'), 45000), 'chat Back ready');
|
||||
await sleep(1800);
|
||||
await c.evaluate('new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)))');
|
||||
log.openAndSettleMs = performance.now() - opened;
|
||||
await capture(c, output, log, 'chat-before-back');
|
||||
if (log.streaming) {
|
||||
streamTimer = setInterval(() => {
|
||||
const request = fetch(fixture.url + '/fixture/chat-event', { method: 'POST', body: JSON.stringify({
|
||||
kind: 'text', message_id: `a${pairs-1}`, part: { type: 'text', id: `p${pairs-1}` }, delta: ' live',
|
||||
}) });
|
||||
requests.add(request);
|
||||
request.finally(() => requests.delete(request));
|
||||
}, 100);
|
||||
await sleep(1200);
|
||||
}
|
||||
const backNodes = (await axNodes(c)).filter(n => /^Back(?:\s|$)/.test(n.name?.value ?? ''));
|
||||
log.backControlNodes = backNodes.map(n => ({name:n.name?.value, role:n.role?.value, backend:n.backendDOMNodeId}));
|
||||
const back = backNodes.find(n => n.backendDOMNodeId);
|
||||
assert.ok(back, 'native Back control');
|
||||
const { model } = await c.send('DOM.getBoxModel', { backendNodeId: back.backendDOMNodeId });
|
||||
const [x1, y1, , , x3, y3] = model.content;
|
||||
await c.evaluate(`(() => {
|
||||
window.__back = {started: performance.now(), frames: [], longTasks: []};
|
||||
new PerformanceObserver(list => { for (const e of list.getEntries()) window.__back.longTasks.push({start:e.startTime,duration:e.duration}); }).observe({entryTypes:['longtask']});
|
||||
const frame = t => { window.__back.frames.push(t); if (location.pathname === '/chat') window.__back.settled=t; else requestAnimationFrame(frame); };
|
||||
requestAnimationFrame(frame);
|
||||
})()`);
|
||||
const started = performance.now();
|
||||
await c.send('Input.dispatchMouseEvent', { type: 'mousePressed', x: (x1+x3)/2, y: (y1+y3)/2, button: 'left', clickCount: 1 });
|
||||
await c.send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: (x1+x3)/2, y: (y1+y3)/2, button: 'left', clickCount: 1 });
|
||||
assert.ok(await waitFor(() => c.evaluate('Boolean(window.__back.settled)'), 30000), 'Back returns to list without network response');
|
||||
const metrics = await c.evaluate('window.__back');
|
||||
log.backWallMs = performance.now() - started;
|
||||
log.routeSettledMs = metrics.settled - metrics.started;
|
||||
log.maxFrameGapMs = Math.max(0, ...metrics.frames.slice(1).map((t, i) => t - metrics.frames[i]));
|
||||
log.longTasks = metrics.longTasks;
|
||||
if (process.env.MAX_BACK_MS) assert.ok(log.routeSettledMs <= Number(process.env.MAX_BACK_MS), `Back exceeded ${process.env.MAX_BACK_MS} ms: ${log.routeSettledMs}`);
|
||||
if (process.env.MAX_FRAME_GAP_MS) assert.ok(log.maxFrameGapMs <= Number(process.env.MAX_FRAME_GAP_MS), `Frame gap exceeded ${process.env.MAX_FRAME_GAP_MS} ms: ${log.maxFrameGapMs}`);
|
||||
log.checks.push('pop restored chat list');
|
||||
await sleep(150);
|
||||
await capture(c, output, log, 'chat-after-back');
|
||||
console.log(JSON.stringify(log, null, 2));
|
||||
} catch (error) {
|
||||
if (c) {
|
||||
await capture(c, output, log, 'failure');
|
||||
await writeFile(join(output, 'failure-ax.json'), JSON.stringify(await axNodes(c), null, 2));
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
clearInterval(streamTimer);
|
||||
await Promise.allSettled([...requests]);
|
||||
await writeFile(join(output, 'result.json'), JSON.stringify(log, null, 2));
|
||||
c?.close(); const exited = once(chrome, 'exit'); chrome.kill(); await exited;
|
||||
await fixture.close();
|
||||
}
|
||||
@@ -310,7 +310,7 @@ async function staticFile(base, pathname, req, res) {
|
||||
* It serves the built probe bundle and upgrades /connect on the same origin, so
|
||||
* the normal web composition infers this server from Uri.base.
|
||||
*/
|
||||
export async function startFixture({ root, runs = RUNS, runDetail, files = {} }) {
|
||||
export async function startFixture({ root, runs = RUNS, runDetail, files = {}, chatMessages }) {
|
||||
const base = await realpath(root);
|
||||
const connections = new Set();
|
||||
const sockets = new Set();
|
||||
@@ -564,7 +564,7 @@ export async function startFixture({ root, runs = RUNS, runDetail, files = {} })
|
||||
client.send({
|
||||
id: request.id, type: 'chat_history',
|
||||
chat_history: {
|
||||
messages: [
|
||||
messages: chatMessages ?? [
|
||||
{ id: 'm1', session_id: CHAT_SESSION.id, role: 'user',
|
||||
parts: [{ type: 'text', text: 'Read-only probe question.' }] },
|
||||
{ id: 'm2', session_id: CHAT_SESSION.id, role: 'assistant',
|
||||
|
||||
Reference in New Issue
Block a user