Merge: perf round 2 — long chats open with only the visible rows
This commit is contained in:
@@ -74,27 +74,35 @@ String chatJumpToLatestLabel(int newMessages) {
|
||||
/// Where a conversation's transcript is placed when it first shows.
|
||||
///
|
||||
/// No saved position means the operator has none of their own: open at the
|
||||
/// newest content. A saved offset is kept, clamped to the current end, so a
|
||||
/// newest content. A saved offset is kept, clamped to the current extent, so a
|
||||
/// conversation whose content grew or shrank while it was closed still lands on
|
||||
/// a real position.
|
||||
/// a real position. A bottom-anchored transcript measures older rows above its
|
||||
/// centre, so its offsets (and [minScrollExtent]) can be negative.
|
||||
double chatRestoreOffset({
|
||||
required double? savedOffset,
|
||||
required double maxScrollExtent,
|
||||
double minScrollExtent = 0,
|
||||
}) {
|
||||
if (savedOffset == null) return maxScrollExtent;
|
||||
return savedOffset.clamp(0.0, maxScrollExtent).toDouble();
|
||||
return savedOffset.clamp(minScrollExtent, maxScrollExtent).toDouble();
|
||||
}
|
||||
|
||||
/// A saved transcript position: the scroll offset and the message id of the
|
||||
/// row the transcript was split at (see `chatTranscriptAnchor`), or null for a
|
||||
/// top-anchored transcript whose offset counts from its first row. An offset
|
||||
/// only means something measured from the same anchor.
|
||||
typedef ChatScrollPosition = ({double offset, String? anchor});
|
||||
|
||||
/// Per-conversation transcript positions, kept while the chat list stays
|
||||
/// mounted. A conversation opened again returns to the position the operator
|
||||
/// left; one they never moved in opens at its newest message.
|
||||
abstract interface class ChatScrollStore {
|
||||
/// The offset the operator left this conversation at, or null when they never
|
||||
/// scrolled away from its end.
|
||||
double? read(String sessionId);
|
||||
/// The position the operator left this conversation at, or null when they
|
||||
/// never scrolled away from its end.
|
||||
ChatScrollPosition? read(String sessionId);
|
||||
|
||||
/// Records a deliberate position away from the end.
|
||||
void write(String sessionId, double offset);
|
||||
void write(String sessionId, ChatScrollPosition position);
|
||||
|
||||
/// Forgets the position once the operator is following the end again.
|
||||
void clear(String sessionId);
|
||||
@@ -107,19 +115,22 @@ abstract interface class ChatScrollStore {
|
||||
/// client keeps no transcript position on disk and does not import the web's
|
||||
/// storage keys.
|
||||
class MemoryChatScrollStore implements ChatScrollStore {
|
||||
final Map<String, double> _offsets = {};
|
||||
final Map<String, ChatScrollPosition> _offsets = {};
|
||||
|
||||
/// A snapshot of the stored offsets, so a test can tell a cleared
|
||||
/// A snapshot of the stored positions, so a test can tell a cleared
|
||||
/// conversation from one that was never moved.
|
||||
Map<String, double> get entries => Map.unmodifiable(_offsets);
|
||||
Map<String, ChatScrollPosition> get entries => Map.unmodifiable(_offsets);
|
||||
|
||||
@override
|
||||
double? read(String sessionId) => _offsets[sessionId];
|
||||
ChatScrollPosition? read(String sessionId) => _offsets[sessionId];
|
||||
|
||||
@override
|
||||
void write(String sessionId, double offset) {
|
||||
if (!offset.isFinite || offset < 0) return;
|
||||
_offsets[sessionId] = offset;
|
||||
void write(String sessionId, ChatScrollPosition position) {
|
||||
// A top-anchored offset counts from the first row and is never negative;
|
||||
// a bottom-anchored one is negative above its centre.
|
||||
if (!position.offset.isFinite) return;
|
||||
if (position.anchor == null && position.offset < 0) return;
|
||||
_offsets[sessionId] = position;
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -35,7 +35,9 @@ import '../../../ui/adaptive/segmented_bar_overlay.dart';
|
||||
import '../../../ui/layout/app_sheet_header.dart';
|
||||
import 'chat_reading_width.dart';
|
||||
import 'chat_thread_header.dart';
|
||||
import 'chat_transcript_anchor.dart';
|
||||
import 'chat_transcript_cache.dart';
|
||||
import 'chat_transcript_scroll_view.dart';
|
||||
import 'chat_permission_card_model.dart';
|
||||
import 'chat_permission_group.dart';
|
||||
import 'chat_transcript_card.dart';
|
||||
@@ -185,7 +187,12 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
|
||||
/// The conversation's saved position, read when this view is attached and
|
||||
/// consumed by the first placement after the history arrives.
|
||||
double? _savedOffset;
|
||||
ChatScrollPosition? _savedPosition;
|
||||
|
||||
/// The message id the transcript is split at (see [chatTranscriptAnchor]),
|
||||
/// chosen once when the history first arrives; null keeps it top-anchored.
|
||||
String? _anchor;
|
||||
bool _anchorChosen = false;
|
||||
bool _placed = false;
|
||||
bool _pinScheduled = false;
|
||||
|
||||
@@ -196,6 +203,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
double? _restoreOffset;
|
||||
bool _restoring = false;
|
||||
double _restoreMeasuredExtent = 0;
|
||||
double _restoreMeasuredStart = 0;
|
||||
|
||||
/// True during a pointer/touch/wheel scroll. Programmatic placement and
|
||||
/// follow jumps never set it, so they cannot release the follow intent.
|
||||
@@ -384,7 +392,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_savedOffset = widget.scrollPositions?.read(widget.sessionId);
|
||||
_savedPosition = widget.scrollPositions?.read(widget.sessionId);
|
||||
_adoptCachedTranscript();
|
||||
_seedCommandNotice();
|
||||
}
|
||||
@@ -416,7 +424,9 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
_syncGeneration++;
|
||||
_commandNotices.clear();
|
||||
_seedCommandNotice();
|
||||
_savedOffset = widget.scrollPositions?.read(widget.sessionId);
|
||||
_savedPosition = widget.scrollPositions?.read(widget.sessionId);
|
||||
_anchor = null;
|
||||
_anchorChosen = false;
|
||||
_placed = false;
|
||||
_following = true;
|
||||
_distanceFromBottom = null;
|
||||
@@ -425,6 +435,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
_restoring = false;
|
||||
_restoreOffset = null;
|
||||
_restoreMeasuredExtent = 0;
|
||||
_restoreMeasuredStart = 0;
|
||||
_lastOffset = 0;
|
||||
} else if (oldWidget.syncToken != widget.syncToken) {
|
||||
// The socket was replaced: conditions recorded before the gap are stale,
|
||||
@@ -489,11 +500,8 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
store.clear(id);
|
||||
return;
|
||||
}
|
||||
if (_scroll.hasClients) {
|
||||
store.write(id, _scroll.position.pixels);
|
||||
} else {
|
||||
store.write(id, _lastOffset);
|
||||
}
|
||||
final offset = _scroll.hasClients ? _scroll.position.pixels : _lastOffset;
|
||||
store.write(id, (offset: offset, anchor: _anchor));
|
||||
}
|
||||
|
||||
/// Measured distance from the end for the current content.
|
||||
@@ -535,11 +543,21 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
/// the operator left one, otherwise the newest message.
|
||||
void _placeFromStore() {
|
||||
if (!_scroll.hasClients) return;
|
||||
final saved = _savedPosition;
|
||||
if (_anchor != null &&
|
||||
saved == null &&
|
||||
_scroll.position.minScrollExtent >= 0) {
|
||||
// The rows above the centre do not fill the viewport, so a bottom-anchored
|
||||
// list would leave a gap above them. A transcript this short is cheap to
|
||||
// lay out whole: show it top-anchored, from its first row, as before.
|
||||
setState(() => _anchor = null);
|
||||
_scheduleFollow();
|
||||
return;
|
||||
}
|
||||
_placed = true;
|
||||
final saved = _savedOffset;
|
||||
_savedOffset = null;
|
||||
_savedPosition = null;
|
||||
if (saved != null) {
|
||||
_restoreOffset = saved;
|
||||
_restoreOffset = saved.offset;
|
||||
_restoring = true;
|
||||
_following = false;
|
||||
_newestAtRelease = null;
|
||||
@@ -564,10 +582,12 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
final target = chatRestoreOffset(
|
||||
savedOffset: _restoreOffset,
|
||||
maxScrollExtent: end,
|
||||
minScrollExtent: _scroll.position.minScrollExtent,
|
||||
);
|
||||
if (_scroll.position.pixels != target) _scroll.jumpTo(target);
|
||||
_lastOffset = target;
|
||||
_restoreMeasuredExtent = _scroll.position.maxScrollExtent;
|
||||
_restoreMeasuredStart = _scroll.position.minScrollExtent;
|
||||
_distanceFromBottom = chatBottomDistance(
|
||||
maxScrollExtent: _restoreMeasuredExtent,
|
||||
pixels: _scroll.position.pixels,
|
||||
@@ -590,8 +610,12 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
}
|
||||
if (_restoring) {
|
||||
final measuredBefore = _restoreMeasuredExtent;
|
||||
final startBefore = _restoreMeasuredStart;
|
||||
_applyRestore();
|
||||
if (_scroll.position.maxScrollExtent == measuredBefore) {
|
||||
// Bottom-anchored, the older rows' estimated extent above the centre
|
||||
// settles too, and a saved offset there is clamped to it.
|
||||
if (_scroll.position.maxScrollExtent == measuredBefore &&
|
||||
_scroll.position.minScrollExtent == startBefore) {
|
||||
// The measured extent stopped changing: the restored offset is the
|
||||
// operator's real position again, so follow intent can be judged.
|
||||
_restoring = false;
|
||||
@@ -680,6 +704,35 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
_restoreOffset = null;
|
||||
}
|
||||
|
||||
/// The index of the row the transcript is split at, or -1 to keep it
|
||||
/// top-anchored. The anchor is chosen once, when the history first arrives:
|
||||
/// a saved position keeps the anchor it was measured from.
|
||||
int _splitFor(List<JsonMap> rows) {
|
||||
if (!_controller.loaded) return -1;
|
||||
if (!_anchorChosen) {
|
||||
_anchorChosen = true;
|
||||
final saved = _savedPosition;
|
||||
if (saved != null &&
|
||||
saved.anchor != null &&
|
||||
chatTranscriptAnchorIndex(rows, saved.anchor) < 0) {
|
||||
// The row the position was measured from is gone: the offset no longer
|
||||
// names a place in this transcript, so it opens at the newest message.
|
||||
_savedPosition = null;
|
||||
}
|
||||
_anchor = _savedPosition == null
|
||||
? chatTranscriptAnchor(rows)
|
||||
: _savedPosition!.anchor;
|
||||
}
|
||||
var index = chatTranscriptAnchorIndex(rows, _anchor);
|
||||
if (index < 0 && _anchor != null) {
|
||||
// A repair replaced the transcript without the anchor row: split the new
|
||||
// one afresh (a following view is pinned to its end again after layout).
|
||||
_anchor = chatTranscriptAnchor(rows);
|
||||
index = chatTranscriptAnchorIndex(rows, _anchor);
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => AnimatedBuilder(
|
||||
animation: Listenable.merge([
|
||||
@@ -743,6 +796,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
// its previous messages, keeping the element (and its open disclosures)
|
||||
// across paging and streaming.
|
||||
final keys = _rowKeys.forRows(rows);
|
||||
final split = _splitFor(rows);
|
||||
final initialLoad = !_controller.loaded && _controller.loading;
|
||||
final historyNotice = _noticeOf(
|
||||
notices,
|
||||
@@ -825,11 +879,16 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
children: [
|
||||
// At rest the first item starts below the floating bar
|
||||
// and the last one ends above the floating composer.
|
||||
ListView(
|
||||
// Bottom-anchored at the split row, the newest rows are
|
||||
// laid out first and older ones only when scrolled to.
|
||||
ChatTranscriptScrollView(
|
||||
key: ValueKey<String>(
|
||||
'chat-thread-${widget.sessionId}',
|
||||
),
|
||||
controller: _scroll,
|
||||
splitAfter: split < 0
|
||||
? null
|
||||
: ValueKey<String>(keys[split]),
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
side,
|
||||
media.top + 8,
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// Where an opened transcript is split into its older and newer halves.
|
||||
///
|
||||
/// Opening a long conversation at its newest message used to lay out every row
|
||||
/// above it: a top-anchored list can only reach its end by measuring the rows
|
||||
/// in between. The transcript is instead a two-sided scroll view whose centre
|
||||
/// sits between an anchor row and the rows after it. The anchor row and every
|
||||
/// older row grow upwards from the centre; the rows after it (and the queued,
|
||||
/// unsent and command lines) grow downwards. Opening lays out only what shows.
|
||||
///
|
||||
/// The split is fixed for as long as the conversation is open, so growth on
|
||||
/// either side never moves the other: an older page loaded above, or a reply
|
||||
/// streaming below, leaves the operator's reading position where it is. The
|
||||
/// newest message row (and a reply that is still streaming) is never the
|
||||
/// anchor or above it, because a row above the centre that grew would push the
|
||||
/// rows above it up while the operator reads them.
|
||||
///
|
||||
/// Returns null when the transcript has fewer than two message rows: there is
|
||||
/// nothing older to keep unmeasured, and the view stays top-anchored.
|
||||
String? chatTranscriptAnchor(List<JsonMap> rows) {
|
||||
var cut = -1;
|
||||
for (var index = rows.length - 1; index >= 0; index--) {
|
||||
if (_messageIds(rows[index]).isNotEmpty) {
|
||||
cut = index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (var index = 0; index < rows.length; index++) {
|
||||
if (rows[index]['streaming'] == true && index < cut) {
|
||||
cut = index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (var index = cut - 1; index >= 0; index--) {
|
||||
final ids = _messageIds(rows[index]);
|
||||
if (ids.isNotEmpty) return ids.last;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// The index of the row that holds [anchor], or -1 when no row does (a repair
|
||||
/// replaced the transcript) or there is no anchor.
|
||||
int chatTranscriptAnchorIndex(List<JsonMap> rows, String? anchor) {
|
||||
if (anchor == null) return -1;
|
||||
for (var index = rows.length - 1; index >= 0; index--) {
|
||||
if (_messageIds(rows[index]).contains(anchor)) return index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// The stored message ids of one user or assistant row. Cards, activity lines
|
||||
/// and the pending-response shell carry none and can never be an anchor.
|
||||
List<String> _messageIds(JsonMap row) {
|
||||
final kind = row['kind'];
|
||||
if (kind == 'assistant') {
|
||||
final listed = row['messageIds'];
|
||||
if (listed is! List) return const [];
|
||||
return [
|
||||
for (final id in listed)
|
||||
if ('$id'.trim().isNotEmpty) '$id'.trim(),
|
||||
];
|
||||
}
|
||||
if (kind == 'user') {
|
||||
final id = '${row['key'] ?? ''}'.trim();
|
||||
return id.isEmpty ? const [] : [id];
|
||||
}
|
||||
return const [];
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// The transcript's scrollable: older rows above a centre, newer rows below.
|
||||
///
|
||||
/// With [splitAfter], the children up to and including the one with that key
|
||||
/// sit above the centre and the rest below it. The centre is at the viewport's
|
||||
/// bottom edge at offset zero: the older children grow upwards from it and the
|
||||
/// newer ones downwards, so the newest rows are laid out first and an older row
|
||||
/// only when it is scrolled to. Offsets above the centre are negative.
|
||||
///
|
||||
/// Without one (or when no child has that key) this is a plain top-anchored
|
||||
/// list starting at its first child. A transcript that fits its viewport stays
|
||||
/// top-anchored, so a short conversation still reads from the top.
|
||||
///
|
||||
/// [padding] keeps the first child below the floating bar and the last child
|
||||
/// above the floating composer, as a list's padding would.
|
||||
class ChatTranscriptScrollView extends StatelessWidget {
|
||||
const ChatTranscriptScrollView({
|
||||
super.key,
|
||||
required this.controller,
|
||||
required this.padding,
|
||||
required this.children,
|
||||
this.splitAfter,
|
||||
});
|
||||
|
||||
static const _centre = ValueKey<String>('chat-thread-newer');
|
||||
|
||||
final ScrollController controller;
|
||||
final EdgeInsets padding;
|
||||
final List<Widget> children;
|
||||
final Key? splitAfter;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final split = splitAfter == null
|
||||
? -1
|
||||
: children.indexWhere((child) => child.key == splitAfter);
|
||||
final anchored = split >= 0;
|
||||
return CustomScrollView(
|
||||
controller: controller,
|
||||
center: _centre,
|
||||
anchor: anchored ? 1 : 0,
|
||||
slivers: [
|
||||
// Before the centre, a sliver grows upwards: its first child sits at
|
||||
// the centre, so the older children go in newest-first.
|
||||
SliverPadding(
|
||||
padding: anchored ? padding.copyWith(bottom: 0) : EdgeInsets.zero,
|
||||
sliver: SliverList(
|
||||
delegate: SliverChildListDelegate([
|
||||
for (var index = split; index >= 0; index--) children[index],
|
||||
]),
|
||||
),
|
||||
),
|
||||
SliverPadding(
|
||||
key: _centre,
|
||||
padding: anchored ? padding.copyWith(top: 0) : padding,
|
||||
sliver: SliverList(
|
||||
delegate: SliverChildListDelegate(children.sublist(split + 1)),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
# Chat transcript open performance
|
||||
|
||||
Performance round 2 on opening a long chat. Started from measurements; no
|
||||
intended behaviour change apart from the differences listed below.
|
||||
Follows `notes/chat-stream-perf.md`.
|
||||
|
||||
## Finding
|
||||
|
||||
The transcript was a top-anchored `ListView`, and the view jumped to its end
|
||||
after the first layout. A list can reach its end only by laying out every row
|
||||
in between, so opening a 300-message chat built 301 message rows and parsed
|
||||
150 markdown answers in one frame before the newest message showed.
|
||||
|
||||
## Change
|
||||
|
||||
- `ChatTranscriptScrollView` (`lib/features/chat/conversation/`) is a
|
||||
`CustomScrollView` with a centre. Rows up to an anchor row sit in a sliver
|
||||
that grows upwards from the centre. Later rows, plus queued, unsent and
|
||||
command lines, sit in a sliver that grows downwards. With `anchor: 1`, offset
|
||||
0 puts the centre at the viewport's bottom, so opening lays out only the
|
||||
rows on screen.
|
||||
- `chatTranscriptAnchor` (`chat_transcript_anchor.dart`) picks the message row
|
||||
before the newest message row, and before any reply that is still
|
||||
streaming. The anchor is chosen once, when the history arrives, and stays
|
||||
fixed while the chat is open. Growth on either side therefore never moves
|
||||
the other side: older pages are prepended above, and replies stream below.
|
||||
- A transcript whose older side fits in the viewport (where `minScrollExtent`
|
||||
is 0 after the first layout) switches back to top-anchored, exactly as
|
||||
before. Short chats still start at the top with no gap above them.
|
||||
- Saved positions are now `({offset, anchor})`. An offset is restored only
|
||||
against the same anchor message id. If that row is gone, the chat opens at
|
||||
its newest message. While a restore settles, the code now also waits for
|
||||
`minScrollExtent` to settle.
|
||||
- Follow, the jump control, the arrival count, keyboard and composer insets,
|
||||
and per-message selection use the same logic as before. The end of the
|
||||
transcript is still `maxScrollExtent`.
|
||||
|
||||
## Numbers
|
||||
|
||||
Widget harness `test/features/chat/perf/chat_stream_rebuild_perf_test.dart`:
|
||||
debug VM, 1000x1600 viewport, two runs each. "Open" covers the frames from
|
||||
the first one that builds a message row to the one where the newest message
|
||||
shows.
|
||||
|
||||
| | before | after |
|
||||
| --- | --- | --- |
|
||||
| open 300: rows built / markdown parses | 301 / 150 | 4 / 2 |
|
||||
| open 300: frames / frame time sum / longest frame | 30 / 1212–1301 ms / 1090–1184 ms | 2 / 99–113 ms / 95–109 ms |
|
||||
| open 1000: rows built / markdown parses | 1001 / 500 | 4 / 2 |
|
||||
| open 1000: frames / frame time sum / longest frame | 38 / 2508–2616 ms / 2465–2566 ms | 2 / 47–54 ms / 44–51 ms |
|
||||
| stream 200 deltas (300 msgs): message builds | 200 | 200 |
|
||||
| stream: frame mean (debug) | 5.5–5.9 ms | 7.2–7.8 ms (build counts by widget type unchanged; not seen in release) |
|
||||
| scroll 80 drags: parses | 0 | 22 (rows not built at open are parsed the first time they show) |
|
||||
| scroll: frame mean | 5.5–6.0 ms | 5.4–6.4 ms |
|
||||
|
||||
Release web probe `node tool/chat_perf_probe.mjs <build> <n> 200`, built from
|
||||
`tool/chat_composer_probe_entry.dart`. Headless Chrome rasterises in software.
|
||||
Before and after runs alternated. The new open phase counts the frames that do
|
||||
work after the history reply (`PERF_HISTORY`).
|
||||
|
||||
| | before | after |
|
||||
| --- | --- | --- |
|
||||
| open 300: build sum (longest frame) | 608–1314 ms (590–1289) | 12.5–24 ms (5.8–11.9) |
|
||||
| open 300: total frame span sum | 1012–1878 ms | 218–490 ms |
|
||||
| open 1000: build sum (longest frame) | 1585–2945 ms (1565–2925) | 6.3–13.8 ms (4.8–8.5) |
|
||||
| open 1000: total frame span sum | 2101–3792 ms | 99–289 ms |
|
||||
| streaming build mean, 300 / 1000 | 6.1–7.2 / 11.3–16.5 ms | 5.9–12.0 / 11.5–19.5 ms (noise) |
|
||||
| scrolling build mean, 300 / 1000 | 1.6–2.5 / 1.7–2.6 ms | 2.3–5.1 / 2.0–3.3 ms |
|
||||
|
||||
For 300 messages, two before runs are used (the first overlapped another
|
||||
probe). The fixture's `_PlatformViewPlaceholderBox` null-check error, logged
|
||||
by the old layout on web, no longer appears. Browser captures of the
|
||||
300-message open, the scroll back, the short chat and the 12-message chat look
|
||||
the same before and after.
|
||||
|
||||
## Behaviour differences
|
||||
|
||||
- **Load older** keeps the rows the operator was reading in place, and the
|
||||
new page appears above them. Before, the view stayed at the top, so the
|
||||
operator landed on the oldest rows of the new page.
|
||||
- When the viewport shrinks while the operator reads above the end (keyboard,
|
||||
resize), the bottom of the view stays put instead of the top.
|
||||
- A transcript replaced by a repair that no longer contains the anchor row is
|
||||
split again. A view that is not following can then move.
|
||||
@@ -5,6 +5,7 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:pipelines_app/application/app_route.dart';
|
||||
import 'package:pipelines_app/application/app_router.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_transcript_scroll_view.dart';
|
||||
import 'package:pipelines_app/features/chat/screen.dart';
|
||||
import 'package:pipelines_app/shell/adaptive_shell.dart';
|
||||
import 'package:pipelines_app/shell/destination.dart';
|
||||
@@ -130,17 +131,16 @@ void main() {
|
||||
reason: 'Use the stock Navigator Back action without a chat override',
|
||||
);
|
||||
final transcript = find.byKey(const ValueKey('chat-thread-s'));
|
||||
final scroll = tester.widget<ListView>(transcript).controller!;
|
||||
await tester.drag(
|
||||
transcript,
|
||||
Offset(0, scroll.position.maxScrollExtent + 500),
|
||||
);
|
||||
final scroll = tester
|
||||
.widget<ChatTranscriptScrollView>(transcript)
|
||||
.controller;
|
||||
// A long transcript is bottom-anchored: older rows sit above offset 0.
|
||||
final span =
|
||||
scroll.position.maxScrollExtent - scroll.position.minScrollExtent;
|
||||
await tester.drag(transcript, Offset(0, span + 500));
|
||||
await tester.pumpAndSettle();
|
||||
expect(scroll.offset, closeTo(scroll.position.minScrollExtent, 1));
|
||||
await tester.drag(
|
||||
transcript,
|
||||
Offset(0, -scroll.position.maxScrollExtent - 500),
|
||||
);
|
||||
await tester.drag(transcript, Offset(0, -span - 500));
|
||||
await tester.pumpAndSettle();
|
||||
expect(scroll.offset, closeTo(scroll.position.maxScrollExtent, 1));
|
||||
// A continuously animated processing control must not keep Back open.
|
||||
|
||||
@@ -103,27 +103,39 @@ void main() {
|
||||
expect(chatRestoreOffset(savedOffset: 0, maxScrollExtent: 640), 0);
|
||||
expect(chatRestoreOffset(savedOffset: 900, maxScrollExtent: 640), 640);
|
||||
});
|
||||
|
||||
test('a bottom-anchored position above the centre stays negative', () {
|
||||
double restore(double saved) => chatRestoreOffset(
|
||||
savedOffset: saved,
|
||||
minScrollExtent: -2000,
|
||||
maxScrollExtent: 300,
|
||||
);
|
||||
expect(restore(-750), -750);
|
||||
expect(restore(-5000), -2000);
|
||||
expect(restore(900), 300);
|
||||
});
|
||||
});
|
||||
|
||||
group('store', () {
|
||||
test('remembers one conversation without touching another', () {
|
||||
final store = MemoryChatScrollStore();
|
||||
store.write('a', 120);
|
||||
store.write('b', 30);
|
||||
expect(store.read('a'), 120);
|
||||
expect(store.read('b'), 30);
|
||||
store.write('a', (offset: 120, anchor: null));
|
||||
store.write('b', (offset: -30, anchor: 'm4'));
|
||||
expect(store.read('a'), (offset: 120.0, anchor: null));
|
||||
expect(store.read('b'), (offset: -30.0, anchor: 'm4'));
|
||||
expect(store.read('c'), isNull);
|
||||
|
||||
store.clear('a');
|
||||
expect(store.read('a'), isNull);
|
||||
expect(store.entries, {'b': 30.0});
|
||||
expect(store.entries, {'b': (offset: -30.0, anchor: 'm4')});
|
||||
});
|
||||
|
||||
test('ignores an unusable offset instead of inventing a position', () {
|
||||
final store = MemoryChatScrollStore();
|
||||
store.write('a', double.nan);
|
||||
store.write('a', double.infinity);
|
||||
store.write('a', -5);
|
||||
store.write('a', (offset: double.nan, anchor: 'm1'));
|
||||
store.write('a', (offset: double.infinity, anchor: null));
|
||||
// Only a bottom-anchored offset counts from a centre and can be negative.
|
||||
store.write('a', (offset: -5, anchor: null));
|
||||
expect(store.read('a'), isNull);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart' as widgets;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_scroll.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
|
||||
|
||||
import 'chat_thread_fixtures.dart';
|
||||
import 'chat_thread_scroll_test.dart' show host, transcriptScroll;
|
||||
|
||||
// The transcript opens bottom-anchored: only the rows that show are built, and
|
||||
// growth on either side of the split never moves the operator's place. In the
|
||||
// 800x600 test viewport the first row starts below the floating header.
|
||||
const _firstRowTop = 80.0;
|
||||
|
||||
List<JsonMap> messages(int count, {int from = 0, int sentences = 6}) => [
|
||||
for (var index = from; index < from + count; index++)
|
||||
chatMessage(
|
||||
'm$index',
|
||||
index.isEven ? 'user' : 'assistant',
|
||||
parts: [
|
||||
{
|
||||
'type': 'text',
|
||||
'text':
|
||||
'Message $index. '
|
||||
'${List.filled(sentences, 'A sentence that wraps across the transcript width.').join(' ')}',
|
||||
},
|
||||
],
|
||||
),
|
||||
];
|
||||
|
||||
Finder row(String id) => find.byWidgetPredicate(
|
||||
(widget) =>
|
||||
widget is ChatMessageView &&
|
||||
(widget.row['key'] == id ||
|
||||
(widget.row['messageIds'] as List?)?.contains(id) == true),
|
||||
skipOffstage: false,
|
||||
);
|
||||
|
||||
Finder get thread => find.byKey(const Key('chat-thread-s'));
|
||||
|
||||
Future<void> dragUntil(WidgetTester tester, Finder target) async {
|
||||
for (var drag = 0; drag < 80 && !tester.any(target); drag++) {
|
||||
await tester.drag(thread, const Offset(0, 500));
|
||||
await tester.pump();
|
||||
}
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
Future<void> openThread(WidgetTester tester, FakeChatGateway gateway) async {
|
||||
await tester.pumpWidget(
|
||||
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('a long transcript opens at its newest message, building only '
|
||||
'what shows', (tester) async {
|
||||
final gateway = FakeChatGateway();
|
||||
addTearDown(gateway.close);
|
||||
gateway.history = (_) async => chatPage(messages(200));
|
||||
final built = <String>{};
|
||||
widgets.debugOnRebuildDirtyWidget = (element, _) {
|
||||
final widget = element.widget;
|
||||
if (widget is ChatMessageView) built.add('${widget.row['key']}');
|
||||
};
|
||||
addTearDown(() => widgets.debugOnRebuildDirtyWidget = null);
|
||||
|
||||
await openThread(tester, gateway);
|
||||
widgets.debugOnRebuildDirtyWidget = null;
|
||||
|
||||
final scroll = transcriptScroll(tester);
|
||||
expect(
|
||||
scroll.position.pixels,
|
||||
closeTo(scroll.position.maxScrollExtent, 0.5),
|
||||
);
|
||||
expect(scroll.position.minScrollExtent, lessThan(0));
|
||||
final newest = tester.getRect(row('m199'));
|
||||
expect(newest.bottom, lessThanOrEqualTo(600));
|
||||
expect(newest.bottom, greaterThan(400));
|
||||
expect(built, isNot(contains('m0')));
|
||||
expect(built.length, lessThan(20));
|
||||
expect(find.byKey(const Key('chat-jump-to-latest')), findsNothing);
|
||||
|
||||
// A reply streaming below the split is followed while at the end.
|
||||
gateway.emitEvent(chatTextFrame('Streaming answer. ', messageId: 'live'));
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(
|
||||
scroll.position.pixels,
|
||||
closeTo(scroll.position.maxScrollExtent, 0.5),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('scrolling up reaches the first row with no gap above it', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = FakeChatGateway();
|
||||
addTearDown(gateway.close);
|
||||
gateway.history = (_) async => chatPage(messages(40));
|
||||
await openThread(tester, gateway);
|
||||
|
||||
await dragUntil(tester, row('m0'));
|
||||
await tester.fling(thread, const Offset(0, 3000), 3000);
|
||||
await tester.pumpAndSettle();
|
||||
final scroll = transcriptScroll(tester);
|
||||
expect(
|
||||
scroll.position.pixels,
|
||||
closeTo(scroll.position.minScrollExtent, 0.5),
|
||||
);
|
||||
expect(tester.getRect(row('m0')).top, closeTo(_firstRowTop, 1));
|
||||
expect(find.byKey(const Key('chat-jump-to-latest')), findsOneWidget);
|
||||
});
|
||||
|
||||
for (final tall in [false, true]) {
|
||||
testWidgets('a short transcript still reads from the top '
|
||||
'(tall newest row: $tall)', (tester) async {
|
||||
final gateway = FakeChatGateway();
|
||||
addTearDown(gateway.close);
|
||||
// The rows above the newest one fit the viewport.
|
||||
gateway.history = (_) async => chatPage([
|
||||
...messages(2, sentences: 1),
|
||||
...messages(1, from: 2, sentences: tall ? 60 : 1),
|
||||
]);
|
||||
await openThread(tester, gateway);
|
||||
|
||||
final scroll = transcriptScroll(tester);
|
||||
expect(scroll.position.minScrollExtent, 0);
|
||||
expect(
|
||||
scroll.position.pixels,
|
||||
closeTo(scroll.position.maxScrollExtent, 0.5),
|
||||
);
|
||||
if (!tall) {
|
||||
expect(scroll.position.maxScrollExtent, 0);
|
||||
expect(tester.getRect(row('m0')).top, closeTo(_firstRowTop, 1));
|
||||
return;
|
||||
}
|
||||
// At its end the tall newest row shows its end; scrolling back reaches
|
||||
// the first row at the top padding.
|
||||
expect(tester.getRect(row('m2')).bottom, lessThanOrEqualTo(600));
|
||||
await tester.fling(thread, const Offset(0, 3000), 3000);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.getRect(row('m0')).top, closeTo(_firstRowTop, 1));
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('loading an older page keeps the rows the operator reads in '
|
||||
'place', (tester) async {
|
||||
final gateway = FakeChatGateway();
|
||||
addTearDown(gateway.close);
|
||||
gateway.history = (_) async =>
|
||||
chatPage(messages(30, from: 30), cursor: 'older', hasMore: true);
|
||||
await openThread(tester, gateway);
|
||||
|
||||
await dragUntil(tester, find.text('Load older'));
|
||||
await tester.fling(thread, const Offset(0, 3000), 3000);
|
||||
await tester.pumpAndSettle();
|
||||
final held = tester.getRect(row('m30'));
|
||||
gateway.history = (_) async => chatPage(messages(30));
|
||||
await tester.tap(find.text('Load older'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Load older'), findsNothing);
|
||||
expect(tester.getRect(row('m30')).top, closeTo(held.top, 0.5));
|
||||
// The older page sits above, reachable by scrolling up.
|
||||
await dragUntil(tester, row('m0'));
|
||||
await tester.fling(thread, const Offset(0, 3000), 3000);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.getRect(row('m0')).top, closeTo(_firstRowTop, 1));
|
||||
});
|
||||
|
||||
testWidgets('a reopened conversation keeps its place after messages arrived '
|
||||
'while it was closed', (tester) async {
|
||||
final gateway = FakeChatGateway();
|
||||
addTearDown(gateway.close);
|
||||
gateway.history = (_) async => chatPage(messages(60));
|
||||
final positions = MemoryChatScrollStore();
|
||||
Widget view() => host(
|
||||
ChatThreadView(
|
||||
gateway: gateway,
|
||||
sessionId: 's',
|
||||
scrollPositions: positions,
|
||||
onBack: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.pumpWidget(view());
|
||||
await tester.pumpAndSettle();
|
||||
for (var drag = 0; drag < 4; drag++) {
|
||||
await tester.drag(thread, const Offset(0, 400));
|
||||
await tester.pump();
|
||||
}
|
||||
await tester.pumpAndSettle();
|
||||
final shown = find.byWidgetPredicate((widget) => widget is ChatMessageView);
|
||||
final top = shown.evaluate().first.widget as ChatMessageView;
|
||||
final id = '${top.row['key']}';
|
||||
final held = tester.getRect(row(id));
|
||||
expect(positions.read('s')!.anchor, isNotNull);
|
||||
|
||||
await tester.pumpWidget(host(const SizedBox()));
|
||||
gateway.history = (_) async => chatPage(messages(64));
|
||||
await tester.pumpWidget(view());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(tester.getRect(row(id)).top, closeTo(held.top, 1));
|
||||
expect(find.text('Jump to latest · 4 new messages'), findsNothing);
|
||||
expect(find.byKey(const Key('chat-jump-to-latest')), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -35,8 +35,14 @@ JsonMap tallPage() => chatPage([
|
||||
),
|
||||
]);
|
||||
|
||||
ScrollController transcriptScroll(WidgetTester tester) =>
|
||||
tester.widget<ListView>(find.byKey(const Key('chat-thread-s'))).controller!;
|
||||
ScrollController transcriptScroll(WidgetTester tester) => tester
|
||||
.widget<CustomScrollView>(
|
||||
find.descendant(
|
||||
of: find.byKey(const Key('chat-thread-s')),
|
||||
matching: find.byType(CustomScrollView),
|
||||
),
|
||||
)
|
||||
.controller!;
|
||||
|
||||
void main() {
|
||||
testWidgets('a streaming transcript stays pinned to its newest end', (
|
||||
@@ -181,14 +187,14 @@ void main() {
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
final saved = scroll.position.pixels;
|
||||
expect(saved, greaterThan(0));
|
||||
expect(positions.read('s'), closeTo(saved, 0.5));
|
||||
expect(saved, lessThan(scroll.position.maxScrollExtent - 300));
|
||||
expect(positions.read('s')!.offset, closeTo(saved, 0.5));
|
||||
|
||||
// Leaving the conversation disposes its view but not the destination's
|
||||
// store.
|
||||
await tester.pumpWidget(host(const SizedBox()));
|
||||
await tester.pumpAndSettle();
|
||||
expect(positions.read('s'), closeTo(saved, 0.5));
|
||||
expect(positions.read('s')!.offset, closeTo(saved, 0.5));
|
||||
|
||||
await tester.pumpWidget(
|
||||
host(
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_transcript_anchor.dart';
|
||||
|
||||
JsonMap user(String id) => {'kind': 'user', 'key': id};
|
||||
|
||||
JsonMap assistant(List<String> ids, {bool streaming = false}) => {
|
||||
'kind': 'assistant',
|
||||
'key': ids.isEmpty ? 'chat-pending-response' : ids.first,
|
||||
'messageIds': ids,
|
||||
if (streaming) 'streaming': true,
|
||||
};
|
||||
|
||||
void main() {
|
||||
test('splits before the newest message row', () {
|
||||
final rows = [
|
||||
user('u1'),
|
||||
assistant(['a2', 'a3']),
|
||||
user('u4'),
|
||||
];
|
||||
expect(chatTranscriptAnchor(rows), 'a3');
|
||||
expect(chatTranscriptAnchorIndex(rows, 'a3'), 1);
|
||||
// Any message the folded turn holds finds it, as paging prepends into it.
|
||||
expect(chatTranscriptAnchorIndex(rows, 'a2'), 1);
|
||||
});
|
||||
|
||||
test('a streaming reply and the rows after it stay below the centre', () {
|
||||
// A queued follow-up can be recorded after the reply still arriving.
|
||||
final rows = [
|
||||
user('u1'),
|
||||
assistant(['a2']),
|
||||
user('u3'),
|
||||
assistant(['a4'], streaming: true),
|
||||
user('u5'),
|
||||
];
|
||||
expect(chatTranscriptAnchor(rows), 'u3');
|
||||
});
|
||||
|
||||
test('cards, activity lines and the pending shell are never the anchor', () {
|
||||
final rows = [
|
||||
user('u1'),
|
||||
{'kind': 'activity', 'key': 'chat-activity-a2'},
|
||||
assistant(['a2']),
|
||||
assistant(const [], streaming: true),
|
||||
{'kind': 'permission', 'key': 'chat-permission-p1'},
|
||||
];
|
||||
// The newest message row is a2, so the anchor is the row before it.
|
||||
expect(chatTranscriptAnchor(rows), 'u1');
|
||||
});
|
||||
|
||||
test('fewer than two message rows stay top-anchored', () {
|
||||
expect(chatTranscriptAnchor(const []), isNull);
|
||||
expect(chatTranscriptAnchor([user('u1')]), isNull);
|
||||
expect(
|
||||
chatTranscriptAnchor([
|
||||
user('u1'),
|
||||
{'kind': 'question', 'key': 'chat-question-q1'},
|
||||
]),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('a missing anchor reports no split', () {
|
||||
final rows = [user('u1'), user('u2')];
|
||||
expect(chatTranscriptAnchorIndex(rows, null), -1);
|
||||
expect(chatTranscriptAnchorIndex(rows, 'gone'), -1);
|
||||
});
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -42,6 +42,7 @@ class _ProbeGateway implements ApiGateway {
|
||||
],
|
||||
};
|
||||
case 'chat_messages':
|
||||
markChatPerfHistory();
|
||||
return {
|
||||
'chat_history': {
|
||||
'messages': [
|
||||
|
||||
@@ -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,22 @@ 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');
|
||||
// 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;
|
||||
|
||||
@@ -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 that do
|
||||
// work after the history reply), 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 = { frame: allFrames.length };
|
||||
}
|
||||
if (text.startsWith('PERF_FRAME ')) {
|
||||
allFrames.push({ 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,19 @@ async function phase(origin, query, drive) {
|
||||
|
||||
const server = await serveBuild(dir);
|
||||
try {
|
||||
const open = await phase(server.origin, `perf=${messages}`, async ({ opened }) => {
|
||||
// The fixture flushes the timings 2.5 s after the history reply.
|
||||
const end = Date.now() + 20000;
|
||||
while (!opened().historyAt && Date.now() < end) await delay(100);
|
||||
await delay(3500);
|
||||
const { historyAt, allFrames } = opened();
|
||||
if (!historyAt) return { open: null };
|
||||
// Frames that did work after the history reply: the cost of opening. The
|
||||
// fixture's flush frames do none.
|
||||
const after = allFrames.slice(historyAt.frame).map((x) => x.f).filter((f) => f[0] >= 1000);
|
||||
const sum = (i) => +(after.reduce((a, f) => a + f[i], 0) / 1000).toFixed(1);
|
||||
return { open: { ...stats(after), buildSumMs: sum(0), totalSumMs: sum(2) } };
|
||||
});
|
||||
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 +124,9 @@ 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();
|
||||
}
|
||||
// A keep-alive connection can hold the server open after close.
|
||||
process.exit(0);
|
||||
|
||||
Reference in New Issue
Block a user