Ian Hickson | b5c6da1 | 2016-05-23 15:04:50 -0700 | [diff] [blame] | 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 | |
Yegor | 42ccffc | 2017-01-10 18:16:00 -0800 | [diff] [blame] | 5 | import 'dart:async'; |
Ian Hickson | b5c6da1 | 2016-05-23 15:04:50 -0700 | [diff] [blame] | 6 | |
| 7 | import 'package:flutter_test/flutter_test.dart'; |
| 8 | import 'package:flutter/material.dart'; |
| 9 | import 'package:flutter/rendering.dart'; |
| 10 | import 'package:flutter/scheduler.dart'; |
| 11 | import 'package:stocks/main.dart' as stocks; |
| 12 | import 'package:stocks/stock_data.dart' as stock_data; |
| 13 | |
Yegor | 1df639b | 2017-01-09 14:57:14 -0800 | [diff] [blame] | 14 | import '../common.dart'; |
| 15 | |
Todd Volkert | 00aac68 | 2018-07-27 08:44:39 -0700 | [diff] [blame] | 16 | const Duration kBenchmarkTime = const Duration(seconds: 15); |
Ian Hickson | b5c6da1 | 2016-05-23 15:04:50 -0700 | [diff] [blame] | 17 | |
| 18 | class BenchmarkingBinding extends LiveTestWidgetsFlutterBinding { |
| 19 | BenchmarkingBinding(this.stopwatch); |
| 20 | |
| 21 | final Stopwatch stopwatch; |
| 22 | |
| 23 | @override |
| 24 | void handleBeginFrame(Duration rawTimeStamp) { |
| 25 | stopwatch.start(); |
| 26 | super.handleBeginFrame(rawTimeStamp); |
Ian Hickson | e8c4692 | 2017-05-02 09:27:53 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | @override |
| 30 | void handleDrawFrame() { |
| 31 | super.handleDrawFrame(); |
Ian Hickson | b5c6da1 | 2016-05-23 15:04:50 -0700 | [diff] [blame] | 32 | stopwatch.stop(); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | Future<Null> main() async { |
| 37 | assert(false); // don't run this in checked mode! Use --release. |
Ian Hickson | 9adb4a7 | 2017-06-23 14:58:29 -0700 | [diff] [blame] | 38 | stock_data.StockData.actuallyFetchData = false; |
Ian Hickson | b5c6da1 | 2016-05-23 15:04:50 -0700 | [diff] [blame] | 39 | |
| 40 | final Stopwatch wallClockWatch = new Stopwatch(); |
| 41 | final Stopwatch cpuWatch = new Stopwatch(); |
| 42 | new BenchmarkingBinding(cpuWatch); |
| 43 | |
| 44 | int totalOpenFrameElapsedMicroseconds = 0; |
| 45 | int totalOpenIterationCount = 0; |
| 46 | int totalCloseFrameElapsedMicroseconds = 0; |
| 47 | int totalCloseIterationCount = 0; |
| 48 | int totalSubsequentFramesElapsedMicroseconds = 0; |
| 49 | int totalSubsequentFramesIterationCount = 0; |
| 50 | |
| 51 | await benchmarkWidgets((WidgetTester tester) async { |
| 52 | stocks.main(); |
| 53 | await tester.pump(); // Start startup animation |
| 54 | await tester.pump(const Duration(seconds: 1)); // Complete startup animation |
| 55 | |
| 56 | bool drawerIsOpen = false; |
| 57 | wallClockWatch.start(); |
| 58 | while (wallClockWatch.elapsed < kBenchmarkTime) { |
| 59 | cpuWatch.reset(); |
| 60 | if (drawerIsOpen) { |
Ian Hickson | bf017b7 | 2017-04-12 15:06:12 -0700 | [diff] [blame] | 61 | await tester.tapAt(const Offset(780.0, 250.0)); // Close drawer |
Ian Hickson | b5c6da1 | 2016-05-23 15:04:50 -0700 | [diff] [blame] | 62 | await tester.pump(); |
| 63 | totalCloseIterationCount += 1; |
| 64 | totalCloseFrameElapsedMicroseconds += cpuWatch.elapsedMicroseconds; |
| 65 | } else { |
Ian Hickson | bf017b7 | 2017-04-12 15:06:12 -0700 | [diff] [blame] | 66 | await tester.tapAt(const Offset(20.0, 50.0)); // Open drawer |
Ian Hickson | b5c6da1 | 2016-05-23 15:04:50 -0700 | [diff] [blame] | 67 | await tester.pump(); |
| 68 | totalOpenIterationCount += 1; |
| 69 | totalOpenFrameElapsedMicroseconds += cpuWatch.elapsedMicroseconds; |
| 70 | } |
| 71 | drawerIsOpen = !drawerIsOpen; |
| 72 | |
| 73 | // Time how long each frame takes |
| 74 | cpuWatch.reset(); |
| 75 | while (SchedulerBinding.instance.hasScheduledFrame) { |
| 76 | await tester.pump(); |
| 77 | totalSubsequentFramesIterationCount += 1; |
| 78 | } |
| 79 | totalSubsequentFramesElapsedMicroseconds += cpuWatch.elapsedMicroseconds; |
| 80 | } |
| 81 | }); |
| 82 | |
Chris Bracken | 6c97dd2 | 2017-03-03 18:06:08 -0800 | [diff] [blame] | 83 | final BenchmarkResultPrinter printer = new BenchmarkResultPrinter(); |
Yegor | 1df639b | 2017-01-09 14:57:14 -0800 | [diff] [blame] | 84 | printer.addResult( |
| 85 | description: 'Stock animation', |
| 86 | value: wallClockWatch.elapsedMicroseconds / (1000 * 1000), |
| 87 | unit: 's', |
| 88 | name: 'stock_animation_total_run_time', |
| 89 | ); |
| 90 | printer.addResult( |
| 91 | description: ' Opening first frame average time', |
| 92 | value: totalOpenFrameElapsedMicroseconds / totalOpenIterationCount, |
| 93 | unit: 'µs per frame ($totalOpenIterationCount frames)', |
| 94 | name: 'stock_animation_open_first_frame_average', |
| 95 | ); |
| 96 | printer.addResult( |
| 97 | description: ' Closing first frame average time', |
| 98 | value: totalCloseFrameElapsedMicroseconds / totalCloseIterationCount, |
| 99 | unit: 'µs per frame ($totalCloseIterationCount frames)', |
| 100 | name: 'stock_animation_close_first_frame_average', |
| 101 | ); |
| 102 | printer.addResult( |
| 103 | description: ' Subsequent frames average time', |
| 104 | value: totalSubsequentFramesElapsedMicroseconds / totalSubsequentFramesIterationCount, |
| 105 | unit: 'µs per frame ($totalSubsequentFramesIterationCount frames)', |
| 106 | name: 'stock_animation_subsequent_frame_average', |
| 107 | ); |
| 108 | printer.printToStdout(); |
Ian Hickson | b5c6da1 | 2016-05-23 15:04:50 -0700 | [diff] [blame] | 109 | } |