Rebuild only the live chat turn while replies stream
Reuse unchanged transcript rows, the composer bar and the header across streamed deltas, and share parsed markdown by text so rows scrolled back into view are not parsed again. Adds a rebuild/parse harness and a release-web frame-timing probe with a long-transcript fixture.
This commit is contained in:
@@ -7,6 +7,8 @@ import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||
|
||||
import 'chat_perf_fixture.dart';
|
||||
|
||||
/// A live-looking fixture conversation with a deterministic composer.
|
||||
///
|
||||
/// A successful prompt starts a running turn (so the primary control becomes
|
||||
@@ -43,6 +45,8 @@ class _ProbeGateway implements ApiGateway {
|
||||
return {
|
||||
'chat_history': {
|
||||
'messages': [
|
||||
// `?perf=<n>`: a long transcript for tool/chat_perf_probe.mjs.
|
||||
...chatPerfTranscript(chatPerfMessages),
|
||||
// A long history scrolls behind the floating header.
|
||||
if (_probeLong || _query['long'] == '1')
|
||||
for (var i = 0; i < 12; i++)
|
||||
@@ -62,28 +66,31 @@ class _ProbeGateway implements ApiGateway {
|
||||
// text parts and a second folded message, each with markdown
|
||||
// paragraphs.
|
||||
if (_query['paras'] == '1') ..._chunkedReply,
|
||||
{
|
||||
'id': 'probe-user',
|
||||
'role': 'user',
|
||||
'parts': [
|
||||
{
|
||||
'type': 'text',
|
||||
'text': 'Show me the composer as an operator would use it.',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
'id': 'probe-assistant',
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{
|
||||
'type': 'text',
|
||||
'text':
|
||||
'The composer sits below the conversation with a '
|
||||
'multiline field and one primary control.',
|
||||
},
|
||||
],
|
||||
},
|
||||
if (chatPerfMessages == 0) ...[
|
||||
{
|
||||
'id': 'probe-user',
|
||||
'role': 'user',
|
||||
'parts': [
|
||||
{
|
||||
'type': 'text',
|
||||
'text':
|
||||
'Show me the composer as an operator would use it.',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
'id': 'probe-assistant',
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{
|
||||
'type': 'text',
|
||||
'text':
|
||||
'The composer sits below the conversation with a '
|
||||
'multiline field and one primary control.',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
],
|
||||
'next_cursor': '',
|
||||
'has_more': false,
|
||||
@@ -124,6 +131,8 @@ class _ProbeGateway implements ApiGateway {
|
||||
'step': {'status': 'ended', 'turn_id': 'probe-turn', 'intermediate': false},
|
||||
});
|
||||
|
||||
void emit(JsonMap body) => _emit(body);
|
||||
|
||||
void _emit(JsonMap body) => _frames.add({
|
||||
'type': 'chat_event',
|
||||
'chat_event': {'session_id': _session, ...body},
|
||||
@@ -200,7 +209,8 @@ const _probeStyle = String.fromEnvironment(
|
||||
|
||||
/// Browser overrides: `?platform=cupertino|material`, `?theme=dark|light`,
|
||||
/// `?long=1`, `?paras=1` (a chunked multi-paragraph reply), `?tasks=<n>`, `?mode=0` (no permission mode recorded) and
|
||||
/// `?model=0` (a backend without models).
|
||||
/// `?model=0` (a backend without models). `?perf=<n>` and `?stream=<n>` are
|
||||
/// the long-transcript performance fixture (see chat_perf_fixture.dart).
|
||||
///
|
||||
/// Delivery states: `?send=<text>` types and sends that text once the thread
|
||||
/// has loaded (`hold` never replies, `queued` queues, `refuse` is refused), and
|
||||
@@ -244,6 +254,7 @@ void main() {
|
||||
),
|
||||
newOperationId: gateway.nextOperation,
|
||||
);
|
||||
startChatPerf(gateway.emit);
|
||||
if (_query['send'] != null) {
|
||||
Timer(
|
||||
const Duration(milliseconds: 1200),
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
|
||||
/// Long-transcript performance fixture for `tool/chat_composer_probe_entry.dart`.
|
||||
///
|
||||
/// `?perf=<n>` opens a transcript of n messages whose answers carry headings,
|
||||
/// inline markdown, a 30-line code block and a list. `?stream=<n>` then streams
|
||||
/// 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`.
|
||||
int get chatPerfMessages =>
|
||||
int.tryParse(Uri.base.queryParameters['perf'] ?? '') ?? 0;
|
||||
int get chatPerfDeltas =>
|
||||
int.tryParse(Uri.base.queryParameters['stream'] ?? '') ?? 0;
|
||||
|
||||
String _answer(int index) =>
|
||||
'## Answer $index\n\nSome **bold** text with `code` and a '
|
||||
'[link](https://example.com/$index).\n\n'
|
||||
'```dart\n'
|
||||
'${List.generate(30, (line) => 'final v$line = $index * $line;').join('\n')}'
|
||||
'\n```\n\n'
|
||||
'- one\n- two\n- three\n';
|
||||
|
||||
/// The fixture history; the newest message is the operator's, so a streamed
|
||||
/// reply opens a new turn.
|
||||
List<JsonMap> chatPerfTranscript(int count) => [
|
||||
for (var index = 0; index < count; index++)
|
||||
(count - index).isOdd
|
||||
? {
|
||||
'id': 'perf-u$index',
|
||||
'role': 'user',
|
||||
'parts': [
|
||||
{'type': 'text', 'text': 'Question $index'},
|
||||
],
|
||||
}
|
||||
: {
|
||||
'id': 'perf-a$index',
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{'type': 'text', 'id': 'perf-t$index', 'text': _answer(index)},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
/// 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;
|
||||
WidgetsFlutterBinding.ensureInitialized().addTimingsCallback((timings) {
|
||||
for (final timing in timings) {
|
||||
// ignore: avoid_print
|
||||
print(
|
||||
'PERF_FRAME ${timing.buildDuration.inMicroseconds} '
|
||||
'${timing.rasterDuration.inMicroseconds} '
|
||||
'${timing.totalSpan.inMicroseconds}',
|
||||
);
|
||||
}
|
||||
});
|
||||
final deltas = chatPerfDeltas;
|
||||
if (deltas == 0) return;
|
||||
Timer(const Duration(milliseconds: 6000), () {
|
||||
// ignore: avoid_print
|
||||
print('PERF_STREAM_START');
|
||||
var sent = 0;
|
||||
Timer.periodic(const Duration(milliseconds: 40), (timer) {
|
||||
emit({
|
||||
'message_id': 'perf-live',
|
||||
'kind': 'text',
|
||||
'part': {'id': 'perf-live-text', 'type': 'text'},
|
||||
'delta': sent == 0 ? 'Live reply ' : 'word$sent ',
|
||||
});
|
||||
if (++sent >= deltas) {
|
||||
timer.cancel();
|
||||
// ignore: avoid_print
|
||||
print('PERF_STREAM_END');
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
// Release-web frame timings for a long chat transcript, over the capture kit.
|
||||
//
|
||||
// Build the fixture once per variant, from app/:
|
||||
// flutter build web --release --target tool/chat_composer_probe_entry.dart \
|
||||
// --output build/chat-perf
|
||||
// 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,
|
||||
// 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.
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import { openBrowser } from './capture/browser.mjs';
|
||||
import { flutterPage } from './capture/page.mjs';
|
||||
import { serveBuild } from './capture/serve.mjs';
|
||||
|
||||
const [dir, messagesArg = '300', deltasArg = '200'] = process.argv.slice(2);
|
||||
if (!dir) {
|
||||
console.error('usage: node tool/chat_perf_probe.mjs <build dir> [messages] [deltas]');
|
||||
process.exit(2);
|
||||
}
|
||||
const messages = Number(messagesArg);
|
||||
const deltas = Number(deltasArg);
|
||||
|
||||
function stats(frames) {
|
||||
const pick = (i) => frames.map((f) => f[i]).sort((a, b) => a - b);
|
||||
const summary = (values) => values.length === 0 ? null : {
|
||||
meanMs: +(values.reduce((a, b) => a + b, 0) / values.length / 1000).toFixed(2),
|
||||
p50Ms: +(values[Math.floor(values.length / 2)] / 1000).toFixed(2),
|
||||
p90Ms: +(values[Math.floor(values.length * 0.9)] / 1000).toFixed(2),
|
||||
maxMs: +(values[values.length - 1] / 1000).toFixed(2),
|
||||
};
|
||||
const total = pick(2);
|
||||
return {
|
||||
frames: frames.length,
|
||||
build: summary(pick(0)),
|
||||
raster: summary(pick(1)),
|
||||
total: summary(total),
|
||||
over16ms: total.filter((t) => t > 16667).length,
|
||||
};
|
||||
}
|
||||
|
||||
async function phase(origin, query, drive) {
|
||||
const browser = await openBrowser();
|
||||
try {
|
||||
const page = flutterPage(browser, (m) => console.error(m));
|
||||
let recording = false;
|
||||
let frames = [];
|
||||
const marks = [];
|
||||
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);
|
||||
}
|
||||
});
|
||||
const opened = Date.now();
|
||||
try {
|
||||
await page.open(`${origin}/?${query}`, { width: 880, height: 1000 });
|
||||
} catch (error) {
|
||||
throw new Error(`${error.message}; page errors: ${JSON.stringify(page.errors.slice(0, 5))}`);
|
||||
}
|
||||
const openMs = Date.now() - opened;
|
||||
await delay(1000);
|
||||
const control = {
|
||||
page,
|
||||
marks,
|
||||
start: () => { frames = []; recording = true; },
|
||||
stop: () => { recording = false; return frames; },
|
||||
};
|
||||
const result = await drive(control);
|
||||
return { openMs, ...result, errors: page.errors.slice(0, 5) };
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
}
|
||||
|
||||
const server = await serveBuild(dir);
|
||||
try {
|
||||
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)]) {
|
||||
await page.scroll(dy);
|
||||
await delay(50);
|
||||
}
|
||||
await delay(500);
|
||||
return { scroll: stats(stop()) };
|
||||
});
|
||||
const stream = await phase(
|
||||
server.origin,
|
||||
`perf=${messages}&stream=${deltas}`,
|
||||
async ({ start, stop, marks }) => {
|
||||
const begin = Date.now() + 15000;
|
||||
while (!marks.includes('PERF_STREAM_START') && Date.now() < begin) await delay(5);
|
||||
start();
|
||||
const end = Date.now() + deltas * 40 + 15000;
|
||||
while (!marks.includes('PERF_STREAM_END') && Date.now() < end) await delay(100);
|
||||
await delay(500);
|
||||
return { streamed: marks.includes('PERF_STREAM_END'), stream: stats(stop()) };
|
||||
},
|
||||
);
|
||||
console.log(JSON.stringify({ build: dir, messages, deltas, scroll, stream }, null, 2));
|
||||
} finally {
|
||||
await server.close();
|
||||
}
|
||||
Reference in New Issue
Block a user