build_bench.dart 2.37 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:async';
6 7 8 9 10 11
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:stocks/main.dart' as stocks;
import 'package:stocks/stock_data.dart' as stock_data;

12 13
import '../common.dart';

14
const Duration kBenchmarkTime = Duration(seconds: 15);
15

16
Future<void> main() async {
17
  assert(false, "Don't run benchmarks in checked mode! Use 'flutter run --release'.");
18
  stock_data.StockData.actuallyFetchData = false;
19

20 21
  // We control the framePolicy below to prevent us from scheduling frames in
  // the engine, so that the engine does not interfere with our timings.
22
  final LiveTestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as LiveTestWidgetsFlutterBinding;
23

24
  final Stopwatch watch = Stopwatch();
25 26 27 28 29 30
  int iterations = 0;

  await benchmarkWidgets((WidgetTester tester) async {
    stocks.main();
    await tester.pump(); // Start startup animation
    await tester.pump(const Duration(seconds: 1)); // Complete startup animation
31
    await tester.tapAt(const Offset(20.0, 40.0)); // Open drawer
32 33 34
    await tester.pump(); // Start drawer animation
    await tester.pump(const Duration(seconds: 1)); // Complete drawer animation

35
    final Element appState = tester.element(find.byType(stocks.StocksApp));
36
    binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.benchmark;
37 38 39

    watch.start();
    while (watch.elapsed < kBenchmarkTime) {
40
      appState.markNeedsBuild();
41 42 43 44
      // We don't use tester.pump() because we're trying to drive it in an
      // artificially high load to find out how much CPU each frame takes.
      // This differs from normal benchmarks which might look at how many
      // frames are missed, etc.
45 46
      // We use Timer.run to ensure there's a microtask flush in between
      // the two calls below.
47
      await tester.pumpBenchmark(Duration(milliseconds: iterations * 16));
48 49 50 51 52
      iterations += 1;
    }
    watch.stop();
  });

53
  final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
54 55 56 57 58 59 60
  printer.addResult(
    description: 'Stock build',
    value: watch.elapsedMicroseconds / iterations,
    unit: 'µs per iteration',
    name: 'stock_build_iteration',
  );
  printer.printToStdout();
61
}