|
| 1 | +import 'dart:collection'; |
| 2 | +import 'dart:math' as math; |
| 3 | +import 'dart:ui' show FramePhase, FrameTiming; |
| 4 | + |
| 5 | +import 'package:flutter/scheduler.dart'; |
| 6 | + |
| 7 | +/// How smooth the app has been: frame timings since start (the last 600 |
| 8 | +/// frames), for Settings → Developer → Rendering and diagnostics. Flutter |
| 9 | +/// reports them in batches (about once a second in release builds), so |
| 10 | +/// watching costs next to nothing. |
| 11 | +class FrameStats { |
| 12 | + FrameStats._(); |
| 13 | + |
| 14 | + static final instance = FrameStats._(); |
| 15 | + |
| 16 | + static const _keep = 600; |
| 17 | + final _frames = ListQueue<FrameTiming>(); |
| 18 | + bool _started = false; |
| 19 | + |
| 20 | + void start() { |
| 21 | + if (_started) return; |
| 22 | + _started = true; |
| 23 | + SchedulerBinding.instance.addTimingsCallback(add); |
| 24 | + } |
| 25 | + |
| 26 | + /// Records frames (the timings callback; also for tests). |
| 27 | + void add(List<FrameTiming> timings) { |
| 28 | + _frames.addAll(timings); |
| 29 | + while (_frames.length > _keep) { |
| 30 | + _frames.removeFirst(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + void reset() => _frames.clear(); |
| 35 | + |
| 36 | + /// Null until there are enough frames to say anything. |
| 37 | + FrameSummary? summary({required double refreshRate}) { |
| 38 | + if (_frames.length < 10) return null; |
| 39 | + return FrameSummary.of(_frames.toList(), refreshRate: refreshRate); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/// Frame timings in numbers. |
| 44 | +class FrameSummary { |
| 45 | + const FrameSummary({ |
| 46 | + required this.frames, |
| 47 | + required this.refreshRate, |
| 48 | + required this.interval, |
| 49 | + required this.build, |
| 50 | + required this.raster, |
| 51 | + required this.overBudget, |
| 52 | + }); |
| 53 | + |
| 54 | + factory FrameSummary.of( |
| 55 | + List<FrameTiming> frames, { |
| 56 | + required double refreshRate, |
| 57 | + }) { |
| 58 | + // Frames come only while something moves: the time between frames of |
| 59 | + // one animation shows the real frame rate (idle gaps don't count). |
| 60 | + final starts = [ |
| 61 | + for (final f in frames) f.timestampInMicroseconds(FramePhase.vsyncStart), |
| 62 | + ]; |
| 63 | + final gaps = <int>[ |
| 64 | + for (var i = 1; i < starts.length; i++) |
| 65 | + if (starts[i] - starts[i - 1] case final gap |
| 66 | + when gap > 0 && gap < 100000) |
| 67 | + gap, |
| 68 | + ]; |
| 69 | + final budget = refreshRate > 0 ? 1e6 / refreshRate : 1e6 / 60; |
| 70 | + final over = frames |
| 71 | + .where( |
| 72 | + (f) => |
| 73 | + f.buildDuration.inMicroseconds > budget || |
| 74 | + f.rasterDuration.inMicroseconds > budget, |
| 75 | + ) |
| 76 | + .length; |
| 77 | + return FrameSummary( |
| 78 | + frames: frames.length, |
| 79 | + refreshRate: refreshRate, |
| 80 | + interval: gaps.isEmpty |
| 81 | + ? null |
| 82 | + : Duration(microseconds: _percentile(gaps, 0.5)), |
| 83 | + build: TimingSpread.of([ |
| 84 | + for (final f in frames) f.buildDuration.inMicroseconds, |
| 85 | + ]), |
| 86 | + raster: TimingSpread.of([ |
| 87 | + for (final f in frames) f.rasterDuration.inMicroseconds, |
| 88 | + ]), |
| 89 | + overBudget: over / frames.length, |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + final int frames; |
| 94 | + |
| 95 | + /// What the display reports (Hz). |
| 96 | + final double refreshRate; |
| 97 | + |
| 98 | + /// The usual time between two frames of an animation; null without one. |
| 99 | + final Duration? interval; |
| 100 | + final TimingSpread build; |
| 101 | + final TimingSpread raster; |
| 102 | + |
| 103 | + /// Share of frames whose build or raster took longer than one refresh. |
| 104 | + final double overBudget; |
| 105 | + |
| 106 | + /// Frames per second while animating. |
| 107 | + double? get fps => interval == null ? null : 1e6 / interval!.inMicroseconds; |
| 108 | + |
| 109 | + @override |
| 110 | + String toString() { |
| 111 | + final rate = fps; |
| 112 | + return [ |
| 113 | + '${refreshRate.round()} Hz display', |
| 114 | + if (rate != null) '${rate.round()} fps while animating', |
| 115 | + 'build $build', |
| 116 | + 'raster $raster', |
| 117 | + '${(overBudget * 100).toStringAsFixed(1)}% over budget', |
| 118 | + '$frames frames', |
| 119 | + ].join(', '); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/// p50 / p90 / max of a timing, in ms. |
| 124 | +class TimingSpread { |
| 125 | + const TimingSpread(this.p50, this.p90, this.max); |
| 126 | + |
| 127 | + factory TimingSpread.of(List<int> micros) => TimingSpread( |
| 128 | + _percentile(micros, 0.5), |
| 129 | + _percentile(micros, 0.9), |
| 130 | + micros.reduce(math.max), |
| 131 | + ); |
| 132 | + |
| 133 | + final int p50; |
| 134 | + final int p90; |
| 135 | + final int max; |
| 136 | + |
| 137 | + static String _ms(int micros) => (micros / 1000).toStringAsFixed(1); |
| 138 | + |
| 139 | + @override |
| 140 | + String toString() => 'p50 ${_ms(p50)} / p90 ${_ms(p90)} / max ${_ms(max)} ms'; |
| 141 | +} |
| 142 | + |
| 143 | +int _percentile(List<int> values, double p) { |
| 144 | + final sorted = [...values]..sort(); |
| 145 | + return sorted[((sorted.length - 1) * p).round()]; |
| 146 | +} |
0 commit comments