diff --git a/dev/benchmarks/microbenchmarks/.gitignore b/dev/benchmarks/microbenchmarks/.gitignore new file mode 100644 index 0000000000..14c7d4c3f7 --- /dev/null +++ b/dev/benchmarks/microbenchmarks/.gitignore @@ -0,0 +1,9 @@ +.DS_Store +.atom/ +.idea +.packages +.pub/ +build/ +ios/.generated/ +packages +pubspec.lock diff --git a/dev/benchmarks/microbenchmarks/README.md b/dev/benchmarks/microbenchmarks/README.md new file mode 100644 index 0000000000..6f6e31758b --- /dev/null +++ b/dev/benchmarks/microbenchmarks/README.md @@ -0,0 +1,12 @@ +# microbenchmarks + +To run these benchmarks on a device: + +``` +flutter run --release -t lib/gestures/velocity_tracker_data.dart +flutter run --release -t lib/stocks/animation_bench.dart +flutter run --release -t lib/stocks/build_bench.dart +flutter run --release -t lib/stocks/layout_bench.dart +``` + +The results should be in the device logs. diff --git a/dev/benchmarks/microbenchmarks/android/AndroidManifest.xml b/dev/benchmarks/microbenchmarks/android/AndroidManifest.xml new file mode 100644 index 0000000000..6aac56c5bb --- /dev/null +++ b/dev/benchmarks/microbenchmarks/android/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + diff --git a/dev/benchmarks/microbenchmarks/android/res/mipmap-hdpi/ic_launcher.png b/dev/benchmarks/microbenchmarks/android/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000000..f0a55cda78 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/android/res/mipmap-hdpi/ic_launcher.png differ diff --git a/dev/benchmarks/microbenchmarks/android/res/mipmap-mdpi/ic_launcher.png b/dev/benchmarks/microbenchmarks/android/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000000..eb98dc451d Binary files /dev/null and b/dev/benchmarks/microbenchmarks/android/res/mipmap-mdpi/ic_launcher.png differ diff --git a/dev/benchmarks/microbenchmarks/android/res/mipmap-xhdpi/ic_launcher.png b/dev/benchmarks/microbenchmarks/android/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000000..f1783dbbf3 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/android/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/dev/benchmarks/microbenchmarks/android/res/mipmap-xxhdpi/ic_launcher.png b/dev/benchmarks/microbenchmarks/android/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000000..46828a290a Binary files /dev/null and b/dev/benchmarks/microbenchmarks/android/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/dev/benchmarks/microbenchmarks/android/res/mipmap-xxxhdpi/ic_launcher.png b/dev/benchmarks/microbenchmarks/android/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000000..a5276715d9 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/android/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/dev/benchmarks/microbenchmarks/flutter.yaml b/dev/benchmarks/microbenchmarks/flutter.yaml new file mode 100644 index 0000000000..810d01190d --- /dev/null +++ b/dev/benchmarks/microbenchmarks/flutter.yaml @@ -0,0 +1,2 @@ +name: microbenchmarks +uses-material-design: true diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Contents.json b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000000..363d1c4ce4 --- /dev/null +++ b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,142 @@ +{ + "images" : [ + { + "size" : "29x29", + "idiom" : "iphone", + "filename" : "Icon-Small@2x.png", + "scale" : "2x" + }, + { + "size" : "29x29", + "idiom" : "iphone", + "filename" : "Icon-Small@3x.png", + "scale" : "3x" + }, + { + "size" : "40x40", + "idiom" : "iphone", + "filename" : "Icon-Small-40@2x.png", + "scale" : "2x" + }, + { + "size" : "40x40", + "idiom" : "iphone", + "filename" : "Icon-Small-40@3x.png", + "scale" : "3x" + }, + { + "size" : "60x60", + "idiom" : "iphone", + "filename" : "Icon-60@2x.png", + "scale" : "2x" + }, + { + "size" : "60x60", + "idiom" : "iphone", + "filename" : "Icon-60@3x.png", + "scale" : "3x" + }, + { + "size" : "29x29", + "idiom" : "ipad", + "filename" : "Icon-Small.png", + "scale" : "1x" + }, + { + "size" : "29x29", + "idiom" : "ipad", + "filename" : "Icon-Small@2x.png", + "scale" : "2x" + }, + { + "size" : "40x40", + "idiom" : "ipad", + "filename" : "Icon-Small-40.png", + "scale" : "1x" + }, + { + "size" : "40x40", + "idiom" : "ipad", + "filename" : "Icon-Small-40@2x.png", + "scale" : "2x" + }, + { + "size" : "76x76", + "idiom" : "ipad", + "filename" : "Icon-76.png", + "scale" : "1x" + }, + { + "size" : "76x76", + "idiom" : "ipad", + "filename" : "Icon-76@2x.png", + "scale" : "2x" + }, + { + "size" : "83.5x83.5", + "idiom" : "ipad", + "filename" : "Icon-83.5@2x.png", + "scale" : "2x" + }, + { + "size" : "16x16", + "idiom" : "mac", + "filename" : "icon_16x16.png", + "scale" : "1x" + }, + { + "size" : "16x16", + "idiom" : "mac", + "filename" : "icon_16x16@2x.png", + "scale" : "2x" + }, + { + "size" : "32x32", + "idiom" : "mac", + "filename" : "icon_32x32.png", + "scale" : "1x" + }, + { + "size" : "32x32", + "idiom" : "mac", + "filename" : "icon_32x32@2x.png", + "scale" : "2x" + }, + { + "size" : "128x128", + "idiom" : "mac", + "filename" : "icon_128x128.png", + "scale" : "1x" + }, + { + "size" : "128x128", + "idiom" : "mac", + "filename" : "icon_128x128@2x.png", + "scale" : "2x" + }, + { + "size" : "256x256", + "idiom" : "mac", + "filename" : "icon_256x256.png", + "scale" : "1x" + }, + { + "size" : "256x256", + "idiom" : "mac", + "filename" : "icon_256x256@2x.png", + "scale" : "2x" + }, + { + "size" : "512x512", + "idiom" : "mac", + "filename" : "icon_512x512.png", + "scale" : "1x" + }, + { + "size" : "512x512", + "idiom" : "mac", + "filename" : "icon_512x512@2x.png", + "scale" : "2x" + } + ] +} \ No newline at end of file diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-60@2x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-60@2x.png new file mode 100644 index 0000000000..9996f5e598 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-60@2x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-60@3x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-60@3x.png new file mode 100644 index 0000000000..7a543edbdd Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-60@3x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-76.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-76.png new file mode 100644 index 0000000000..05a8268104 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-76.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-76@2x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-76@2x.png new file mode 100644 index 0000000000..bfbca28f5d Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-76@2x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-83.5@2x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-83.5@2x.png new file mode 100644 index 0000000000..c924b8e497 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-83.5@2x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40.png new file mode 100644 index 0000000000..3006f1d348 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40@2x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40@2x.png new file mode 100644 index 0000000000..5f7d22d364 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40@2x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40@3x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40@3x.png new file mode 100644 index 0000000000..9996f5e598 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40@3x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small.png new file mode 100644 index 0000000000..3433da1196 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small@2x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small@2x.png new file mode 100644 index 0000000000..f7f9f16d1b Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small@2x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small@3x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small@3x.png new file mode 100644 index 0000000000..e9c23601cf Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small@3x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_128x128.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_128x128.png new file mode 100644 index 0000000000..dd5fccb0a9 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_128x128.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_128x128@2x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_128x128@2x.png new file mode 100644 index 0000000000..1909e27511 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_128x128@2x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_16x16.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_16x16.png new file mode 100644 index 0000000000..40a701b514 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_16x16.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_16x16@2x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_16x16@2x.png new file mode 100644 index 0000000000..06c1a80560 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_16x16@2x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_256x256.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_256x256.png new file mode 100644 index 0000000000..1909e27511 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_256x256.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_256x256@2x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_256x256@2x.png new file mode 100644 index 0000000000..69c96ef1e2 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_256x256@2x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_32x32.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_32x32.png new file mode 100644 index 0000000000..06c1a80560 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_32x32.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_32x32@2x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_32x32@2x.png new file mode 100644 index 0000000000..0f94371460 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_32x32@2x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_512x512.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_512x512.png new file mode 100644 index 0000000000..69c96ef1e2 Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_512x512.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_512x512@2x.png b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_512x512@2x.png new file mode 100644 index 0000000000..1d6355f58c Binary files /dev/null and b/dev/benchmarks/microbenchmarks/ios/Assets.xcassets/AppIcon.appiconset/icon_512x512@2x.png differ diff --git a/dev/benchmarks/microbenchmarks/ios/Info.plist b/dev/benchmarks/microbenchmarks/ios/Info.plist new file mode 100644 index 0000000000..686f5103e0 --- /dev/null +++ b/dev/benchmarks/microbenchmarks/ios/Info.plist @@ -0,0 +1,47 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + Runner + CFBundleIdentifier + com.yourcompany.microbenchmarks + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + microbenchmarks + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIRequiredDeviceCapabilities + + arm64 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UIViewControllerBasedStatusBarAppearance + + + diff --git a/dev/benchmarks/microbenchmarks/ios/LaunchScreen.storyboard b/dev/benchmarks/microbenchmarks/ios/LaunchScreen.storyboard new file mode 100644 index 0000000000..78686cd075 --- /dev/null +++ b/dev/benchmarks/microbenchmarks/ios/LaunchScreen.storyboard @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packages/flutter/benchmark/gestures/velocity_tracker_data.dart b/dev/benchmarks/microbenchmarks/lib/gestures/data/velocity_tracker_data.dart similarity index 99% rename from packages/flutter/benchmark/gestures/velocity_tracker_data.dart rename to dev/benchmarks/microbenchmarks/lib/gestures/data/velocity_tracker_data.dart index ae4f593c74..b31ee551de 100644 --- a/packages/flutter/benchmark/gestures/velocity_tracker_data.dart +++ b/dev/benchmarks/microbenchmarks/lib/gestures/data/velocity_tracker_data.dart @@ -1,3 +1,7 @@ +// 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. + import 'package:flutter/gestures.dart'; /// Data for velocity_tracker_bench.dart diff --git a/dev/benchmarks/microbenchmarks/lib/gestures/velocity_tracker_bench.dart b/dev/benchmarks/microbenchmarks/lib/gestures/velocity_tracker_bench.dart new file mode 100644 index 0000000000..f9344a0147 --- /dev/null +++ b/dev/benchmarks/microbenchmarks/lib/gestures/velocity_tracker_bench.dart @@ -0,0 +1,27 @@ +// 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. + +import 'dart:io'; +import 'package:flutter/gestures.dart'; +import 'data/velocity_tracker_data.dart'; + +const int _kNumIters = 10000; + +void main() { + final VelocityTracker tracker = new VelocityTracker(); + final Stopwatch watch = new Stopwatch(); + print('Velocity tracker benchmark...'); + watch.start(); + for (int i = 0; i < _kNumIters; i += 1) { + for (PointerEvent event in velocityEventData) { + if (event is PointerDownEvent || event is PointerMoveEvent) + tracker.addPosition(event.timeStamp, event.position); + if (event is PointerUpEvent) + tracker.getVelocity(); + } + } + watch.stop(); + print('Velocity tracker: ${(watch.elapsedMicroseconds / _kNumIters).toStringAsFixed(1)}µs per iteration'); + exit(0); +} diff --git a/dev/benchmarks/microbenchmarks/lib/stocks/animation_bench.dart b/dev/benchmarks/microbenchmarks/lib/stocks/animation_bench.dart new file mode 100644 index 0000000000..717b123eb3 --- /dev/null +++ b/dev/benchmarks/microbenchmarks/lib/stocks/animation_bench.dart @@ -0,0 +1,82 @@ +// 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. + +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/scheduler.dart'; +import 'package:stocks/main.dart' as stocks; +import 'package:stocks/stock_data.dart' as stock_data; + +const Duration kBenchmarkTime = const Duration(seconds: 15); + +class BenchmarkingBinding extends LiveTestWidgetsFlutterBinding { + BenchmarkingBinding(this.stopwatch); + + final Stopwatch stopwatch; + + @override + void handleBeginFrame(Duration rawTimeStamp) { + stopwatch.start(); + super.handleBeginFrame(rawTimeStamp); + stopwatch.stop(); + } +} + +Future main() async { + assert(false); // don't run this in checked mode! Use --release. + stock_data.StockDataFetcher.actuallyFetchData = false; + + final Stopwatch wallClockWatch = new Stopwatch(); + final Stopwatch cpuWatch = new Stopwatch(); + new BenchmarkingBinding(cpuWatch); + + int totalOpenFrameElapsedMicroseconds = 0; + int totalOpenIterationCount = 0; + int totalCloseFrameElapsedMicroseconds = 0; + int totalCloseIterationCount = 0; + int totalSubsequentFramesElapsedMicroseconds = 0; + int totalSubsequentFramesIterationCount = 0; + + await benchmarkWidgets((WidgetTester tester) async { + stocks.main(); + await tester.pump(); // Start startup animation + await tester.pump(const Duration(seconds: 1)); // Complete startup animation + + bool drawerIsOpen = false; + wallClockWatch.start(); + while (wallClockWatch.elapsed < kBenchmarkTime) { + cpuWatch.reset(); + if (drawerIsOpen) { + await tester.tapAt(const Point(780.0, 250.0)); // Close drawer + await tester.pump(); + totalCloseIterationCount += 1; + totalCloseFrameElapsedMicroseconds += cpuWatch.elapsedMicroseconds; + } else { + await tester.tapAt(const Point(20.0, 50.0)); // Open drawer + await tester.pump(); + totalOpenIterationCount += 1; + totalOpenFrameElapsedMicroseconds += cpuWatch.elapsedMicroseconds; + } + drawerIsOpen = !drawerIsOpen; + + // Time how long each frame takes + cpuWatch.reset(); + while (SchedulerBinding.instance.hasScheduledFrame) { + await tester.pump(); + totalSubsequentFramesIterationCount += 1; + } + totalSubsequentFramesElapsedMicroseconds += cpuWatch.elapsedMicroseconds; + } + }); + + print('Stock animation (ran for ${(wallClockWatch.elapsedMicroseconds / (1000 * 1000)).toStringAsFixed(1)}s):'); + print(' Opening first frame average time: ${(totalOpenFrameElapsedMicroseconds / (totalOpenIterationCount)).toStringAsFixed(1)}µs per frame ($totalOpenIterationCount frames)'); + print(' Closing first frame average time: ${(totalCloseFrameElapsedMicroseconds / (totalCloseIterationCount)).toStringAsFixed(1)}µs per frame ($totalCloseIterationCount frames)'); + print(' Subsequent frames average time: ${(totalSubsequentFramesElapsedMicroseconds / (totalSubsequentFramesIterationCount)).toStringAsFixed(1)}µs per frame ($totalSubsequentFramesIterationCount frames)'); + + exit(0); +} diff --git a/dev/benchmarks/microbenchmarks/lib/stocks/build_bench.dart b/dev/benchmarks/microbenchmarks/lib/stocks/build_bench.dart new file mode 100644 index 0000000000..4df214e88f --- /dev/null +++ b/dev/benchmarks/microbenchmarks/lib/stocks/build_bench.dart @@ -0,0 +1,43 @@ +import 'dart:io'; + +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; + +const Duration kBenchmarkTime = const Duration(seconds: 15); + +void _doNothing() { } + +Future main() async { + assert(false); // don't run this in checked mode! Use --release. + stock_data.StockDataFetcher.actuallyFetchData = false; + + final Stopwatch watch = new Stopwatch(); + 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 + await tester.tapAt(new Point(20.0, 20.0)); // Open drawer + await tester.pump(); // Start drawer animation + await tester.pump(const Duration(seconds: 1)); // Complete drawer animation + + + final stocks.StocksAppState appState = tester.state(find.byType(stocks.StocksApp)); + final BuildOwner buildOwner = WidgetsBinding.instance.buildOwner; + + watch.start(); + while (watch.elapsed < kBenchmarkTime) { + appState.setState(_doNothing); + buildOwner.buildDirtyElements(); + iterations += 1; + } + watch.stop(); + }); + + print('Stock build: ${(watch.elapsedMicroseconds / iterations).toStringAsFixed(1)}µs per iteration'); + exit(0); +} diff --git a/dev/benchmarks/microbenchmarks/lib/stocks/layout_bench.dart b/dev/benchmarks/microbenchmarks/lib/stocks/layout_bench.dart new file mode 100644 index 0000000000..0a1d199901 --- /dev/null +++ b/dev/benchmarks/microbenchmarks/lib/stocks/layout_bench.dart @@ -0,0 +1,43 @@ +import 'dart:io'; + +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; + +const Duration kBenchmarkTime = const Duration(seconds: 15); + + + +Future main() async { + assert(false); // don't run this in checked mode! Use --release. + stock_data.StockDataFetcher.actuallyFetchData = false; + + final Stopwatch watch = new Stopwatch(); + 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 + await tester.tapAt(new Point(20.0, 20.0)); // Open drawer + await tester.pump(); // Start drawer animation + await tester.pump(const Duration(seconds: 1)); // Complete drawer animation + + final TestViewConfiguration big = new TestViewConfiguration(size: const Size(360.0, 640.0)); + final TestViewConfiguration small = new TestViewConfiguration(size: const Size(355.0, 635.0)); + final RenderView renderView = WidgetsBinding.instance.renderView; + + watch.start(); + while (watch.elapsed < kBenchmarkTime) { + renderView.configuration = (iterations % 2 == 0) ? big : small; + RendererBinding.instance.pipelineOwner.flushLayout(); + iterations += 1; + } + watch.stop(); + }); + + print('Stock layout: ${(watch.elapsedMicroseconds / iterations).toStringAsFixed(1)}µs per iteration'); + exit(0); +} diff --git a/dev/benchmarks/microbenchmarks/pubspec.yaml b/dev/benchmarks/microbenchmarks/pubspec.yaml new file mode 100644 index 0000000000..d04d09e3e3 --- /dev/null +++ b/dev/benchmarks/microbenchmarks/pubspec.yaml @@ -0,0 +1,9 @@ +name: microbenchmarks +description: A new flutter project. +dependencies: + flutter: + path: ../../../packages/flutter + flutter_test: + path: ../../../packages/flutter_test + stocks: + path: ../../../examples/stocks diff --git a/packages/flutter/benchmark/README.md b/packages/flutter/benchmark/README.md deleted file mode 100644 index 4c2f23ee18..0000000000 --- a/packages/flutter/benchmark/README.md +++ /dev/null @@ -1,22 +0,0 @@ -# Benchmarks - -This directory (and its sub-directories) contain benchmarks for -Flutter. The reporting format for benchmarks is not standardized yet, -so benchmarks here are typically run by hand. To run a benchmark: - -1. Build `sky_shell` for Linux Release using the instructions in the - [Engine repository](https://github.com/flutter/engine). - -2. Run `pub get` in the `packages/flutter` directory. - -3. Run the benchmarks by running the following command from the root - of the flutter repository. Replace `stocks/layout_bench.dart` with - the path to whichever benchmark you want to run. If you didn't - build the engine in the recommended place, then also update the - path accordingly. If you made changes to sky_services, you'll also - need to update the `pubspec.yaml` file to point to that using a - dependency_override. - -``` -../engine/src/out/Release/sky_shell packages/flutter/benchmark/stocks/layout_bench.dart --package-root=packages/flutter/packages -``` diff --git a/packages/flutter/benchmark/gestures/velocity_tracker_bench.dart b/packages/flutter/benchmark/gestures/velocity_tracker_bench.dart deleted file mode 100644 index 776ba6359d..0000000000 --- a/packages/flutter/benchmark/gestures/velocity_tracker_bench.dart +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2015 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. - -import 'package:flutter/gestures.dart'; -import 'package:test/test.dart'; -import 'velocity_tracker_data.dart'; - -const int _kNumIters = 10000; - -void main() { - test('Dart velocity tracker performance', () { - VelocityTracker tracker = new VelocityTracker(); - Stopwatch watch = new Stopwatch(); - watch.start(); - for (int i = 0; i < _kNumIters; i++) { - for (PointerEvent event in velocityEventData) { - if (event is PointerDownEvent || event is PointerMoveEvent) - tracker.addPosition(event.timeStamp, event.position); - if (event is PointerUpEvent) - tracker.getVelocity(); - } - } - watch.stop(); - print("Dart tracker: " + watch.elapsed.toString()); - }); -} diff --git a/packages/flutter/benchmark/stocks/animation_bench.dart b/packages/flutter/benchmark/stocks/animation_bench.dart deleted file mode 100644 index 6b876a227b..0000000000 --- a/packages/flutter/benchmark/stocks/animation_bench.dart +++ /dev/null @@ -1,50 +0,0 @@ -import 'dart:io'; - -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; - -const int _kNumberOfIterations = 50000; -const bool _kRunForever = false; - -void main() { - stock_data.StockDataFetcher.actuallyFetchData = false; - - const Duration _kAnimationDuration = const Duration(milliseconds: 200); - const Duration tickDuration = const Duration(milliseconds: 2); - int numberOfTicks = _kAnimationDuration.inMicroseconds ~/ tickDuration.inMicroseconds; - int numberOfRounts = _kNumberOfIterations ~/ numberOfTicks; - - Stopwatch watch = new Stopwatch() - ..start(); - - benchmarkWidgets((WidgetTester tester) { - stocks.main(); - tester.pump(); // Start startup animation - tester.pump(const Duration(seconds: 1)); // Complete startup animation - - bool drawerIsOpen = false; - - for (int i = 0; i < numberOfRounts || _kRunForever; ++i) { - if (drawerIsOpen) - tester.tapAt(const Point(20.0, 20.0)); // Open drawer - else - tester.tapAt(const Point(780.0, 20.0)); // Close drawer - - tester.pump(); // Start drawer animation - - for (int j = 0; j < numberOfTicks; ++j) - tester.pump(tickDuration); - - tester.pump(const Duration(seconds: 1)); // Complete animation - drawerIsOpen = !drawerIsOpen; - } - }); - - watch.stop(); - print("Stock animation: " + watch.elapsed.toString()); - - exit(0); -} diff --git a/packages/flutter/benchmark/stocks/build_bench.dart b/packages/flutter/benchmark/stocks/build_bench.dart deleted file mode 100644 index 47ccf4f648..0000000000 --- a/packages/flutter/benchmark/stocks/build_bench.dart +++ /dev/null @@ -1,42 +0,0 @@ -import 'dart:io'; - -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; - -const int _kNumberOfIterations = 100000; -const bool _kRunForever = false; - -void _doNothing() { } - -void main() { - stock_data.StockDataFetcher.actuallyFetchData = false; - - stocks.StocksAppState appState; - - benchmarkWidgets((WidgetTester tester) { - stocks.main(); - tester.pump(); // Start startup animation - tester.pump(const Duration(seconds: 1)); // Complete startup animation - tester.tapAt(new Point(20.0, 20.0)); // Open drawer - tester.pump(); // Start drawer animation - tester.pump(const Duration(seconds: 1)); // Complete drawer animation - appState = tester.state(find.byType(stocks.StocksApp)); - }); - - BuildOwner buildOwner = WidgetsBinding.instance.buildOwner; - - Stopwatch watch = new Stopwatch() - ..start(); - - for (int i = 0; i < _kNumberOfIterations || _kRunForever; ++i) { - appState.setState(_doNothing); - buildOwner.buildDirtyElements(); - } - - watch.stop(); - print("Stock build: " + watch.elapsed.toString()); - exit(0); -} diff --git a/packages/flutter/benchmark/stocks/layout_bench.dart b/packages/flutter/benchmark/stocks/layout_bench.dart deleted file mode 100644 index 8f81191ece..0000000000 --- a/packages/flutter/benchmark/stocks/layout_bench.dart +++ /dev/null @@ -1,39 +0,0 @@ -import 'dart:io'; - -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; - -const int _kNumberOfIterations = 100000; -const bool _kRunForever = false; - -void main() { - stock_data.StockDataFetcher.actuallyFetchData = false; - - benchmarkWidgets((WidgetTester tester) { - stocks.main(); - tester.pump(); // Start startup animation - tester.pump(const Duration(seconds: 1)); // Complete startup animation - tester.tapAt(new Point(20.0, 20.0)); // Open drawer - tester.pump(); // Start drawer animation - tester.pump(const Duration(seconds: 1)); // Complete drawer animation - }); - - ViewConfiguration big = const ViewConfiguration(size: const Size(360.0, 640.0)); - ViewConfiguration small = const ViewConfiguration(size: const Size(355.0, 635.0)); - RenderView renderView = WidgetsBinding.instance.renderView; - - Stopwatch watch = new Stopwatch() - ..start(); - - for (int i = 0; i < _kNumberOfIterations || _kRunForever; ++i) { - renderView.configuration = (i % 2 == 0) ? big : small; - RendererBinding.instance.pipelineOwner.flushLayout(); - } - - watch.stop(); - print("Stock layout: " + watch.elapsed.toString()); - exit(0); -} diff --git a/packages/flutter/pubspec.yaml b/packages/flutter/pubspec.yaml index c634191a7a..1fee320aa4 100644 --- a/packages/flutter/pubspec.yaml +++ b/packages/flutter/pubspec.yaml @@ -22,8 +22,6 @@ dependencies: dev_dependencies: flutter_test: path: ../flutter_test - stocks: # for the benchmarks - path: ../../examples/stocks environment: sdk: '>=1.12.0 <2.0.0' diff --git a/packages/flutter_test/lib/src/binding.dart b/packages/flutter_test/lib/src/binding.dart index 456f7d94dc..5855ee623b 100644 --- a/packages/flutter_test/lib/src/binding.dart +++ b/packages/flutter_test/lib/src/binding.dart @@ -67,7 +67,7 @@ enum TestBindingEventSource { device, } -const Size _kTestViewportSize = const Size(800.0, 600.0); +const Size _kDefaultTestViewportSize = const Size(800.0, 600.0); /// Base class for bindings used by widgets library tests. /// @@ -94,9 +94,9 @@ abstract class TestWidgetsFlutterBinding extends BindingBase static WidgetsBinding ensureInitialized() { if (WidgetsBinding.instance == null) { if (Platform.environment.containsKey('FLUTTER_TEST')) { - new AutomatedTestWidgetsFlutterBinding._(); + new AutomatedTestWidgetsFlutterBinding(); } else { - new LiveTestWidgetsFlutterBinding._(); + new LiveTestWidgetsFlutterBinding(); } } assert(WidgetsBinding.instance is TestWidgetsFlutterBinding); @@ -394,8 +394,6 @@ abstract class TestWidgetsFlutterBinding extends BindingBase /// This binding controls time, allowing tests to verify long /// animation sequences without having to execute them in real time. class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding { - AutomatedTestWidgetsFlutterBinding._(); - @override void initInstances() { debugPrint = debugPrintSynchronously; @@ -542,8 +540,6 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding { /// doesn't trigger a paint, since then you could not see anything /// anyway.) class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding { - LiveTestWidgetsFlutterBinding._(); - @override bool get inTest => _inTest; bool _inTest = false; @@ -676,11 +672,7 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding { @override ViewConfiguration createViewConfiguration() { - return new _TestViewConfiguration( - // TODO(ianh): that these are not the same is https://github.com/flutter/flutter/issues/1360 - _getMatrix(ui.window.devicePixelRatio), - _getMatrix(1.0) - ); + return new TestViewConfiguration(); } @override @@ -697,12 +689,23 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding { Matrix4 transform = renderView.configuration.toHitTestMatrix(); return MatrixUtils.transformPoint(transform, point); } +} - Matrix4 _getMatrix(double devicePixelRatio) { +/// A [ViewConfiguration] that pretends the display is of a particular size. The +/// size is in logical pixels. The resulting ViewConfiguration maps the given +/// size onto the actual display using the [ImageFit.contain] algorithm. +class TestViewConfiguration extends ViewConfiguration { + /// Creates a [TestViewConfiguration] with the given size. Defaults to 800x600. + TestViewConfiguration({ Size size: _kDefaultTestViewportSize }) + : _paintMatrix = _getMatrix(size, ui.window.devicePixelRatio), + _hitTestMatrix = _getMatrix(size, 1.0), + super(size: size); + + static Matrix4 _getMatrix(Size size, double devicePixelRatio) { final double actualWidth = ui.window.size.width * devicePixelRatio; final double actualHeight = ui.window.size.height * devicePixelRatio; - final double desiredWidth = _kTestViewportSize.width; - final double desiredHeight = _kTestViewportSize.height; + final double desiredWidth = size.width; + final double desiredHeight = size.height; double scale, shiftX, shiftY; if ((actualWidth / actualHeight) > (desiredWidth / desiredHeight)) { scale = actualHeight / desiredHeight; @@ -720,18 +723,24 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding { ); return matrix; } -} -class _TestViewConfiguration extends ViewConfiguration { - _TestViewConfiguration(this.paintMatrix, this.hitTestMatrix) : super(size: _kTestViewportSize); - - final Matrix4 paintMatrix; - final Matrix4 hitTestMatrix; + final Matrix4 _paintMatrix; + final Matrix4 _hitTestMatrix; @override - Matrix4 toMatrix() => paintMatrix.clone(); + Matrix4 toMatrix() => _paintMatrix.clone(); - Matrix4 toHitTestMatrix() => hitTestMatrix.clone(); + /// Provides the transformation matrix that converts coordinates in the test + /// coordinate space to coordinates in logical pixels on the real display. + /// + /// This is essenitally the same as [toMatrix] but ignoring the device pixel + /// ratio. + /// + /// This is useful because pointers are described in logical pixels, as + /// opposed to graphics which are expressed in physical pixels. + // TODO(ianh): We should make graphics and pointers use the same coordinate space. + // See: https://github.com/flutter/flutter/issues/1360 + Matrix4 toHitTestMatrix() => _hitTestMatrix.clone(); @override String toString() => 'TestViewConfiguration'; @@ -758,7 +767,9 @@ class _LiveTestRenderView extends RenderView { }) : super(configuration: configuration); @override - _TestViewConfiguration get configuration => super.configuration; + TestViewConfiguration get configuration => super.configuration; + @override + set configuration(TestViewConfiguration value) { super.configuration = value; } final Map _pointers = {}; diff --git a/packages/flutter_test/lib/src/widget_tester.dart b/packages/flutter_test/lib/src/widget_tester.dart index 31dd6f1798..782bd93218 100644 --- a/packages/flutter_test/lib/src/widget_tester.dart +++ b/packages/flutter_test/lib/src/widget_tester.dart @@ -65,14 +65,18 @@ void testWidgets(String description, WidgetTesterCallback callback, { /// The callback can be asynchronous (using `async`/`await` or using /// explicit [Future]s). If it is, then [benchmarkWidgets] will return /// a [Future] that completes when the callback's does. Otherwise, it -/// will return a widget that is always complete. +/// will return a Future that is always complete. +/// +/// If the callback is asynchronous, make sure you `await` the call +/// to [benchmarkWidgets], otherwise it won't run! /// /// Benchmarks must not be run in checked mode. To avoid this, this -/// function will assert if it is run in checked mode. +/// function will print a big message if it is run in checked mode. /// /// Example: /// /// main() async { +/// assert(false); // fail in checked mode /// await benchmarkWidgets((WidgetTester tester) { /// tester.pumpWidget(new MyWidget()); /// final Stopwatch timer = new Stopwatch()..start(); @@ -86,7 +90,20 @@ void testWidgets(String description, WidgetTesterCallback callback, { /// exit(0); /// } Future benchmarkWidgets(WidgetTesterCallback callback) { - assert(false); // Don't run benchmarks in checked mode. + assert(() { + print('┏╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍┓'); + print('┇ ⚠ THIS BENCHMARK IS BEING RUN WITH ASSERTS ENABLED ⚠ ┇'); + print('┡╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍┦'); + print('│ │'); + print('│ Numbers obtained from a benchmark while asserts are │'); + print('│ enabled will not accurately reflect the performance │'); + print('│ that will be experienced by end users using release ╎'); + print('│ builds. Benchmarks should be run using this command ┆'); + print('│ line: flutter run --release -t benchmark.dart ┊'); + print('│ '); + print('└─────────────────────────────────────────────────╌┄┈ 🐢'); + return true; + }); TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized(); assert(binding is! AutomatedTestWidgetsFlutterBinding); WidgetTester tester = new WidgetTester._(binding);