Measure chat transcript open cost in the perf harness and web probe

This commit is contained in:
2026-11-02 00:15:40 +00:00
parent 52e31fc4ec
commit b80c52b3e1
4 changed files with 139 additions and 9 deletions
+1
View File
@@ -42,6 +42,7 @@ class _ProbeGateway implements ApiGateway {
],
};
case 'chat_messages':
markChatPerfHistory();
return {
'chat_history': {
'messages': [
+9 -1
View File
@@ -11,7 +11,7 @@ import 'package:pipelines_app/core/api_gateway.dart';
/// n text deltas into a new reply, one every 40 ms, starting 6 s after load.
/// With either, every frame's build and raster durations are printed to the
/// console as `PERF_FRAME <build us> <raster us> <total us>` for
/// `tool/chat_perf_probe.mjs`.
/// `tool/chat_perf_probe.mjs`, and the history reply as `PERF_HISTORY`.
int get chatPerfMessages =>
int.tryParse(Uri.base.queryParameters['perf'] ?? '') ?? 0;
int get chatPerfDeltas =>
@@ -46,6 +46,14 @@ List<JsonMap> chatPerfTranscript(int count) => [
},
];
/// Marks the history reply for the probe's open phase: the frames after it are
/// the cost of opening the transcript.
void markChatPerfHistory() {
if (chatPerfMessages == 0) return;
// ignore: avoid_print
print('PERF_HISTORY');
}
/// Starts the frame report and, when asked, the streamed reply through [emit].
void startChatPerf(void Function(JsonMap body) emit) {
if (chatPerfMessages == 0 && chatPerfDeltas == 0) return;
+36 -3
View File
@@ -6,8 +6,9 @@
// 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,
// Three phases, each on a fresh page: open the transcript (the frames after the
// history reply until the page is quiet), wheel-scroll it 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.
@@ -49,12 +50,19 @@ async function phase(origin, query, drive) {
let recording = false;
let frames = [];
const marks = [];
// Every frame since navigation, for the open phase.
const allFrames = [];
let historyAt = null;
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);
if (text === 'PERF_HISTORY') historyAt = { at: Date.now(), frame: allFrames.length };
}
if (text.startsWith('PERF_FRAME ')) {
allFrames.push({ at: Date.now(), f: text.split(' ').slice(1).map(Number) });
}
});
const opened = Date.now();
@@ -70,6 +78,7 @@ async function phase(origin, query, drive) {
marks,
start: () => { frames = []; recording = true; },
stop: () => { recording = false; return frames; },
opened: () => ({ historyAt, allFrames }),
};
const result = await drive(control);
return { openMs, ...result, errors: page.errors.slice(0, 5) };
@@ -80,6 +89,30 @@ async function phase(origin, query, drive) {
const server = await serveBuild(dir);
try {
const open = await phase(server.origin, `perf=${messages}`, async ({ opened }) => {
// The page is quiet once no frame has arrived for a second.
const end = Date.now() + 20000;
for (;;) {
const { historyAt, allFrames } = opened();
const last = allFrames.at(-1)?.at ?? 0;
if (historyAt && Date.now() - Math.max(last, historyAt.at) > 1000) break;
if (Date.now() > end) break;
await delay(100);
}
const { historyAt, allFrames } = opened();
if (!historyAt) return { open: null };
const after = allFrames.slice(historyAt.frame);
const sum = (i) => +(after.reduce((a, x) => a + x.f[i], 0) / 1000).toFixed(1);
return {
open: {
...stats(after.map((x) => x.f)),
buildSumMs: sum(0),
totalSumMs: sum(2),
// Wall time from the history reply to the last frame of the burst.
settleMs: after.length ? after.at(-1).at - historyAt.at : 0,
},
};
});
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)]) {
@@ -102,7 +135,7 @@ try {
return { streamed: marks.includes('PERF_STREAM_END'), stream: stats(stop()) };
},
);
console.log(JSON.stringify({ build: dir, messages, deltas, scroll, stream }, null, 2));
console.log(JSON.stringify({ build: dir, messages, deltas, open, scroll, stream }, null, 2));
} finally {
await server.close();
}