|
| 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/foundation.dart'; |
| 6 | + |
| 7 | +import '../common.dart'; |
| 8 | + |
| 9 | +const int _kNumIterations = 10; |
| 10 | +const int _kNumWarmUp = 100; |
| 11 | + |
| 12 | +class Data { |
| 13 | + Data(this.value); |
| 14 | + |
| 15 | + Map<String, dynamic> toJson() => <String, dynamic>{ 'value': value }; |
| 16 | + |
| 17 | + final int value; |
| 18 | +} |
| 19 | + |
| 20 | +List<Data> test(int length) { |
| 21 | + return List<Data>.generate(length, |
| 22 | + (int index) => Data(index * index)); |
| 23 | +} |
| 24 | + |
| 25 | +Future<void> main() async { |
| 26 | + assert(false, "Don't run benchmarks in checked mode! Use 'flutter run --release'."); |
| 27 | + |
| 28 | + // Warm up lap |
| 29 | + for (int i = 0; i < _kNumWarmUp; i += 1) { |
| 30 | + await compute(test, 10); |
| 31 | + } |
| 32 | + |
| 33 | + final Stopwatch watch = Stopwatch(); |
| 34 | + watch.start(); |
| 35 | + for (int i = 0; i < _kNumIterations; i += 1) { |
| 36 | + await compute(test, 1000000); |
| 37 | + } |
| 38 | + final int elapsedMicroseconds = watch.elapsedMicroseconds; |
| 39 | + |
| 40 | + final BenchmarkResultPrinter printer = BenchmarkResultPrinter(); |
| 41 | + const double scale = 1000.0 / _kNumIterations; |
| 42 | + printer.addResult( |
| 43 | + description: 'compute', |
| 44 | + value: elapsedMicroseconds * scale, |
| 45 | + unit: 'ns per iteration', |
| 46 | + name: 'compute_iteration', |
| 47 | + ); |
| 48 | + printer.printToStdout(); |
| 49 | +} |
0 commit comments