Merge: perf round 3 — run detail builds hidden tabs after the transition

This commit is contained in:
2026-11-02 13:55:25 +00:00
10 changed files with 720 additions and 11 deletions
@@ -467,6 +467,7 @@ class _RunDetailTabsState extends State<RunDetailTabs> {
final body = FadeThroughStack(
key: ValueKey('run-detail-body-${widget.run['id']}'),
index: tabs.indexOf(shown),
lazy: true,
children: [for (final tab in tabs) _body(tab)],
);
if (edge == FloatingBarEdge.top &&
+40 -1
View File
@@ -1,3 +1,4 @@
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'motion.dart';
@@ -7,15 +8,23 @@ import 'motion.dart';
/// Unlike [AnimatedSwitcher], this never removes a page after it has been
/// visited, so scroll positions, controllers and local state remain owned by
/// their original element tree.
///
/// With [lazy], the stack first builds only the page on screen. It builds
/// the others one per frame once no animation is running, or at once when one
/// is shown first. Opening a page with several tabs then builds one tab during
/// its route transition, and the first switch to another tab still usually
/// finds it built.
class FadeThroughStack extends StatefulWidget {
const FadeThroughStack({
super.key,
required this.index,
required this.children,
this.lazy = false,
});
final int index;
final List<Widget> children;
final bool lazy;
@override
State<FadeThroughStack> createState() => _FadeThroughStackState();
@@ -30,11 +39,35 @@ class _FadeThroughStackState extends State<FadeThroughStack>
late int _visibleIndex = widget.index;
int? _outgoingIndex;
/// The pages built so far; with [FadeThroughStack.lazy], the rest are not
/// built yet.
late final Set<int> _visited = {widget.index};
@override
void initState() {
super.initState();
_controller.value = 1;
_controller.addStatusListener(_statusChanged);
if (widget.lazy) _buildNextWhenIdle();
}
/// After each frame: once no animation is running (no transient frame
/// callbacks, such as a route transition's), builds the first page not
/// built yet, one per frame, until every page is built.
void _buildNextWhenIdle() {
SchedulerBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
final count = widget.children.length;
final next = [
for (var i = 0; i < count; i++)
if (!_visited.contains(i)) i,
];
if (next.isEmpty) return;
if (SchedulerBinding.instance.transientCallbackCount == 0) {
setState(() => _visited.add(next.first));
}
_buildNextWhenIdle();
});
}
@override
@@ -74,6 +107,7 @@ class _FadeThroughStackState extends State<FadeThroughStack>
@override
Widget build(BuildContext context) {
final reduced = Motion.of(context, Motion.tabFadeThrough) == Duration.zero;
_visited.add(widget.index);
if (reduced) {
_outgoingIndex = null;
_visibleIndex = widget.index;
@@ -129,7 +163,12 @@ class _FadeThroughStackState extends State<FadeThroughStack>
excluding: !active,
child: Transform.scale(
scale: scale,
child: Opacity(opacity: opacity, child: widget.children[index]),
child: Opacity(
opacity: opacity,
child: !widget.lazy || _visited.contains(index)
? widget.children[index]
: const SizedBox.shrink(),
),
),
),
),
+122
View File
@@ -0,0 +1,122 @@
# Performance round 3: survey and lazy run-detail tabs
This round started from measurements. It follows `notes/chat-stream-perf.md`,
`notes/chat-open-perf.md` and `notes/perf-glass-chrome.md`.
## Tools
- `tool/perf/interaction_bench.mjs` runs scripted interactions over the release
full-app fixture (`tool/search_selection_probe_entry.dart`) at 560x900,
Material, light theme. It clicks points, so Flutter semantics stay off. It
records Flutter FrameTiming (`tool/perf/frame_log.dart`, `?perf=1`) and
prints medians: build sum and max, raster sum and max, and the longest frame.
`--frames` prints every frame. `--cpuprofile` together with a
`--profile --source-maps` build assigns V8 samples to Dart files
(`tool/perf/cpu_attribution.mjs`).
- `tool/perf/perf_fixture.dart` adds fixture data: `?hits=N` Search hits,
`?chats=N` chats (each opens a short markdown transcript), and `?graph=1` (run
7 includes its graph).
- `tool/perf/js_size_by_source.mjs` splits main.dart.js by Dart source.
Headless Chrome has no GPU, so CanvasKit rasterises on the CPU and raster time
dominates every frame (see the glass note: with the GPU, frames take about
2-3 ms). Build time is the part that the app's code controls.
## Survey (base `main` ec619234, medians of 5)
Ranked by the worst build frame (ms):
| interaction | build max | build sum | longest frame |
| --- | --- | --- | --- |
| Chat tab, first visit, 150 chats | 116.5 | 189 | 143 |
| Settings, from Pipelines | 33.6 | 65 | 46 |
| Run detail open, with graph preview | 27-30 | 83-91 | 60-67 |
| Run detail open (runs list, 2 or 400 runs) | 24-29 | 72-90 | 55-66 |
| Search, typing `release` (209 hits) | 27.5 | 127 (24 frames) | 48 |
| Chat tab, first visit, 30 chats | 27-35 | 74-83 | 52-58 |
| Pipeline detail open | 16-22 | 55-66 | 56-60 |
| Run graph full screen | 20.8 | 35 | 65 |
| Chat list to open chat (20 messages) | 20.3 | 75 | 61 |
| Run Timeline tab, 400 steps | 17-25 | 36-46 | 33-44 |
| Main tab switch (Runs/Pipelines/Search) | 15-19 | 50-60 | 40-46 |
| Visit page from Timeline | 12-20 | 36-49 | 54-59 |
| Run page tabs (Summary/Artifacts/Changes) | 10-18 | 28-37 | 35-40 |
| Pipeline Graph and Source tabs | 8-16 | 27-37 | 37-42 |
| Artifact viewer open (the fixture serves no file) | 4.3 | 10 | 29 |
| Startup | 9.4 | 37 (15 frames) | 27 |
Startup: the first rasterised frame comes 300-400 ms after navigation with a
warm cache and about 1.5 s cold. A cold load spends about 1.15 s fetching and
compiling canvaskit.wasm. main.dart.js is 4.0 MiB (1.2 MiB gzipped), and app
code is about 15% of it (chat 171 KiB, the largest feature). Deferred loading
would save well under 100 KiB gzipped per feature, so it is not worth doing.
The icon fonts are already tree-shaken (MaterialIcons 1.6 MB to 18 KB).
Startup does no heavy synchronous work.
## Fix: lazy run-detail tabs
Profiling showed that opening a run detail built and laid out every page tab
offstage (Timeline, Artifacts, Changes) during the route transition, because
`FadeThroughStack` mounts all of its children. `FadeThroughStack(lazy: true)`
now builds only the shown page at first. It builds the rest one per frame once
no animation is running (no transient frame callbacks, so after the route
transition), or at once when one is shown first. After that, every page stays
mounted, as before. Only `RunDetailTabs` sets `lazy`. Making pipeline detail
and visit-page sections lazy gave no difference beyond noise, so those stay as
they were.
Before and after, `runs->run detail`, three runs each with `--frames`. Each
cell is the build time of the frames during the route transition (the first
9 frames):
| | before | after |
| --- | --- | --- |
| transition build sum | 62-103 ms | 38-59 ms |
| worst transition build frame | 20.7-54.1 ms | 16-34 ms |
Medians of 5, two rounds alternating with `main`:
| | before | after |
| --- | --- | --- |
| open: build max | 25.6 / 26.4 | 22.9 / 19.2 (now an idle prebuild frame) |
| open, 400 runs: build max | 28.9 / 28.5 | 21.0 / 22.5 |
| open with graph preview: build max | 25.3 / 28.2 | 21.2 |
| first switch to Timeline: build max | 13.0 / 9.8 | 14.1 / 16.3 (noise) |
| first switch to Timeline, 400 steps | 17.5 / 17.3 | 18.0 |
| Artifacts, Changes switches | 14.6-16.1 | 9.4-15.1 |
The build sum for the open window goes up by about 10 ms, because the pages
built later now take frames of their own. Those frames come after the
transition, while nothing animates. A first attempt that built pages only when
shown made the open cheaper (build max about 16 ms), but it moved the cost to
the first switch to Timeline (build max 17 to 24 ms, 400 steps 21 to 28 ms).
The idle prebuild avoids that.
Behaviour: no visible change. Within the first frames after a run detail
opens, a tab the operator has not shown is not mounted yet. Its offstage
widgets (for example the artifact gallery's thumbnails) therefore start a few
frames later. If an animation runs without stopping, unshown tabs are built
when they are first shown.
## Candidates for round 4
1. Chat list rows (largest value). `MaterialSessionList` and
`CupertinoSessionList` build every row of an open section in a `Column`
inside `CollapsibleListSection`'s `AnimatedSize`. The first Chat visit costs
about 0.75 ms of build per chat: 35 ms at 30 chats and 116 ms at 150, which
is a dropped-frame burst during the tab fade. Moving to slivers (heading
plus a lazy `SliverList`) fixes the scaling, but the section's size
animation has to be rebuilt, both for open and close (explicit
`Align.heightFactor`) and for rows arriving into an open section. Without
that it is a visual change, so it needs its own careful item.
2. Settings (33 ms build frame). Everything sits in one `SingleChildScrollView`
(about 1350 px of content). A lazy list would dispose offscreen sections that
own controllers, so it needs keep-alives or lifted state. Medium value: the
page is opened rarely.
3. Run detail open, other costs (about 15-20 ms left): the header plus Summary,
and `detailTimeline` and other step derivations that `_body` still runs for
unbuilt tabs on every rebuild. Builders instead of child widgets would
remove that work. Low to medium value.
4. Search result frame (27 ms for the first page of rows). Low value.
5. Cold start: canvaskit.wasm makes up most of the 1.5 s cold first frame.
Only caching and service-worker policy could help. Out of the app's control.
+52 -10
View File
@@ -34,17 +34,19 @@ class _PageState extends State<_Page> {
final tapped = <String>[];
Widget _host(int index, {bool reduced = false}) => MaterialApp(
home: MediaQuery(
data: MediaQueryData(disableAnimations: reduced),
child: Scaffold(
body: FadeThroughStack(
index: index,
children: const [_Page('a'), _Page('b'), _Page('c')],
Widget _host(int index, {bool reduced = false, bool lazy = false}) =>
MaterialApp(
home: MediaQuery(
data: MediaQueryData(disableAnimations: reduced),
child: Scaffold(
body: FadeThroughStack(
index: index,
lazy: lazy,
children: const [_Page('a'), _Page('b'), _Page('c')],
),
),
),
),
),
);
);
double _opacityOf(WidgetTester tester, String name) => tester
.widget<Opacity>(
@@ -129,4 +131,44 @@ void main() {
await tester.pump();
expect(_opacityOf(tester, 'b'), 1.0);
});
testWidgets('a lazy stack builds the shown page, then the rest when idle', (
tester,
) async {
await tester.pumpWidget(_host(0, lazy: true));
expect(find.text('page a'), findsOneWidget);
expect(find.text('page b', skipOffstage: false), findsNothing);
expect(find.text('page c', skipOffstage: false), findsNothing);
// One page per idle frame: b first, then c in a later frame.
var frames = 0;
Future<void> frameUntil(String page) async {
while (find.text(page, skipOffstage: false).evaluate().isEmpty) {
expect(frames++, lessThan(20));
await tester.pump(const Duration(milliseconds: 16));
}
}
await frameUntil('page b');
expect(find.text('page c', skipOffstage: false), findsNothing);
await frameUntil('page c');
expect(_onstage(tester, 'b'), isFalse);
expect(_onstage(tester, 'c'), isFalse);
});
testWidgets('a lazy stack keeps each page and its state', (tester) async {
await tester.pumpWidget(_host(0, lazy: true));
await tester.enterText(find.byKey(const Key('field a')), 'draft');
final first = tester.state(find.byType(_Page).first);
await tester.pumpWidget(_host(2, lazy: true));
await tester.pump(Motion.tabFadeThrough * 2);
expect(find.text('page c'), findsOneWidget);
expect(_opacityOf(tester, 'c'), 1.0);
await tester.pumpWidget(_host(0, lazy: true));
await tester.pumpAndSettle();
expect(tester.state(find.byType(_Page).first), same(first));
expect(find.text('draft'), findsOneWidget);
});
}
+105
View File
@@ -0,0 +1,105 @@
// Attributes a V8 CPU profile of the fixture app to Dart source files through
// the build's source map (`flutter build web --profile --source-maps`), for
// tool/perf/interaction_bench.mjs --cpuprofile.
//
// Each sample's stack is mapped frame by frame (a JS function's start position
// to its Dart file and line); a file or function counts once per sample, so
// the numbers are inclusive milliseconds. Frames without a mapping (CanvasKit,
// the browser) are grouped by their script.
import { readFile } from 'node:fs/promises';
const B64 = Object.fromEntries([...'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'].map((c, i) => [c, i]));
function decodeVlq(segment) {
const out = [];
let value = 0;
let shift = 0;
for (const c of segment) {
const digit = B64[c];
value += (digit & 31) << shift;
if (digit & 32) { shift += 5; continue; }
out.push(value & 1 ? -(value >>> 1) : value >>> 1);
value = 0;
shift = 0;
}
return out;
}
/** Loads `<dir>/main.dart.js.map`: lookup(line, column) -> { source, line } or null. */
export async function loadSourceMap(dir) {
const map = JSON.parse(await readFile(`${dir}/main.dart.js.map`, 'utf8'));
const lines = [];
let src = 0;
let srcLine = 0;
let srcCol = 0;
let name = 0;
map.mappings.split(';').forEach((line) => {
const segments = [];
let col = 0;
for (const seg of line.split(',')) {
if (!seg) continue;
const v = decodeVlq(seg);
col += v[0];
if (v.length >= 4) {
src += v[1]; srcLine += v[2]; srcCol += v[3];
if (v.length >= 5) name += v[4];
segments.push([col, src, srcLine, v.length >= 5 ? name : -1]);
}
}
lines.push(segments);
});
return (line, column) => {
const segs = lines[line];
if (!segs?.length) return null;
let lo = 0;
let hi = segs.length - 1;
let hit = null;
while (lo <= hi) {
const mid = (lo + hi) >> 1;
if (segs[mid][0] <= column) { hit = segs[mid]; lo = mid + 1; } else hi = mid - 1;
}
hit ??= segs[0];
const source = map.sources[hit[1]]
.replace(/^.*\/packages\/flutter\/lib\/src\//, 'flutter/')
.replace(/^.*\/pipelines_app\/lib\//, 'app:')
.replace(/^(\.\.\/)+lib\//, 'app:')
.replace(/^org-dartlang-sdk:\/\/\/(lib\/)?/, 'sdk:');
return { source, line: hit[2] + 1, name: hit[3] >= 0 ? map.names[hit[3]] : null };
};
}
/**
* Inclusive ms per Dart file and per Dart function (`file:line name`) across
* the profile's samples; [filter] keeps the keys worth printing.
*/
export function attribute(profile, lookup, { n = 30, filter = () => true } = {}) {
const byId = new Map(profile.nodes.map((node) => [node.id, node]));
const parent = new Map();
for (const node of profile.nodes) for (const c of node.children ?? []) parent.set(c, node.id);
const where = new Map();
for (const node of profile.nodes) {
const f = node.callFrame;
const mapped = f.url.endsWith('main.dart.js') ? lookup(f.lineNumber, f.columnNumber) : null;
where.set(node.id, mapped
? { file: mapped.source, fn: `${mapped.source}:${mapped.line} ${f.functionName}` }
: { file: `(${f.url.split('/').pop() || f.functionName})`, fn: null });
}
const files = new Map();
const fns = new Map();
let busy = 0;
profile.samples.forEach((id, i) => {
const dt = (profile.timeDeltas[i + 1] ?? 0) / 1000;
if (['(idle)', '(program)'].includes(byId.get(id).callFrame.functionName)) return;
busy += dt;
const seenFiles = new Set();
const seenFns = new Set();
for (let cur = id; cur !== undefined; cur = parent.get(cur)) {
const w = where.get(cur);
if (!seenFiles.has(w.file)) { seenFiles.add(w.file); files.set(w.file, (files.get(w.file) ?? 0) + dt); }
if (w.fn && !seenFns.has(w.fn)) { seenFns.add(w.fn); fns.set(w.fn, (fns.get(w.fn) ?? 0) + dt); }
}
});
const r1 = (x) => Math.round(x * 10) / 10;
const top = (m) => [...m].filter(([k]) => filter(k)).sort((a, b) => b[1] - a[1]).slice(0, n).map(([k, v]) => `${r1(v)} ${k}`);
return { busyMs: r1(busy), files: top(files), functions: top(fns) };
}
+35
View File
@@ -0,0 +1,35 @@
// Frame report for the interaction bench (tool/perf/interaction_bench.mjs).
//
// With `?perf=1` the full-app fixture prints every frame's Flutter timings to
// the console as `PERF_FRAME <build us> <raster us> <total us>`, and the first
// rasterised frame as `PERF_FIRST_FRAME <ms since navigation>`.
// `window.__perfKick()` schedules a frame, so the engine hands over timings it
// is still holding back (the web engine reports them in batches). Without the
// flag nothing is installed.
import 'dart:js_interop';
import 'package:flutter/widgets.dart';
import 'package:web/web.dart' as web;
@JS('__perfKick')
external set _perfKick(JSFunction kick);
void installFrameLog(Map<String, String> params) {
if (params['perf'] != '1') return;
final binding = WidgetsFlutterBinding.ensureInitialized();
binding.addTimingsCallback((timings) {
for (final timing in timings) {
// ignore: avoid_print
print(
'PERF_FRAME ${timing.buildDuration.inMicroseconds} '
'${timing.rasterDuration.inMicroseconds} '
'${timing.totalSpan.inMicroseconds}',
);
}
});
binding.waitUntilFirstFrameRasterized.then((_) {
// ignore: avoid_print
print('PERF_FIRST_FRAME ${web.window.performance.now().round()}');
});
_perfKick = (() => binding.scheduleFrame()).toJS;
}
+174
View File
@@ -0,0 +1,174 @@
#!/usr/bin/env node
// Interaction bench over the full-app fixture (tool/search_selection_probe_entry.dart,
// built release). For each scenario it loads a route with `?perf=1`, runs the
// setup steps, then records Flutter's FrameTiming (tool/perf/frame_log.dart)
// for the measured steps until the page settles, and prints per-scenario
// medians: frames, build sum / max, raster sum / max and the longest frame.
// Startup reports the first rasterised frame (ms since navigation) and the
// main.dart.js / canvaskit fetch times.
//
// --cpuprofile records a V8 CPU profile of the measured steps (first rep),
// writes `<scenario>.cpuprofile` and prints the functions with the most
// inclusive and self time; use a `flutter build web --profile` build so the
// names are readable; add --source-maps to attribute time to Dart files
// (tool/perf/cpu_attribution.mjs). --frames prints every measured frame's
// build and raster time; --shots writes a screenshot after each scenario.
//
// node tool/perf/interaction_bench.mjs --build build/probe-app --out /tmp/bench \
// [--repeat 5] [--only name,name] [--size 560x900] [--shots] [--cpuprofile] [--frames]
//
// Clicks are points (CSS px at 560x900, Material, light), so Flutter's
// semantics tree stays off as for most users. Headless Chrome without a GPU:
// CanvasKit rasterises on the CPU, so compare builds with each other rather
// than predicting a device.
import { mkdir, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { parseArgs } from 'node:util';
import { setTimeout as delay } from 'node:timers/promises';
import { openBrowser } from '../capture/browser.mjs';
import { serveBuild } from '../capture/serve.mjs';
import { attribute, loadSourceMap } from './cpu_attribution.mjs';
const { values: opts } = parseArgs({
options: {
build: { type: 'string' }, out: { type: 'string' }, repeat: { type: 'string', default: '5' },
only: { type: 'string' }, cpuprofile: { type: 'boolean' }, frames: { type: 'boolean' }, shots: { type: 'boolean' }, size: { type: 'string', default: '560x900' },
},
});
if (!opts.build || !opts.out) throw new Error('usage: --build <dir> --out <dir>');
const [width, height] = opts.size.split('x').map(Number);
// Bottom tab bar and top-bar points at 560x900 (Material).
const TAB = { first: [337, 854], second: [396, 854], third: [455, 854], fourth: [514, 854] };
const TOP_RIGHT = [524, 32];
const FIRST_ROW = [280, 138];
// `setup` runs unrecorded after load; `act` is recorded. A step is
// `{ click: [x, y] }`, `{ type: 'text' }` (one character per 120 ms), `{ wait }`.
const SCENARIOS = [
{ name: 'startup', route: '/runs', act: [] },
{ name: 'tab:runs->pipelines', route: '/runs', act: [{ click: TAB.first }] },
{ name: 'tab:pipelines->runs', route: '/pipelines', act: [{ click: TAB.second }] },
{ name: 'tab:runs->search', route: '/runs', act: [{ click: TAB.fourth }] },
{ name: 'tab:runs->chat', route: '/runs', q: { chats: '30' }, act: [{ click: TAB.third }] },
{ name: 'tab:runs->chat (150 chats)', route: '/runs', q: { chats: '150' }, act: [{ click: TAB.third }] },
{ name: 'startup (150 chats)', route: '/runs', q: { chats: '150' }, act: [] },
{ name: 'runs(400)->run detail', route: '/runs', q: { runs: '400' }, act: [{ click: FIRST_ROW }] },
{ name: 'runs->run detail', route: '/runs', act: [{ click: FIRST_ROW }] },
{ name: 'run tab:summary->timeline', route: '/runs/7', act: [{ click: TAB.second }] },
{ name: 'run tab:timeline(400)', route: '/runs/7', q: { steps: '400' }, act: [{ click: TAB.second }] },
{ name: 'run tab:->artifacts', route: '/runs/7', act: [{ click: TAB.third }] },
{ name: 'run tab:->changes', route: '/runs/7', act: [{ click: TAB.fourth }] },
{ name: 'timeline->visit page', route: '/runs/7', setup: [{ click: TAB.second }], act: [{ click: [280, 150] }] },
{ name: 'run tab:timeline->summary', route: '/runs/7', setup: [{ click: TAB.second }], act: [{ click: TAB.first }] },
{ name: 'run detail with graph preview', route: '/runs', q: { graph: '1' }, act: [{ click: FIRST_ROW }] },
{ name: 'run graph full screen', route: '/runs/7', q: { graph: '1' }, act: [{ click: [512, 152] }] },
{ name: 'artifact viewer open', route: '/runs/7', setup: [{ click: TAB.third }], act: [{ click: [280, 376] }] },
{ name: 'search type "release" (200 hits)', route: '/search', q: { hits: '200' },
setup: [{ click: [200, 32] }], act: [{ type: 'release' }] },
{ name: 'pipelines->pipeline detail', route: '/pipelines', act: [{ click: [280, 155] }] },
{ name: 'pipeline tab:->graph', route: '/pipelines/24', act: [{ click: TAB.third }] },
{ name: 'pipeline tab:->source', route: '/pipelines/24', act: [{ click: TAB.fourth }] },
{ name: 'chat list->open chat', route: '/chat', q: { chats: '30' }, act: [{ click: FIRST_ROW }] },
{ name: 'pipelines->settings', route: '/pipelines', act: [{ click: TOP_RIGHT }] },
];
const median = (xs) => { const s = [...xs].sort((a, b) => a - b); return s[Math.floor(s.length / 2)]; };
const r1 = (x) => Math.round(x * 10) / 10;
const lookup = opts.cpuprofile ? await loadSourceMap(opts.build) : null;
const server = await serveBuild(opts.build);
const browser = await openBrowser();
const { send, on } = browser;
const evaluate = async (expression) => (await send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true })).result?.value;
let frames = [];
let firstFrame = null;
const errors = [];
on('Runtime.consoleAPICalled', (p) => {
const text = p.args.map((a) => a.value ?? '').join(' ');
if (text.startsWith('PERF_FRAME ')) frames.push(text.split(' ').slice(1).map(Number));
else if (text.startsWith('PERF_FIRST_FRAME ')) firstFrame = Number(text.split(' ')[1]);
else if (p.type === 'error') errors.push(text.slice(0, 200));
});
on('Runtime.exceptionThrown', (p) => errors.push(String(p.exceptionDetails?.exception?.description ?? p.exceptionDetails?.text).slice(0, 200)));
async function tap([x, y]) {
for (const type of ['mouseMoved', 'mousePressed', 'mouseReleased']) {
await send('Input.dispatchMouseEvent', { type, x, y, button: 'left', clickCount: 1 });
}
}
async function run(steps) {
for (const step of steps ?? []) {
if (step.click) { await tap(step.click); await delay(1200); }
if (step.type) for (const ch of step.type) { await send('Input.insertText', { text: ch }); await delay(120); }
if (step.wait) await delay(step.wait);
}
}
// The engine hands timings over in batches; two late frames flush them.
async function settle() {
await delay(1500);
for (let i = 0; i < 2; i++) { await evaluate('window.__perfKick?.()'); await delay(250); }
}
await mkdir(opts.out, { recursive: true });
const results = {};
try {
await send('Page.enable');
await send('Runtime.enable');
await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: false });
await send('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: 'light' }] });
const only = opts.only?.split(',');
for (const s of SCENARIOS.filter((s) => !only || only.includes(s.name))) {
const reps = [];
for (let rep = 0; rep < Number(opts.repeat); rep++) {
const url = new URL('/', server.origin);
url.searchParams.set('route', s.route);
url.searchParams.set('theme', 'light');
url.searchParams.set('perf', '1');
for (const [k, v] of Object.entries(s.q ?? {})) url.searchParams.set(k, v);
frames = []; firstFrame = null;
await send('Page.navigate', { url: url.href });
for (let i = 0; i < 300 && firstFrame === null; i++) await delay(50);
await delay(2000);
const startup = s.name.startsWith('startup');
const loads = startup ? await evaluate(`Object.fromEntries(performance.getEntriesByType('resource')
.filter((e) => /main\\.dart\\.js|canvaskit\\.wasm/.test(e.name))
.map((e) => [e.name.split('/').pop(), Math.round(e.responseEnd)]))`) : null;
await run(s.setup);
await settle();
const startFrames = startup ? 0 : frames.length;
const profiling = opts.cpuprofile && rep === 0;
if (profiling) {
await send('Profiler.enable');
await send('Profiler.setSamplingInterval', { interval: 100 });
await send('Profiler.start');
}
await run(s.act);
await settle();
if (profiling) {
const { profile } = await send('Profiler.stop');
await writeFile(join(opts.out, `${s.name.replace(/[^\w-]+/g, '_')}.cpuprofile`), JSON.stringify(profile));
console.log(s.name, attribute(profile, lookup, { filter: (k) => k.startsWith('app:') || !k.includes(' ') }));
}
if (opts.shots && rep === 0) {
const png = (await send('Page.captureScreenshot', { format: 'png' })).data;
await writeFile(join(opts.out, `${s.name.replace(/[^\w-]+/g, '_')}.png`), Buffer.from(png, 'base64'));
}
const f = frames.slice(startFrames).filter((x) => x[0] + x[1] > 300);
if (opts.frames) console.log(` ${s.name} rep ${rep} build/raster ms:`, f.map((x) => `${r1(x[0] / 1000)}/${r1(x[1] / 1000)}`).join(' '));
const sum = (i) => f.reduce((a, x) => a + x[i], 0) / 1000;
const max = (i) => f.reduce((a, x) => Math.max(a, x[i]), 0) / 1000;
reps.push({ frames: f.length, buildSum: sum(0), buildMax: max(0), rasterSum: sum(1), rasterMax: max(1),
longest: max(2), over16: f.filter((x) => x[2] > 16667).length, firstFrame, ...loads });
}
const keys = Object.keys(reps[0]).filter((k) => typeof reps[0][k] === 'number');
results[s.name] = Object.fromEntries(keys.map((k) => [k, r1(median(reps.map((r) => r[k])))]));
results[s.name].reps = reps.length;
console.log(s.name.padEnd(36), JSON.stringify(results[s.name]));
}
if (errors.length) console.log('page errors (first 5):', [...new Set(errors)].slice(0, 5));
await writeFile(join(opts.out, 'interaction-bench.json'), JSON.stringify({ build: opts.build, size: opts.size, results }, null, 2));
} finally {
await browser.close().catch(() => {});
await server.close();
}
+68
View File
@@ -0,0 +1,68 @@
#!/usr/bin/env node
// Splits main.dart.js by Dart source through its source map, to find deferred
// loading candidates:
//
// flutter build web --release --source-maps --output build/size
// node tool/perf/js_size_by_source.mjs build/size [depth=3]
//
// Every generated character is charged to the source of the mapping segment
// it falls in (characters before a line's first segment, or in an unmapped
// segment, count as "(unmapped)"). App sources are grouped to [depth] path
// parts under lib/ (`features/chat/ui` at 3), other sources by package.
import { readFile } from 'node:fs/promises';
const [dir, depthArg = '3'] = process.argv.slice(2);
if (!dir) throw new Error('usage: js_size_by_source.mjs <build dir> [depth]');
const depth = Number(depthArg);
const js = (await readFile(`${dir}/main.dart.js`, 'utf8')).split('\n');
const map = JSON.parse(await readFile(`${dir}/main.dart.js.map`, 'utf8'));
const B64 = Object.fromEntries([...'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'].map((c, i) => [c, i]));
function decode(segment) {
const out = [];
let value = 0;
let shift = 0;
for (const c of segment) {
const digit = B64[c];
value += (digit & 31) << shift;
if (digit & 32) { shift += 5; continue; }
out.push(value & 1 ? -(value >>> 1) : value >>> 1);
value = 0;
shift = 0;
}
return out;
}
function group(source) {
const app = source.match(/(?:^|\/)lib\/((?:features|ui|shell|core|application)\/.*)$/);
if (app && !source.includes('/packages/')) return `app:${app[1].split('/').slice(0, depth).join('/').replace(/\.dart$/, '')}`;
const flutter = source.match(/packages\/flutter\/lib\/src\/(\w+)/);
if (flutter) return `flutter/${flutter[1]}`;
const pkg = source.match(/(?:pub\.dev|hosted\/[^/]+)\/([\w]+)-[\d.]+/) ?? source.match(/packages\/(\w+)\//);
if (pkg) return `pkg:${pkg[1]}`;
if (source.startsWith('org-dartlang-sdk')) return source.includes('_engine') ? 'sdk:web_engine' : 'sdk:dart';
return source.split('/').slice(-2).join('/');
}
const sizes = new Map();
const add = (key, n) => sizes.set(key, (sizes.get(key) ?? 0) + n);
let src = 0;
map.mappings.split(';').forEach((line, i) => {
const text = js[i] ?? '';
let col = 0;
let last = { col: 0, key: '(unmapped)' };
for (const seg of line.split(',')) {
if (!seg) continue;
const v = decode(seg);
col += v[0];
add(last.key, col - last.col);
if (v.length >= 4) src += v[1];
last = { col, key: v.length >= 4 ? group(map.sources[src]) : '(unmapped)' };
}
add(last.key, text.length + 1 - last.col);
});
const total = js.reduce((n, l) => n + l.length + 1, 0);
console.log(`main.dart.js ${(total / 1024).toFixed(0)} KiB`);
for (const [k, v] of [...sizes].sort((a, b) => b[1] - a[1]).slice(0, 45)) {
console.log(`${(v / 1024).toFixed(1).padStart(8)} KiB ${(100 * v / total).toFixed(1).padStart(5)}% ${k}`);
}
+112
View File
@@ -0,0 +1,112 @@
// Extra fixture data for the interaction bench (tool/perf/interaction_bench.mjs)
// over the full-app probe (tool/search_selection_probe_entry.dart). Every
// option defaults to off, so other captures see the fixture unchanged.
//
// `?hits=N` N more `release` run hits in Search;
// `?chats=N` N chat sessions in the chat list, each opening a short
// markdown transcript;
// `?graph=1` run 7 carries its pinned graph (plan -> build).
import 'package:pipelines_app/core/api_gateway.dart';
Map<String, String> _params = const {};
/// Reads the options from the page URL's query, before the app rewrites it.
void configurePerfFixture(Map<String, String> params) => _params = params;
int get _hits => int.tryParse(_params['hits'] ?? '') ?? 0;
int get _chats => int.tryParse(_params['chats'] ?? '') ?? 0;
bool get perfRunGraph => _params['graph'] == '1';
/// Run 7's pinned graph for `?graph=1`.
JsonMap perfRunGraphFixture() => {
'name': 'Release train',
'stages': [
{
'key': 'plan',
'type': 'model',
'edges': {'success': 'build', 'error': '#'},
},
{
'key': 'build',
'type': 'model',
'edges': {'success': 'plan', 'error': '#failure'},
},
],
};
/// Filler run hits for a query that `release` text matches.
List<JsonMap> perfSearchHits(String query, String kind) {
if (_hits == 0 || (kind.isNotEmpty && kind != 'run')) return const [];
if (query.isNotEmpty && !'release filler run'.contains(query)) {
return const [];
}
return [
for (var i = 0; i < _hits; i++)
{
'kind': 'run',
'target': 'run:${1000 + i}',
'score': 1,
'recency': '2026-10-18T08:42:00Z',
'run': {
'id': 1000 + i,
'pipeline_name': 'Release filler $i',
'status': i % 3 == 0 ? 'failed' : 'succeeded',
'workspace': '/srv/probe/release',
'created_at': '2026-10-18T08:12:00Z',
'finished_at': '2026-10-18T08:42:00Z',
},
},
];
}
String _answer(int i) =>
'## Answer $i\n\nSome **bold** text with `code`.\n\n'
'```dart\n${List.generate(8, (l) => 'final v$l = $i * $l;').join('\n')}'
'\n```\n\n- one\n- two\n';
/// Chat replies for `?chats=N`, or null to fall through to the fixture.
JsonMap? perfChatReply(JsonMap request) {
if (_chats == 0) return null;
final id = request['id'];
switch ('${request['type']}') {
case 'chat_sessions':
return {
'id': id,
'type': 'chat_sessions',
'chat_sessions': [
for (var i = 0; i < _chats; i++)
{
'id': 'perf-chat-$i',
'title': 'Fixture chat ${i + 1}',
'directory': '/workspace/demo',
},
],
};
case 'chat_subscribe':
return {'id': id, 'type': 'chat_subscribed'};
case 'chat_shells':
return {'id': id, 'type': 'chat_shells', 'chat_shells': const []};
case 'chat_messages':
return {
'id': id,
'type': 'chat_messages',
'chat_history': {
'messages': [
for (var i = 0; i < 20; i++)
{
'id': 'perf-m$i',
'role': i.isEven ? 'user' : 'assistant',
'parts': [
{
'type': 'text',
'text': i.isEven ? 'Question $i' : _answer(i),
},
],
},
],
'next_cursor': '',
'has_more': false,
},
};
}
return null;
}
@@ -28,6 +28,8 @@ import 'package:web/web.dart' as web;
import 'package:web_socket_channel/web_socket_channel.dart';
import 'perf/backdrop_census.dart';
import 'perf/frame_log.dart';
import 'perf/perf_fixture.dart';
import 'probe_folder_fixture.dart';
/// The run every run-scoped hit names. Its recorded sequences are deliberately
@@ -82,6 +84,7 @@ JsonMap probeRunDetail() => {
},
..._fillerSteps(),
],
if (perfRunGraph) 'graph': perfRunGraphFixture(),
};
/// Extra rows for scroll-performance captures (`?runs=N`, `?steps=N`). Both
@@ -366,6 +369,8 @@ JsonMap? probeReply(JsonMap request) {
'type': 'error',
'error': message,
};
final perf = perfChatReply(request);
if (perf != null) return perf;
switch (type) {
case 'subscribe':
case 'unsubscribe':
@@ -416,6 +421,7 @@ JsonMap? probeReply(JsonMap request) {
if ((kind.isEmpty || entry.hit['kind'] == kind) &&
(query.isEmpty || entry.haystack.toLowerCase().contains(query)))
entry.hit,
...perfSearchHits(query, kind),
];
return {
'id': id,
@@ -520,6 +526,9 @@ class _FixtureSink implements WebSocketSink {
/// `?q=release` types that query into Search once its field is shown;
/// `?platform=cupertino` renders the Cupertino host (`material`, the
/// default, renders Material as browsers do);
/// `?perf=1` prints frame timings (tool/perf/frame_log.dart);
/// `?hits=N`, `?chats=N` and `?graph=1` add bench data
/// (tool/perf/perf_fixture.dart);
/// `?theme=dark` / `?theme=light` pins the platform brightness the app's
/// `ThemeMode.system` follows (omitted: the browser's color scheme).
///
@@ -527,6 +536,8 @@ class _FixtureSink implements WebSocketSink {
void main() {
final params = Uri.base.queryParameters;
installBackdropCensus();
installFrameLog(params);
configurePerfFixture(params);
_fillerRunCount = int.tryParse(params['runs'] ?? '') ?? 0;
_fillerStepCount = int.tryParse(params['steps'] ?? '') ?? 0;
final cupertino = params['platform'] == 'cupertino';