Unverified Commit 28d2f010 authored by Tong Mu's avatar Tong Mu Committed by GitHub

Web macrobenchmark: bench_mouse_region_grid_hover now tests hitTestDuration (#60554)

parent dcbc694b
...@@ -36,11 +36,17 @@ class _NestedMouseRegion extends StatelessWidget { ...@@ -36,11 +36,17 @@ class _NestedMouseRegion extends StatelessWidget {
/// ///
/// Measures our ability to hit test mouse regions. /// Measures our ability to hit test mouse regions.
class BenchMouseRegionGridHover extends WidgetRecorder { class BenchMouseRegionGridHover extends WidgetRecorder {
BenchMouseRegionGridHover() : super(name: benchmarkName); BenchMouseRegionGridHover() : super(name: benchmarkName) {
tester = _Tester(onDataPoint: handleDataPoint);
}
static const String benchmarkName = 'bench_mouse_region_grid_hover'; static const String benchmarkName = 'bench_mouse_region_grid_hover';
final _Tester tester = _Tester(); _Tester tester;
void handleDataPoint(Duration duration) {
profile.addDataPoint('hitTestDuration', duration, reported: true);
}
// Use a non-trivial border to force Web to switch painter // Use a non-trivial border to force Web to switch painter
Border _getBorder(int columnIndex, int rowIndex) { Border _getBorder(int columnIndex, int rowIndex) {
...@@ -62,6 +68,7 @@ class BenchMouseRegionGridHover extends WidgetRecorder { ...@@ -62,6 +68,7 @@ class BenchMouseRegionGridHover extends WidgetRecorder {
started = true; started = true;
SchedulerBinding.instance.addPostFrameCallback((Duration timeStamp) async { SchedulerBinding.instance.addPostFrameCallback((Duration timeStamp) async {
tester.start(); tester.start();
registerDidStop(tester.stop);
}); });
} }
super.frameDidDraw(); super.frameDidDraw();
...@@ -127,6 +134,10 @@ class _UntilNextFrame { ...@@ -127,6 +134,10 @@ class _UntilNextFrame {
} }
class _Tester { class _Tester {
_Tester({this.onDataPoint});
final ValueSetter<Duration> onDataPoint;
static const Duration hoverDuration = Duration(milliseconds: 20); static const Duration hoverDuration = Duration(milliseconds: 20);
bool _stopped = false; bool _stopped = false;
...@@ -150,7 +161,11 @@ class _Tester { ...@@ -150,7 +161,11 @@ class _Tester {
Future<void> _hoverTo(Offset location, Duration duration) async { Future<void> _hoverTo(Offset location, Duration duration) async {
currentTime += duration; currentTime += duration;
final Stopwatch stopwatch = Stopwatch()..start();
await gesture.moveTo(location, timeStamp: currentTime); await gesture.moveTo(location, timeStamp: currentTime);
stopwatch.stop();
if (onDataPoint != null)
onDataPoint(stopwatch.elapsed);
await _UntilNextFrame.wait(); await _UntilNextFrame.wait();
} }
......
...@@ -43,12 +43,7 @@ class BenchMouseRegionGridScroll extends WidgetRecorder { ...@@ -43,12 +43,7 @@ class BenchMouseRegionGridScroll extends WidgetRecorder {
started = true; started = true;
SchedulerBinding.instance.addPostFrameCallback((Duration timeStamp) async { SchedulerBinding.instance.addPostFrameCallback((Duration timeStamp) async {
tester.start(); tester.start();
final VoidCallback localDidStop = didStop; registerDidStop(tester.stop);
didStop = () {
if (localDidStop != null)
localDidStop();
tester.stop();
};
}); });
} }
super.frameDidDraw(); super.frameDidDraw();
......
...@@ -345,8 +345,11 @@ abstract class WidgetRecorder extends Recorder implements FrameRecorder { ...@@ -345,8 +345,11 @@ abstract class WidgetRecorder extends Recorder implements FrameRecorder {
/// pumping frames automatically. /// pumping frames automatically.
Widget createWidget(); Widget createWidget();
final List<VoidCallback> _didStopCallbacks = <VoidCallback>[];
@override @override
VoidCallback didStop; void registerDidStop(VoidCallback fn) {
_didStopCallbacks.add(fn);
}
@override @override
Profile profile; Profile profile;
...@@ -373,7 +376,8 @@ abstract class WidgetRecorder extends Recorder implements FrameRecorder { ...@@ -373,7 +376,8 @@ abstract class WidgetRecorder extends Recorder implements FrameRecorder {
if (shouldContinue()) { if (shouldContinue()) {
window.scheduleFrame(); window.scheduleFrame();
} else { } else {
didStop(); for (final VoidCallback fn in _didStopCallbacks)
fn();
_runCompleter.complete(); _runCompleter.complete();
} }
} }
...@@ -437,8 +441,11 @@ abstract class WidgetBuildRecorder extends Recorder implements FrameRecorder { ...@@ -437,8 +441,11 @@ abstract class WidgetBuildRecorder extends Recorder implements FrameRecorder {
/// consider using [WidgetRecorder]. /// consider using [WidgetRecorder].
Widget createWidget(); Widget createWidget();
final List<VoidCallback> _didStopCallbacks = <VoidCallback>[];
@override @override
VoidCallback didStop; void registerDidStop(VoidCallback fn) {
_didStopCallbacks.add(fn);
}
@override @override
Profile profile; Profile profile;
...@@ -484,7 +491,8 @@ abstract class WidgetBuildRecorder extends Recorder implements FrameRecorder { ...@@ -484,7 +491,8 @@ abstract class WidgetBuildRecorder extends Recorder implements FrameRecorder {
showWidget = !showWidget; showWidget = !showWidget;
_hostState._setStateTrampoline(); _hostState._setStateTrampoline();
} else { } else {
didStop(); for (final VoidCallback fn in _didStopCallbacks)
fn();
_runCompleter.complete(); _runCompleter.complete();
} }
} }
...@@ -942,9 +950,8 @@ String _ratioToPercent(double value) { ...@@ -942,9 +950,8 @@ String _ratioToPercent(double value) {
/// Implemented by recorders that use [_RecordingWidgetsBinding] to receive /// Implemented by recorders that use [_RecordingWidgetsBinding] to receive
/// frame life-cycle calls. /// frame life-cycle calls.
abstract class FrameRecorder { abstract class FrameRecorder {
/// Called by the recorder when it stops recording and doesn't need to collect /// Add a callback that will be called by the recorder when it stops recording.
/// any more data. void registerDidStop(VoidCallback cb);
set didStop(VoidCallback cb);
/// Called just before calling [SchedulerBinding.handleDrawFrame]. /// Called just before calling [SchedulerBinding.handleDrawFrame].
void frameWillDraw(); void frameWillDraw();
...@@ -998,9 +1005,9 @@ class _RecordingWidgetsBinding extends BindingBase ...@@ -998,9 +1005,9 @@ class _RecordingWidgetsBinding extends BindingBase
} }
final FlutterExceptionHandler originalOnError = FlutterError.onError; final FlutterExceptionHandler originalOnError = FlutterError.onError;
recorder.didStop = () { recorder.registerDidStop(() {
_benchmarkStopped = true; _benchmarkStopped = true;
}; });
// Fail hard and fast on errors. Benchmarks should not have any errors. // Fail hard and fast on errors. Benchmarks should not have any errors.
FlutterError.onError = (FlutterErrorDetails details) { FlutterError.onError = (FlutterErrorDetails details) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment