Skip to content

Commit e992023

Browse files
Add a microbenchmark for text intrinsic height layout (#145007)
For flutter/flutter#144577. There's no promise that the performance will be great when `IntrinsicHeight/IntrinsicWidth` is used extensively but it's not that uncommon of a widget.
1 parent eb74e5a commit e992023

File tree

2 files changed

+63
-0
lines changed

2 files changed

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

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)