|
| 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 | +} |
0 commit comments