Skip to content

Commit 8270872

Browse files
Reland "Add a microbenchmark for text intrinsic height layout (flutter#145007)" (flutter#145037)
Add the missing `Directionality` widget and `await test.pump()` call: commit: flutter@0fd7712 Without the `pump` 1s, it sometimes schedules multiple `handleBeginFrame`s and `handleDrawFrame`s.
1 parent 2fde3ac commit 8270872

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2014 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+
import 'package:flutter/rendering.dart';
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
import '../common.dart';
10+
11+
const Duration kBenchmarkTime = Duration(seconds: 15);
12+
13+
// Use an Align to loosen the constraints.
14+
final Widget intrinsicTextHeight = Directionality(
15+
textDirection: TextDirection.ltr,
16+
child: Align(
17+
child: IntrinsicHeight(
18+
child: Text('A' * 100),
19+
),
20+
),
21+
);
22+
23+
Future<void> main() async {
24+
assert(false, "Don't run benchmarks in debug mode! Use 'flutter run --release'.");
25+
26+
// We control the framePolicy below to prevent us from scheduling frames in
27+
// the engine, so that the engine does not interfere with our timings.
28+
final LiveTestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as LiveTestWidgetsFlutterBinding;
29+
30+
final Stopwatch watch = Stopwatch();
31+
int iterations = 0;
32+
33+
await benchmarkWidgets((WidgetTester tester) async {
34+
runApp(intrinsicTextHeight);
35+
// Wait for the UI to stabilize.
36+
await tester.pump(const Duration(seconds: 1));
37+
38+
final TestViewConfiguration big = TestViewConfiguration.fromView(
39+
size: const Size(360.0, 640.0),
40+
view: tester.view,
41+
);
42+
final TestViewConfiguration small = TestViewConfiguration.fromView(
43+
size: const Size(100.0, 640.0),
44+
view: tester.view,
45+
);
46+
final RenderView renderView = WidgetsBinding.instance.renderViews.single;
47+
binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.benchmark;
48+
49+
watch.start();
50+
while (watch.elapsed < kBenchmarkTime) {
51+
renderView.configuration = iterations.isEven ? big : small;
52+
await tester.pumpBenchmark(Duration(milliseconds: iterations * 16));
53+
iterations += 1;
54+
}
55+
watch.stop();
56+
});
57+
58+
59+
final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
60+
printer.addResult(
61+
description: 'Text intrinsic height',
62+
value: watch.elapsedMicroseconds / iterations,
63+
unit: 'µs per iteration',
64+
name: 'text_intrinsic_height_iteration',
65+
);
66+
printer.printToStdout();
67+
}

dev/devicelab/lib/tasks/microbenchmarks.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ TaskFunction createMicrobenchmarkTask({
7474
...await runMicrobench('lib/stocks/build_bench.dart'),
7575
...await runMicrobench('lib/stocks/layout_bench.dart'),
7676
...await runMicrobench('lib/ui/image_bench.dart'),
77+
...await runMicrobench('lib/layout/text_intrinsic_bench.dart'),
7778
};
7879

7980
return TaskResult.success(allResults,

0 commit comments

Comments
 (0)