|
| 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 | + |
| 5 | +import 'dart:io'; |
| 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 | + |
| 14 | +const Duration kBenchmarkTime = const Duration(seconds: 15); |
| 15 | + |
| 16 | +class BenchmarkingBinding extends LiveTestWidgetsFlutterBinding { |
| 17 | + BenchmarkingBinding(this.stopwatch); |
| 18 | + |
| 19 | + final Stopwatch stopwatch; |
| 20 | + |
| 21 | + @override |
| 22 | + void handleBeginFrame(Duration rawTimeStamp) { |
| 23 | + stopwatch.start(); |
| 24 | + super.handleBeginFrame(rawTimeStamp); |
| 25 | + stopwatch.stop(); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +Future<Null> main() async { |
| 30 | + assert(false); // don't run this in checked mode! Use --release. |
| 31 | + stock_data.StockDataFetcher.actuallyFetchData = false; |
| 32 | + |
| 33 | + final Stopwatch wallClockWatch = new Stopwatch(); |
| 34 | + final Stopwatch cpuWatch = new Stopwatch(); |
| 35 | + new BenchmarkingBinding(cpuWatch); |
| 36 | + |
| 37 | + int totalOpenFrameElapsedMicroseconds = 0; |
| 38 | + int totalOpenIterationCount = 0; |
| 39 | + int totalCloseFrameElapsedMicroseconds = 0; |
| 40 | + int totalCloseIterationCount = 0; |
| 41 | + int totalSubsequentFramesElapsedMicroseconds = 0; |
| 42 | + int totalSubsequentFramesIterationCount = 0; |
| 43 | + |
| 44 | + await benchmarkWidgets((WidgetTester tester) async { |
| 45 | + stocks.main(); |
| 46 | + await tester.pump(); // Start startup animation |
| 47 | + await tester.pump(const Duration(seconds: 1)); // Complete startup animation |
| 48 | + |
| 49 | + bool drawerIsOpen = false; |
| 50 | + wallClockWatch.start(); |
| 51 | + while (wallClockWatch.elapsed < kBenchmarkTime) { |
| 52 | + cpuWatch.reset(); |
| 53 | + if (drawerIsOpen) { |
| 54 | + await tester.tapAt(const Point(780.0, 250.0)); // Close drawer |
| 55 | + await tester.pump(); |
| 56 | + totalCloseIterationCount += 1; |
| 57 | + totalCloseFrameElapsedMicroseconds += cpuWatch.elapsedMicroseconds; |
| 58 | + } else { |
| 59 | + await tester.tapAt(const Point(20.0, 50.0)); // Open drawer |
| 60 | + await tester.pump(); |
| 61 | + totalOpenIterationCount += 1; |
| 62 | + totalOpenFrameElapsedMicroseconds += cpuWatch.elapsedMicroseconds; |
| 63 | + } |
| 64 | + drawerIsOpen = !drawerIsOpen; |
| 65 | + |
| 66 | + // Time how long each frame takes |
| 67 | + cpuWatch.reset(); |
| 68 | + while (SchedulerBinding.instance.hasScheduledFrame) { |
| 69 | + await tester.pump(); |
| 70 | + totalSubsequentFramesIterationCount += 1; |
| 71 | + } |
| 72 | + totalSubsequentFramesElapsedMicroseconds += cpuWatch.elapsedMicroseconds; |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + print('Stock animation (ran for ${(wallClockWatch.elapsedMicroseconds / (1000 * 1000)).toStringAsFixed(1)}s):'); |
| 77 | + print(' Opening first frame average time: ${(totalOpenFrameElapsedMicroseconds / (totalOpenIterationCount)).toStringAsFixed(1)}µs per frame ($totalOpenIterationCount frames)'); |
| 78 | + print(' Closing first frame average time: ${(totalCloseFrameElapsedMicroseconds / (totalCloseIterationCount)).toStringAsFixed(1)}µs per frame ($totalCloseIterationCount frames)'); |
| 79 | + print(' Subsequent frames average time: ${(totalSubsequentFramesElapsedMicroseconds / (totalSubsequentFramesIterationCount)).toStringAsFixed(1)}µs per frame ($totalSubsequentFramesIterationCount frames)'); |
| 80 | + |
| 81 | + exit(0); |
| 82 | +} |
0 commit comments