todo-app/lib/frame_stats_web.dart
Krzysztof kuhy Rudnicki 3704d34f74 Fix the web instrument's arming flag, and let the wrapper redirect its paths
Two things found while verifying the >=100fps done-condition at 4K.

`bool.fromEnvironment` only accepts the literal `true`, so
`--dart-define=TODO_FRAME_STATS=1` silently evaluated to false and the web
instrument emitted nothing at all -- a whole measurement run produced no data
and looked like the callback was broken. It now reads the value as a string and
accepts either form.

The wrapper gains --backlog-path/--log-path. Verification needs to point the
server somewhere harmless: it starts an app whose store may be empty, and an
empty store exports over the real ~/todo/BACKLOG.md through the very endpoint
being tested.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-20 13:47:38 +02:00

90 lines
2.9 KiB
Dart

// coverage:ignore-file
// TEMPORARY diagnostic instrument, web variant. See frame_stats_io.dart.
//
// Armed at build time (`--dart-define=TODO_FRAME_STATS=1`) rather than by an
// environment variable, which a browser does not have. Reports to the devtools
// console.
import 'dart:async';
import 'dart:ui' show FramePhase;
import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
/// Whether the frame-stats instrument is armed for this build.
///
/// Read as a string rather than `bool.fromEnvironment`, which only accepts the
/// exact literal `true` — `--dart-define=TODO_FRAME_STATS=1` silently yields
/// false and the instrument produces nothing at all.
const String _frameStatsFlag = String.fromEnvironment('TODO_FRAME_STATS');
/// Whether the instrument is armed (`--dart-define=TODO_FRAME_STATS=1`).
const bool frameStatsEnabled =
_frameStatsFlag == '1' || _frameStatsFlag == 'true';
const _slowFrameMs = 8;
const _probePeriod = Duration(milliseconds: 16);
const _blockedMs = 24;
final Stopwatch _clock = Stopwatch();
Timer? _probe;
final List<int> _cadenceUs = <int>[];
String get _at => (_clock.elapsedMilliseconds / 1000).toStringAsFixed(2);
/// Installs the frame-timing and event-loop instruments.
void installFrameStats() {
if (!frameStatsEnabled) return;
_clock.start();
debugPrint('[frame-stats web] armed');
SchedulerBinding.instance.addTimingsCallback((timings) {
for (final t in timings) {
final total = t.buildDuration.inMilliseconds +
t.rasterDuration.inMilliseconds;
if (total > _slowFrameMs) {
debugPrint(
'[SLOW-FRAME $_at s] total=${total}ms '
'(build=${t.buildDuration.inMilliseconds} '
'raster=${t.rasterDuration.inMilliseconds})',
);
}
// Cadence is the headline number for the >=100fps done-condition.
final vsync = t.timestampInMicroseconds(FramePhase.vsyncStart);
_cadenceUs.add(vsync);
}
_reportCadence();
});
var expected = _clock.elapsedMicroseconds + _probePeriod.inMicroseconds;
_probe = Timer.periodic(_probePeriod, (_) {
final now = _clock.elapsedMicroseconds;
final lateMs = (now - expected) / 1000;
if (lateMs > _blockedMs) {
debugPrint('[UI-BLOCKED $_at s] ${lateMs.toStringAsFixed(1)}ms');
}
expected = now + _probePeriod.inMicroseconds;
});
}
void _reportCadence() {
if (_cadenceUs.length < 120) return;
final deltas = <int>[];
for (var i = 1; i < _cadenceUs.length; i++) {
deltas.add(_cadenceUs[i] - _cadenceUs[i - 1]);
}
deltas.sort();
final p50 = deltas[deltas.length ~/ 2] / 1000;
final p95 = deltas[(deltas.length * 95) ~/ 100] / 1000;
debugPrint(
'[frame-stats web] cadence p50=${p50.toStringAsFixed(2)}ms '
'p95=${p95.toStringAsFixed(2)}ms (~${(1000 / p50).toStringAsFixed(1)} fps)',
);
_cadenceUs.clear();
}
/// Stops the probe.
void disposeFrameStats() {
_probe?.cancel();
}