|
| 1 | +// Copyright 2014 The Flutter 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 'package:flutter/services.dart' show PlatformAssetBundle; |
| 6 | +import 'package:flutter/widgets.dart'; |
| 7 | + |
| 8 | +import '../common.dart'; |
| 9 | + |
| 10 | +const int _kBatchSize = 100; |
| 11 | +const int _kNumIterations = 100; |
| 12 | + |
| 13 | +void main() async { |
| 14 | + assert(false, |
| 15 | + "Don't run benchmarks in debug mode! Use 'flutter run --release'."); |
| 16 | + final BenchmarkResultPrinter printer = BenchmarkResultPrinter(); |
| 17 | + WidgetsFlutterBinding.ensureInitialized(); |
| 18 | + final Stopwatch watch = Stopwatch(); |
| 19 | + final PlatformAssetBundle bundle = PlatformAssetBundle(); |
| 20 | + |
| 21 | + final List<double> values = <double>[]; |
| 22 | + for (int j = 0; j < _kNumIterations; ++j) { |
| 23 | + double tally = 0; |
| 24 | + watch.reset(); |
| 25 | + watch.start(); |
| 26 | + for (int i = 0; i < _kBatchSize; i += 1) { |
| 27 | + // Note: We don't load images like this. PlatformAssetBundle is used for |
| 28 | + // other assets (like Rive animations). We are using an image because it's |
| 29 | + // conveniently sized and available for the test. |
| 30 | + tally += (await bundle.load('packages/flutter_gallery_assets/places/india_pondicherry_salt_farm.png')).lengthInBytes; |
| 31 | + } |
| 32 | + watch.stop(); |
| 33 | + values.add(watch.elapsedMicroseconds.toDouble() / _kBatchSize); |
| 34 | + if (tally < 0.0) { |
| 35 | + print("This shouldn't happen."); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + printer.addResultStatistics( |
| 40 | + description: 'PlatformAssetBundle.load 1MB', |
| 41 | + values: values, |
| 42 | + unit: 'us per iteration', |
| 43 | + name: 'PlatformAssetBundle_load_1MB', |
| 44 | + ); |
| 45 | + |
| 46 | + printer.printToStdout(); |
| 47 | +} |
0 commit comments