Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 03ed06f

Browse files
authoredJun 28, 2019
New benchmark: Gesture semantics (flutter#35232)
* Add semanticsEnabled to widgetBenchmark * Add button_matrix_app and gesture benchmark
1 parent 95ca768 commit 03ed06f

File tree

5 files changed

+125
-3
lines changed

5 files changed

+125
-3
lines changed
 

‎dev/benchmarks/microbenchmarks/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ these:
66

77
```
88
flutter run --release lib/gestures/velocity_tracker_data.dart
9+
flutter run --release lib/gestures/gesture_detector_bench.dart
910
flutter run --release lib/stocks/animation_bench.dart
1011
flutter run --release lib/stocks/build_bench.dart
1112
flutter run --release lib/stocks/layout_bench.dart
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2019 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
7+
class ButtonMatrixApp extends StatefulWidget {
8+
@override
9+
ButtonMatrixAppState createState() => ButtonMatrixAppState();
10+
}
11+
12+
class ButtonMatrixAppState extends State<ButtonMatrixApp> {
13+
14+
int count = 1;
15+
int increment = 1;
16+
17+
@override
18+
Widget build(BuildContext context) {
19+
return MaterialApp(
20+
home: Scaffold(
21+
appBar: AppBar(
22+
title: Text('Count: $count'),
23+
actions: <Widget>[
24+
FlatButton(
25+
onPressed: () => setState(() { count += increment; }),
26+
child: Text('Add $increment'),
27+
),
28+
],
29+
),
30+
body: Row(
31+
mainAxisAlignment: MainAxisAlignment.center,
32+
children: List<Widget>.filled(
33+
3,
34+
Column(
35+
children: List<Widget>.filled(
36+
10,
37+
FlatButton(
38+
child: const Text('Faster'),
39+
onPressed: () => setState(() { increment += 1; }),
40+
),
41+
),
42+
),
43+
),
44+
),
45+
),
46+
);
47+
}
48+
}
49+
50+
void main() {
51+
runApp(ButtonMatrixApp());
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2019 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/gestures.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
8+
import '../common.dart';
9+
import './apps/button_matrix_app.dart' as button_matrix;
10+
11+
const int _kNumWarmUpIters = 20;
12+
const int _kNumIters = 300;
13+
14+
Future<void> main() async {
15+
assert(false, "Don't run benchmarks in checked mode! Use 'flutter run --release'.");
16+
final Stopwatch watch = Stopwatch();
17+
print('GestureDetector semantics benchmark...');
18+
19+
await benchmarkWidgets((WidgetTester tester) async {
20+
button_matrix.main();
21+
await tester.pump();
22+
await tester.pump(const Duration(seconds: 1));
23+
24+
Future<void> iter() async {
25+
// Press a button to update the screen
26+
await tester.tapAt(const Offset(760.0, 30.0));
27+
await tester.pump();
28+
}
29+
30+
// Warm up runs get the app into steady state, making benchmark
31+
// results more credible
32+
for (int i = 0; i < _kNumWarmUpIters; i += 1) {
33+
await iter();
34+
}
35+
await tester.pumpAndSettle();
36+
37+
watch.start();
38+
for (int i = 0; i < _kNumIters; i += 1) {
39+
await iter();
40+
}
41+
watch.stop();
42+
}, semanticsEnabled: true);
43+
44+
final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
45+
printer.addResult(
46+
description: 'GestureDetector',
47+
value: watch.elapsedMicroseconds / _kNumIters,
48+
unit: 'µs per iteration',
49+
name: 'gesture_detector_bench',
50+
);
51+
printer.printToStdout();
52+
}

‎dev/devicelab/lib/tasks/microbenchmarks.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ TaskFunction createMicrobenchmarkTask() {
5353
allResults.addAll(await _runMicrobench('lib/stocks/build_bench.dart'));
5454
allResults.addAll(await _runMicrobench('lib/geometry/rrect_contains_bench.dart'));
5555
allResults.addAll(await _runMicrobench('lib/gestures/velocity_tracker_bench.dart'));
56+
allResults.addAll(await _runMicrobench('lib/gestures/gesture_detector_bench.dart'));
5657
allResults.addAll(await _runMicrobench('lib/stocks/animation_bench.dart'));
5758

5859
return TaskResult.success(allResults, benchmarkScoreKeys: allResults.keys.toList());

‎packages/flutter_test/lib/src/widget_tester.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ typedef WidgetTesterCallback = Future<void> Function(WidgetTester widgetTester);
6868
/// In general, timeouts are race conditions and cause flakes, so best practice
6969
/// is to avoid the use of timeouts in tests.
7070
///
71-
/// If the `enableSemantics` parameter is set to `true`,
71+
/// If the `semanticsEnabled` parameter is set to `true`,
7272
/// [WidgetTester.ensureSemantics] will have been called before the tester is
7373
/// passed to the `callback`, and that handle will automatically be disposed
7474
/// after the callback is finished.
@@ -144,6 +144,11 @@ void testWidgets(
144144
/// If the callback is asynchronous, make sure you `await` the call
145145
/// to [benchmarkWidgets], otherwise it won't run!
146146
///
147+
/// If the `semanticsEnabled` parameter is set to `true`,
148+
/// [WidgetTester.ensureSemantics] will have been called before the tester is
149+
/// passed to the `callback`, and that handle will automatically be disposed
150+
/// after the callback is finished.
151+
///
147152
/// Benchmarks must not be run in checked mode, because the performance is not
148153
/// representative. To avoid this, this function will print a big message if it
149154
/// is run in checked mode. Unit tests of this method pass `mayRunWithAsserts`,
@@ -165,7 +170,11 @@ void testWidgets(
165170
/// });
166171
/// exit(0);
167172
/// }
168-
Future<void> benchmarkWidgets(WidgetTesterCallback callback, {bool mayRunWithAsserts = false}) {
173+
Future<void> benchmarkWidgets(
174+
WidgetTesterCallback callback, {
175+
bool mayRunWithAsserts = false,
176+
bool semanticsEnabled = true,
177+
}) {
169178
assert(() {
170179
if (mayRunWithAsserts)
171180
return true;
@@ -186,9 +195,16 @@ Future<void> benchmarkWidgets(WidgetTesterCallback callback, {bool mayRunWithAss
186195
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
187196
assert(binding is! AutomatedTestWidgetsFlutterBinding);
188197
final WidgetTester tester = WidgetTester._(binding);
198+
SemanticsHandle semanticsHandle;
199+
if (semanticsEnabled == true) {
200+
semanticsHandle = tester.ensureSemantics();
201+
}
189202
tester._recordNumberOfSemanticsHandles();
190203
return binding.runTest(
191-
() => callback(tester),
204+
() async {
205+
await callback(tester);
206+
semanticsHandle?.dispose();
207+
},
192208
tester._endOfTestVerifications,
193209
) ?? Future<void>.value();
194210
}

0 commit comments

Comments
 (0)
Please sign in to comment.