|
| 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'; |
| 6 | + |
| 7 | +import '../common.dart'; |
| 8 | + |
| 9 | +const int _kNumIterations = 100000; |
| 10 | + |
| 11 | +void main() { |
| 12 | + assert(false, |
| 13 | + "Don't run benchmarks in checked mode! Use 'flutter run --release'."); |
| 14 | + final BenchmarkResultPrinter printer = BenchmarkResultPrinter(); |
| 15 | + |
| 16 | + const StandardMethodCodec codec = StandardMethodCodec(); |
| 17 | + final Stopwatch watch = Stopwatch(); |
| 18 | + const String methodName = 'something'; |
| 19 | + watch.start(); |
| 20 | + for (int i = 0; i < _kNumIterations; i += 1) { |
| 21 | + codec.encodeMethodCall(const MethodCall(methodName)); |
| 22 | + } |
| 23 | + watch.stop(); |
| 24 | + |
| 25 | + printer.addResult( |
| 26 | + description: 'StandardMethodCodec null', |
| 27 | + value: watch.elapsedMicroseconds.toDouble() / _kNumIterations, |
| 28 | + unit: 'us per iteration', |
| 29 | + name: 'StandardMethodCodec_null', |
| 30 | + ); |
| 31 | + |
| 32 | + watch.reset(); |
| 33 | + watch.start(); |
| 34 | + for (int i = 0; i < _kNumIterations; i += 1) { |
| 35 | + codec.encodeMethodCall(const MethodCall(methodName, 12345)); |
| 36 | + } |
| 37 | + watch.stop(); |
| 38 | + |
| 39 | + printer.addResult( |
| 40 | + description: 'StandardMethodCodec int', |
| 41 | + value: watch.elapsedMicroseconds.toDouble() / _kNumIterations, |
| 42 | + unit: 'us per iteration', |
| 43 | + name: 'StandardMethodCodec_int', |
| 44 | + ); |
| 45 | + |
| 46 | + watch.reset(); |
| 47 | + |
| 48 | + watch.start(); |
| 49 | + for (int i = 0; i < _kNumIterations; i += 1) { |
| 50 | + codec.encodeMethodCall( |
| 51 | + const MethodCall(methodName, 'This is a performance test.')); |
| 52 | + } |
| 53 | + watch.stop(); |
| 54 | + |
| 55 | + printer.addResult( |
| 56 | + description: 'StandardMethodCodec string', |
| 57 | + value: watch.elapsedMicroseconds.toDouble() / _kNumIterations, |
| 58 | + unit: 'us per iteration', |
| 59 | + name: 'StandardMethodCodec_string', |
| 60 | + ); |
| 61 | + |
| 62 | + watch.reset(); |
| 63 | + watch.start(); |
| 64 | + for (int i = 0; i < _kNumIterations; i += 1) { |
| 65 | + codec.encodeMethodCall(const MethodCall( |
| 66 | + methodName, <Object>[1234, 'This is a performance test.', 1.25, true])); |
| 67 | + } |
| 68 | + watch.stop(); |
| 69 | + |
| 70 | + printer.addResult( |
| 71 | + description: 'StandardMethodCodec heterogenous list', |
| 72 | + value: watch.elapsedMicroseconds.toDouble() / _kNumIterations, |
| 73 | + unit: 'us per iteration', |
| 74 | + name: 'StandardMethodCodec_heterogenous_list', |
| 75 | + ); |
| 76 | + |
| 77 | + watch.reset(); |
| 78 | + watch.start(); |
| 79 | + for (int i = 0; i < _kNumIterations; i += 1) { |
| 80 | + codec.encodeMethodCall(const MethodCall(methodName, <String, Object>{ |
| 81 | + 'integer': 1234, |
| 82 | + 'string': 'This is a performance test.', |
| 83 | + 'float': 1.25, |
| 84 | + 'boolean': true |
| 85 | + })); |
| 86 | + } |
| 87 | + watch.stop(); |
| 88 | + |
| 89 | + printer.addResult( |
| 90 | + description: 'StandardMethodCodec heterogenous map', |
| 91 | + value: watch.elapsedMicroseconds.toDouble() / _kNumIterations, |
| 92 | + unit: 'us per iteration', |
| 93 | + name: 'StandardMethodCodec_heterogenous_map', |
| 94 | + ); |
| 95 | + |
| 96 | + watch.reset(); |
| 97 | + |
| 98 | + printer.printToStdout(); |
| 99 | +} |
0 commit comments