Share one minute clock across time labels and narrow shell and runs rebuilds

Relative-time labels follow one wall-minute RelativeTimeClock instead of a
timer per row, skip ticks while their tab is offstage and rebuild only when
their text changes. The shell rebuilds its top bar only for the focused
destination's published bar and its tab bar only for badge changes. Run
events that repeat a row's state no longer notify, and the runs list filters
and sorts once per build. Adds a rebuild/timer benchmark.
This commit is contained in:
2026-11-01 23:29:20 +00:00
parent bdf70af7e8
commit 3b996bdbc7
9 changed files with 621 additions and 164 deletions
@@ -0,0 +1,239 @@
// Measures wasted background work outside the chat transcript: timers kept
// alive by list rows, rebuilds per minute tick, offstage tabs' work and the
// rebuilds a burst of run events causes. Each scenario prints its numbers
// (`BENCH ...`) and asserts the budgets the shared clock and the narrowed
// notifications keep.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/runs/data/runs_repository.dart';
import 'package:pipelines_app/features/runs/ui/runs_controller.dart';
import 'package:pipelines_app/features/runs/ui/runs_list.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart';
import 'package:pipelines_app/ui/text/relative_time_label.dart';
class _Gateway implements ApiGateway {
final controller = StreamController<JsonMap>.broadcast(sync: true);
List<JsonMap> runs = [];
@override
Stream<JsonMap> get frames => controller.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async => {
'runs': [for (final run in runs) Map<String, dynamic>.of(run)],
};
}
/// Counts element rebuilds by widget type while installed.
class _Rebuilds {
final Map<Type, int> counts = {};
void install() => debugOnRebuildDirtyWidget = (element, _) => counts.update(
element.widget.runtimeType,
(n) => n + 1,
ifAbsent: () => 1,
);
void uninstall() => debugOnRebuildDirtyWidget = null;
int of(Type type) => counts[type] ?? 0;
int get total => counts.values.fold(0, (a, b) => a + b);
void reset() => counts.clear();
}
/// Tracks every Timer created in its zone (one-shot and periodic).
class _Timers {
final List<Timer> created = [];
int get active => created.where((t) => t.isActive).length;
late final ZoneSpecification spec = ZoneSpecification(
createTimer: (self, parent, zone, duration, f) {
final timer = parent.createTimer(zone, duration, f);
created.add(timer);
return timer;
},
createPeriodicTimer: (self, parent, zone, period, f) {
final timer = parent.createPeriodicTimer(zone, period, f);
created.add(timer);
return timer;
},
);
}
List<JsonMap> _runs(int count, DateTime now) => [
for (var i = 0; i < count; i++)
{
'id': i + 1,
'name': 'Pipeline ${i % 7}',
'title': 'Run ${i + 1}',
'status': i < 20 ? 'running' : 'succeeded',
'executing': i < 20,
'completed_steps': 1,
// The lifecycle fields a server list row carries.
'notify_mode': 'auto',
'resource_wait': null,
'cancel_requested': false,
'cleanup_unconfirmed': false,
// Every 20 seconds back: the newest rows change label each minute.
'created_at': now.subtract(Duration(seconds: 20 * i)).toIso8601String(),
},
];
void main() {
late _Gateway gateway;
late RunsController controller;
final rebuilds = _Rebuilds();
setUp(() async {
gateway = _Gateway()..runs = _runs(300, DateTime.now());
controller = RunsController(RunsRepository(gateway));
controller.listen(gateway.frames);
await controller.load();
});
tearDown(() async {
rebuilds.uninstall();
controller.dispose();
await gateway.controller.close();
});
Widget shell(AppDestination destination) => MaterialApp(
home: AdaptiveShell(
destination: destination,
destinationBodies: {
AppDestination.runs: (_) => AnimatedBuilder(
animation: controller,
builder: (_, _) =>
RunsList(controller: controller, onSelected: (_) {}),
),
AppDestination.pipelines: (_) => const Center(child: Text('pipelines')),
},
),
);
/// Sends frame [i] of a busy stream: most frames repeat a running row's
/// current state; every fifth advances its step count.
void frame(int i) {
final id = 1 + i % 20;
final row = gateway.runs[id - 1];
if (i % 5 == 0) row['completed_steps'] = 2 + i;
gateway.controller.add({
'type': 'run_event',
'run_id': id,
'status': 'running',
'completed_steps': row['completed_steps'],
});
}
Future<void> sizeView(WidgetTester tester) async {
tester.view.physicalSize = const Size(400, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
}
testWidgets('300-run list: timers and rebuilds per minute tick', (
tester,
) async {
await sizeView(tester);
final timers = _Timers();
await runZoned(zoneSpecification: timers.spec, () async {
await tester.pumpWidget(shell(AppDestination.runs));
await tester.pump();
final labels = find.byType(RelativeTimeLabel).evaluate().length;
final active = timers.active;
// Measure one whole minute after the first tick, wherever it falls.
await tester.pump(const Duration(minutes: 1));
await tester.pump();
final created = timers.created.length;
rebuilds
..reset()
..install();
await tester.pump(const Duration(minutes: 1));
await tester.pump();
final perMinute = timers.created.length - created;
rebuilds.uninstall();
// ignore: avoid_print
print(
'BENCH list300 mountedLabels=$labels activeTimers=$active '
'labelRebuildsPerMinute=${rebuilds.of(RelativeTimeLabel)} '
'totalRebuildsPerMinute=${rebuilds.total} '
'timersScheduledPerMinute=$perMinute',
);
expect(active, lessThanOrEqualTo(1));
await tester.pumpWidget(const SizedBox());
expect(timers.active, 0);
});
});
testWidgets('offstage Runs tab: minute ticks and run events', (tester) async {
await sizeView(tester);
final timers = _Timers();
await runZoned(zoneSpecification: timers.spec, () async {
await tester.pumpWidget(shell(AppDestination.runs));
await tester.pump();
await tester.pumpWidget(shell(AppDestination.pipelines));
await tester.pump(const Duration(seconds: 1)); // fade-through done
final active = timers.active;
rebuilds
..reset()
..install();
await tester.pump(const Duration(minutes: 2));
await tester.pump();
final tick = rebuilds.of(RelativeTimeLabel);
rebuilds.reset();
for (var i = 0; i < 50; i++) {
frame(i);
await tester.pump();
}
await tester.pump();
rebuilds.uninstall();
// ignore: avoid_print
print(
'BENCH offstage activeTimers=$active labelRebuildsIn2Minutes=$tick '
'burst50: runsListRebuilds=${rebuilds.of(RunsList)} '
'shellBarRebuilds=${rebuilds.of(FloatingDestinationBar)} '
'totalRebuilds=${rebuilds.total}',
);
expect(tick, 0);
expect(rebuilds.of(FloatingDestinationBar), 0);
await tester.pumpWidget(const SizedBox());
expect(timers.active, 0);
});
});
testWidgets('visible Runs tab: a 50-frame run_event burst', (tester) async {
await sizeView(tester);
var notifications = 0;
controller.addListener(() => notifications++);
await tester.pumpWidget(shell(AppDestination.runs));
await tester.pump();
rebuilds
..reset()
..install();
// Frames arrive between frames on a real socket: pump after each.
for (var i = 0; i < 50; i++) {
frame(i);
await tester.pump();
}
await tester.pump();
rebuilds.uninstall();
// ignore: avoid_print
print(
'BENCH visibleBurst50 controllerNotifications=$notifications '
'runsListRebuilds=${rebuilds.of(RunsList)} '
'runRowRebuilds=${rebuilds.of(RunListRow)} '
'shellBarRebuilds=${rebuilds.of(FloatingDestinationBar)} '
'totalRebuilds=${rebuilds.total}',
);
final top = rebuilds.counts.entries.toList()
..sort((a, b) => b.value - a.value);
// ignore: avoid_print
print(
'BENCH top ${top.take(15).map((e) => '${e.key}=${e.value}').join(' ')}',
);
// Only the ten frames that advance a step change anything visible.
expect(notifications, lessThanOrEqualTo(10));
await tester.pumpWidget(const SizedBox());
});
}
+65 -13
View File
@@ -1,45 +1,96 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/ui/text/relative_time.dart';
import 'package:pipelines_app/ui/text/relative_time_clock.dart';
import 'package:pipelines_app/ui/text/relative_time_label.dart';
/// The start of the current wall-clock minute, where the shared clock ticks.
DateTime _minute() {
final now = DateTime.now();
return DateTime(now.year, now.month, now.day, now.hour, now.minute);
}
void main() {
testWidgets('updates at the next relative-time boundary', (tester) async {
final instant = DateTime.now().subtract(const Duration(minutes: 4));
final clock = RelativeTimeClock.shared;
testWidgets('updates on the next minute tick', (tester) async {
final instant = _minute().subtract(const Duration(minutes: 4));
await tester.pumpWidget(
MaterialApp(home: RelativeTimeLabel(instant: instant)),
);
expect(find.text('4m'), findsOneWidget);
await tester.pump(const Duration(minutes: 1, seconds: 1));
await tester.pump(const Duration(minutes: 1));
expect(find.text('5m'), findsOneWidget);
});
testWidgets('future labels schedule their next boundary', (tester) async {
final instant = DateTime.now().add(const Duration(minutes: 4));
testWidgets('future labels count down on the ticks', (tester) async {
final instant = _minute().add(
const Duration(minutes: 4, milliseconds: 500),
);
await tester.pumpWidget(
MaterialApp(home: RelativeTimeLabel(instant: instant)),
);
expect(find.text('in 3m'), findsOneWidget);
expect(find.text(formatRelativeTime(instant)!), findsOneWidget);
await tester.pump(const Duration(minutes: 1, seconds: 1));
await tester.pump(const Duration(minutes: 2));
expect(find.text('in 2m'), findsOneWidget);
});
testWidgets('cancels its timer when disposed', (tester) async {
testWidgets('many labels share one timer, released when disposed', (
tester,
) async {
final instant = DateTime.now().subtract(const Duration(minutes: 4));
await tester.pumpWidget(
MaterialApp(
home: RelativeTimeLabel(
instant: DateTime.now().subtract(const Duration(minutes: 4)),
home: Wrap(
children: [
for (var i = 0; i < 50; i++) RelativeTimeLabel(instant: instant),
],
),
),
);
expect(clock.ticking, isTrue);
await tester.pumpWidget(const MaterialApp(home: SizedBox()));
expect(clock.ticking, isFalse);
await tester.pump(const Duration(hours: 1));
});
testWidgets('a muted label skips ticks and catches up when shown', (
tester,
) async {
final instant = _minute().subtract(const Duration(minutes: 4));
var builds = 0;
Widget app(bool enabled) => MaterialApp(
home: TickerMode(
enabled: enabled,
child: Builder(
builder: (_) {
builds++;
return RelativeTimeLabel(instant: instant);
},
),
),
);
await tester.pumpWidget(app(false));
final label = find.byType(RelativeTimeLabel);
var labelBuilds = 0;
debugOnRebuildDirtyWidget = (element, _) {
if (element.widget is RelativeTimeLabel) labelBuilds++;
};
addTearDown(() => debugOnRebuildDirtyWidget = null);
await tester.pump(const Duration(minutes: 1));
expect(labelBuilds, 0);
expect(find.descendant(of: label, matching: find.text('4m')), findsOneWidget);
await tester.pumpWidget(app(true));
expect(find.text('5m'), findsOneWidget);
expect(builds, 2);
});
testWidgets('keeps ticking while inactive, pauses while hidden and '
'refreshes on resume', (tester) async {
final instant = DateTime.now().subtract(const Duration(minutes: 4));
final instant = _minute().subtract(const Duration(minutes: 4));
await tester.pumpWidget(
MaterialApp(home: RelativeTimeLabel(instant: instant)),
);
@@ -49,12 +100,13 @@ void main() {
WidgetsBinding.instance.handleAppLifecycleStateChanged(
AppLifecycleState.inactive,
);
await tester.pump(const Duration(minutes: 1, seconds: 1));
await tester.pump(const Duration(minutes: 1));
expect(find.text('5m'), findsOneWidget);
WidgetsBinding.instance.handleAppLifecycleStateChanged(
AppLifecycleState.hidden,
);
expect(clock.ticking, isFalse);
await tester.pump(const Duration(minutes: 1, seconds: 1));
expect(find.text('5m'), findsOneWidget);
@@ -64,8 +116,8 @@ void main() {
WidgetsBinding.instance.handleAppLifecycleStateChanged(
AppLifecycleState.resumed,
);
expect(clock.ticking, isTrue);
await tester.pump();
await tester.pump(const Duration(minutes: 1, seconds: 1));
expect(find.text('5m'), findsOneWidget);
});
}