From 87617e4cc32370ae69a805f66ab33be9933c5cff Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Fri, 11 Sep 2020 18:05:15 -0700 Subject: [PATCH] New benchmark: bench_mouse_region_mixed_grid_hover (#63808) * Adds a new web benchmark bench_mouse_region_mixed_grid_hover --- .../bench_mouse_region_mixed_grid_hover.dart | 207 ++++++++++++++++++ .../macrobenchmarks/lib/web_benchmarks.dart | 2 + 2 files changed, 209 insertions(+) create mode 100644 dev/benchmarks/macrobenchmarks/lib/src/web/bench_mouse_region_mixed_grid_hover.dart diff --git a/dev/benchmarks/macrobenchmarks/lib/src/web/bench_mouse_region_mixed_grid_hover.dart b/dev/benchmarks/macrobenchmarks/lib/src/web/bench_mouse_region_mixed_grid_hover.dart new file mode 100644 index 0000000000..433e614a8b --- /dev/null +++ b/dev/benchmarks/macrobenchmarks/lib/src/web/bench_mouse_region_mixed_grid_hover.dart @@ -0,0 +1,207 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; +import 'dart:ui'; + +import 'package:flutter/gestures.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/widgets.dart'; +import 'package:flutter/scheduler.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import 'recorder.dart'; + +class _NestedMouseRegion extends StatelessWidget { + const _NestedMouseRegion({this.nests, this.child}); + + final int nests; + final Widget child; + + @override + Widget build(BuildContext context) { + Widget current = child; + for (int i = 0; i < nests; i++) { + current = MouseRegion( + onEnter: (_) {}, + child: child, + ); + } + return current; + } +} + +class _NestedListener extends StatelessWidget { + const _NestedListener({this.nests, this.child}); + + final int nests; + final Widget child; + + @override + Widget build(BuildContext context) { + Widget current = child; + for (int i = 0; i < nests; i++) { + current = Listener( + onPointerDown: (_) {}, + child: child, + ); + } + return current; + } +} + +/// Creates a grid of mouse regions, then continuously hovers over them. +/// +/// Measures our ability to hit test mouse regions. +class BenchMouseRegionMixedGridHover extends WidgetRecorder { + BenchMouseRegionMixedGridHover() : super(name: benchmarkName) { + tester = _Tester(onDataPoint: handleDataPoint); + } + + static const String benchmarkName = 'bench_mouse_region_mixed_grid_hover'; + + _Tester tester; + + void handleDataPoint(Duration duration) { + profile.addDataPoint('hitTestDuration', duration, reported: true); + } + + // Use a non-trivial border to force Web to switch painter + Border _getBorder(int columnIndex, int rowIndex) { + const BorderSide defaultBorderSide = BorderSide(); + + return Border( + left: columnIndex == 0 ? defaultBorderSide : BorderSide.none, + top: rowIndex == 0 ? defaultBorderSide : BorderSide.none, + right: defaultBorderSide, + bottom: defaultBorderSide, + ); + } + + bool started = false; + + @override + void frameDidDraw() { + if (!started) { + started = true; + SchedulerBinding.instance.addPostFrameCallback((Duration timeStamp) async { + tester.start(); + registerDidStop(tester.stop); + }); + } + super.frameDidDraw(); + } + + @override + Widget createWidget() { + const int rowsCount = 60; + const int columnsCount = 20; + const double containerSize = 20; + return Directionality( + textDirection: TextDirection.ltr, + child: Align( + alignment: Alignment.topLeft, + child: SizedBox( + width: 400, + height: 400, + child: ListView.builder( + itemCount: rowsCount, + cacheExtent: rowsCount * containerSize, + physics: const ClampingScrollPhysics(), + itemBuilder: (BuildContext context, int rowIndex) => _NestedMouseRegion( + nests: 10, + child: Row( + children: List.generate( + columnsCount, + (int columnIndex) => _NestedListener( + nests: 40, + child: _NestedMouseRegion( + nests: 10, + child: Container( + decoration: BoxDecoration( + border: _getBorder(columnIndex, rowIndex), + color: Color.fromARGB(255, rowIndex * 20 % 256, 127, 127), + ), + width: containerSize, + height: containerSize, + ), + ), + ), + ), + ), + ), + ), + ), + ), + ); + } +} + +class _UntilNextFrame { + _UntilNextFrame._(); + + static Completer _completer; + + static Future wait() { + if (_UntilNextFrame._completer == null) { + _UntilNextFrame._completer = Completer(); + SchedulerBinding.instance.addPostFrameCallback((_) { + _UntilNextFrame._completer.complete(null); + _UntilNextFrame._completer = null; + }); + } + return _UntilNextFrame._completer.future; + } +} + +class _Tester { + _Tester({this.onDataPoint}); + + final ValueSetter onDataPoint; + + static const Duration hoverDuration = Duration(milliseconds: 20); + + bool _stopped = false; + + TestGesture get gesture { + return _gesture ??= TestGesture( + dispatcher: (PointerEvent event, HitTestResult result) async { + RendererBinding.instance.dispatchEvent(event, result); + }, + hitTester: (Offset location) { + final HitTestResult result = HitTestResult(); + RendererBinding.instance.hitTest(result, location); + return result; + }, + kind: PointerDeviceKind.mouse, + ); + } + TestGesture _gesture; + + Duration currentTime = Duration.zero; + + Future _hoverTo(Offset location, Duration duration) async { + currentTime += duration; + final Stopwatch stopwatch = Stopwatch()..start(); + await gesture.moveTo(location, timeStamp: currentTime); + stopwatch.stop(); + if (onDataPoint != null) + onDataPoint(stopwatch.elapsed); + await _UntilNextFrame.wait(); + } + + Future start() async { + await Future.delayed(Duration.zero); + while (!_stopped) { + await _hoverTo(const Offset(30, 10), hoverDuration); + await _hoverTo(const Offset(10, 370), hoverDuration); + await _hoverTo(const Offset(370, 390), hoverDuration); + await _hoverTo(const Offset(390, 30), hoverDuration); + } + } + + void stop() { + _stopped = true; + } +} diff --git a/dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart b/dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart index 082a7bc477..856d148e1b 100644 --- a/dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart +++ b/dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart @@ -20,6 +20,7 @@ import 'src/web/bench_draw_rect.dart'; import 'src/web/bench_dynamic_clip_on_static_picture.dart'; import 'src/web/bench_mouse_region_grid_hover.dart'; import 'src/web/bench_mouse_region_grid_scroll.dart'; +import 'src/web/bench_mouse_region_mixed_grid_hover.dart'; import 'src/web/bench_paths.dart'; import 'src/web/bench_picture_recording.dart'; import 'src/web/bench_simple_lazy_text_scroll.dart'; @@ -52,6 +53,7 @@ final Map benchmarks = { BenchUpdateManyChildLayers.benchmarkName: () => BenchUpdateManyChildLayers(), BenchMouseRegionGridScroll.benchmarkName: () => BenchMouseRegionGridScroll(), BenchMouseRegionGridHover.benchmarkName: () => BenchMouseRegionGridHover(), + BenchMouseRegionMixedGridHover.benchmarkName: () => BenchMouseRegionMixedGridHover(), if (isCanvasKit) BenchBuildColorsGrid.canvasKitBenchmarkName: () => BenchBuildColorsGrid.canvasKit(),