Measure chat transcript open cost in the perf harness and web probe
This commit is contained in:
@@ -9,9 +9,11 @@ import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart
|
||||
|
||||
import '../conversation/chat_thread_fixtures.dart';
|
||||
|
||||
/// Measurement harness: streams 200 text deltas into a 300-message transcript
|
||||
/// and counts message builds, markdown builds/parses and per-delta frame time;
|
||||
/// then scrolls the long transcript up and back down.
|
||||
/// Measurement harness: opens 300- and 1000-message transcripts and counts the
|
||||
/// rows built and parsed until the newest message shows; streams 200 text
|
||||
/// deltas into a 300-message transcript and counts message builds, markdown
|
||||
/// builds/parses and per-delta frame time; then scrolls the long transcript up
|
||||
/// and back down.
|
||||
const _messages = 300;
|
||||
const _deltas = 200;
|
||||
|
||||
@@ -23,8 +25,8 @@ String _answer(int index) =>
|
||||
'\n```\n\n'
|
||||
'- one\n- two\n- three\n';
|
||||
|
||||
List<JsonMap> _transcript() => [
|
||||
for (var index = 0; index < _messages; index++)
|
||||
List<JsonMap> _transcript([int count = _messages]) => [
|
||||
for (var index = 0; index < count; index++)
|
||||
// The newest message is the operator's, so the reply opens a new turn.
|
||||
index.isOdd
|
||||
? chatMessage(
|
||||
@@ -77,6 +79,92 @@ String _stats(List<int> samples) {
|
||||
|
||||
void main() {
|
||||
setUp(chatMarkdownCache.clear);
|
||||
for (final count in [300, 1000]) {
|
||||
testWidgets('opening a $count-message transcript builds what shows', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(1000, 1600);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final gateway = FakeChatGateway();
|
||||
addTearDown(gateway.close);
|
||||
gateway.history = (_) async => chatPage(_transcript(count));
|
||||
var messageBuilds = 0;
|
||||
widgets.debugOnRebuildDirtyWidget = (element, _) {
|
||||
if (element.widget is ChatMessageView) messageBuilds++;
|
||||
};
|
||||
addTearDown(() => widgets.debugOnRebuildDirtyWidget = null);
|
||||
// The newest message is the operator's question.
|
||||
final newestId = 'u${count - 1}';
|
||||
final newest = find.byWidgetPredicate(
|
||||
(widget) =>
|
||||
widget is ChatMessageView &&
|
||||
(widget.row['key'] == newestId ||
|
||||
widget.row['message']?['id'] == newestId),
|
||||
);
|
||||
final watch = Stopwatch()..start();
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ChatThreadView(
|
||||
gateway: gateway,
|
||||
sessionId: 's',
|
||||
onBack: () {},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
// From the first frame that builds a message row to the frame that shows
|
||||
// the newest one: the open cost the operator waits through.
|
||||
int? firstRowFrame;
|
||||
int? shownFrame;
|
||||
var openUs = 0;
|
||||
var maxFrameUs = 0;
|
||||
bool newestShows() {
|
||||
if (newest.evaluate().isEmpty) return false;
|
||||
final rect = tester.getRect(newest);
|
||||
return rect.bottom > 0 && rect.top < 1600;
|
||||
}
|
||||
|
||||
final frame = Stopwatch();
|
||||
for (var index = 1; index <= 300 && shownFrame == null; index++) {
|
||||
final rowsBefore = messageBuilds;
|
||||
frame
|
||||
..reset()
|
||||
..start();
|
||||
await tester.pump(const Duration(milliseconds: 16));
|
||||
frame.stop();
|
||||
if (firstRowFrame == null && messageBuilds > rowsBefore) {
|
||||
firstRowFrame = index;
|
||||
}
|
||||
if (firstRowFrame != null) {
|
||||
openUs += frame.elapsedMicroseconds;
|
||||
if (frame.elapsedMicroseconds > maxFrameUs) {
|
||||
maxFrameUs = frame.elapsedMicroseconds;
|
||||
}
|
||||
}
|
||||
if (newestShows()) shownFrame = index;
|
||||
}
|
||||
final rowsAtShow = messageBuilds;
|
||||
final parsesAtShow = chatMarkdownCache.parses;
|
||||
await tester.pumpAndSettle();
|
||||
watch.stop();
|
||||
widgets.debugOnRebuildDirtyWidget = null;
|
||||
// ignore: avoid_print
|
||||
print(
|
||||
'PERF open$count: rowsBuiltAtShow=$rowsAtShow '
|
||||
'parsesAtShow=$parsesAtShow '
|
||||
'framesToShow=${shownFrame == null ? null : shownFrame - firstRowFrame! + 1} '
|
||||
'openMs=${(openUs / 1000).toStringAsFixed(1)} '
|
||||
'maxFrameMs=${(maxFrameUs / 1000).toStringAsFixed(1)} '
|
||||
'settled rows=$messageBuilds parses=${chatMarkdownCache.parses} '
|
||||
'wallMs=${watch.elapsedMilliseconds}',
|
||||
);
|
||||
expect(shownFrame, isNotNull);
|
||||
expect(tester.getRect(newest).bottom, lessThanOrEqualTo(1600));
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('streaming deltas rebuild only the live turn', (tester) async {
|
||||
final gateway = await _open(tester);
|
||||
var messageBuilds = 0;
|
||||
|
||||
Reference in New Issue
Block a user