Files
pipelines/app/tool/chat_perf_fixture.dart

103 lines
3.6 KiB
Dart

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`, and the history reply as `PERF_HISTORY`.
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'
'| check | result | owner |\n'
'| --- | --- | --- |\n'
'| unit tests | passed | worker $index |\n'
'| static analysis | passed | worker $index |\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)},
],
},
];
/// 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');
// The web engine hands timings over in batches at the end of a frame, so a
// quiet page can hold the open's last frames back; two late frames flush
// them. Each costs well under a millisecond and the probe ignores them.
for (final ms in [2500, 2700]) {
Timer(Duration(milliseconds: ms), () {
WidgetsBinding.instance.scheduleFrame();
});
}
}
/// 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');
}
});
});
}