1// Copyright 2016 The Chromium 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 5import 'dart:async'; 6import 'package:flutter_test/flutter_test.dart'; 7import 'package:flutter/material.dart'; 8import 'package:flutter/rendering.dart'; 9import 'package:stocks/main.dart' as stocks; 10import 'package:stocks/stock_data.dart' as stock_data; 11 12import '../common.dart'; 13 14const Duration kBenchmarkTime = Duration(seconds: 15); 15 16Future<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 // We control the framePolicy below to prevent us from scheduling frames in 21 // the engine, so that the engine does not interfere with our timings. 22 final LiveTestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized(); 23 24 final Stopwatch watch = Stopwatch(); 25 int iterations = 0; 26 27 await benchmarkWidgets((WidgetTester tester) async { 28 stocks.main(); 29 await tester.pump(); // Start startup animation 30 await tester.pump(const Duration(seconds: 1)); // Complete startup animation 31 await tester.tapAt(const Offset(20.0, 40.0)); // Open drawer 32 await tester.pump(); // Start drawer animation 33 await tester.pump(const Duration(seconds: 1)); // Complete drawer animation 34 35 final Element appState = tester.element(find.byType(stocks.StocksApp)); 36 binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.benchmark; 37 38 watch.start(); 39 while (watch.elapsed < kBenchmarkTime) { 40 appState.markNeedsBuild(); 41 // We don't use tester.pump() because we're trying to drive it in an 42 // artificially high load to find out how much CPU each frame takes. 43 // This differs from normal benchmarks which might look at how many 44 // frames are missed, etc. 45 // We use Timer.run to ensure there's a microtask flush in between 46 // the two calls below. 47 await tester.pumpBenchmark(Duration(milliseconds: iterations * 16)); 48 iterations += 1; 49 } 50 watch.stop(); 51 }); 52 53 final BenchmarkResultPrinter printer = BenchmarkResultPrinter(); 54 printer.addResult( 55 description: 'Stock build', 56 value: watch.elapsedMicroseconds / iterations, 57 unit: 'µs per iteration', 58 name: 'stock_build_iteration', 59 ); 60 printer.printToStdout(); 61} 62