layout_bench.dart 2.32 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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 12

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;

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

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

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

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

25
  final Stopwatch watch = Stopwatch();
26 27 28 29 30 31
  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
32
    await tester.tapAt(const Offset(20.0, 40.0)); // Open drawer
33 34 35
    await tester.pump(); // Start drawer animation
    await tester.pump(const Duration(seconds: 1)); // Complete drawer animation

36 37 38 39 40 41 42 43
    final TestViewConfiguration big = TestViewConfiguration(
      size: const Size(360.0, 640.0),
      window: RendererBinding.instance.window,
    );
    final TestViewConfiguration small = TestViewConfiguration(
      size: const Size(355.0, 635.0),
      window: RendererBinding.instance.window,
    );
44
    final RenderView renderView = WidgetsBinding.instance.renderView;
45
    binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.benchmark;
46 47 48 49

    watch.start();
    while (watch.elapsed < kBenchmarkTime) {
      renderView.configuration = (iterations % 2 == 0) ? big : small;
50
      await tester.pumpBenchmark(Duration(milliseconds: iterations * 16));
51 52 53 54 55
      iterations += 1;
    }
    watch.stop();
  });

56
  final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
57 58 59 60 61 62 63
  printer.addResult(
    description: 'Stock layout',
    value: watch.elapsedMicroseconds / iterations,
    unit: 'µs per iteration',
    name: 'stock_layout_iteration',
  );
  printer.printToStdout();
64
}