|
| 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 StandardMessageCodec codec = StandardMessageCodec(); |
| 17 | + final Stopwatch watch = Stopwatch(); |
| 18 | + watch.start(); |
| 19 | + for (int i = 0; i < _kNumIterations; i += 1) { |
| 20 | + codec.encodeMessage(null); |
| 21 | + } |
| 22 | + watch.stop(); |
| 23 | + |
| 24 | + printer.addResult( |
| 25 | + description: 'StandardMessageCodec null', |
| 26 | + value: watch.elapsedMicroseconds.toDouble() / _kNumIterations, |
| 27 | + unit: 'us per iteration', |
| 28 | + name: 'StandardMessageCodec_null', |
| 29 | + ); |
| 30 | + |
| 31 | + watch.reset(); |
| 32 | + watch.start(); |
| 33 | + for (int i = 0; i < _kNumIterations; i += 1) { |
| 34 | + codec.encodeMessage(12345); |
| 35 | + } |
| 36 | + watch.stop(); |
| 37 | + |
| 38 | + printer.addResult( |
| 39 | + description: 'StandardMessageCodec int', |
| 40 | + value: watch.elapsedMicroseconds.toDouble() / _kNumIterations, |
| 41 | + unit: 'us per iteration', |
| 42 | + name: 'StandardMessageCodec_int', |
| 43 | + ); |
| 44 | + |
| 45 | + watch.reset(); |
| 46 | + |
| 47 | + watch.start(); |
| 48 | + for (int i = 0; i < _kNumIterations; i += 1) { |
| 49 | + codec.encodeMessage('This is a performance test.'); |
| 50 | + } |
| 51 | + watch.stop(); |
| 52 | + |
| 53 | + printer.addResult( |
| 54 | + description: 'StandardMessageCodec string', |
| 55 | + value: watch.elapsedMicroseconds.toDouble() / _kNumIterations, |
| 56 | + unit: 'us per iteration', |
| 57 | + name: 'StandardMessageCodec_string', |
| 58 | + ); |
| 59 | + |
| 60 | + watch.reset(); |
| 61 | + watch.start(); |
| 62 | + for (int i = 0; i < _kNumIterations; i += 1) { |
| 63 | + codec.encodeMessage(<Object>[1234, 'This is a performance test.', 1.25, true]); |
| 64 | + } |
| 65 | + watch.stop(); |
| 66 | + |
| 67 | + printer.addResult( |
| 68 | + description: 'StandardMessageCodec heterogenous list', |
| 69 | + value: watch.elapsedMicroseconds.toDouble() / _kNumIterations, |
| 70 | + unit: 'us per iteration', |
| 71 | + name: 'StandardMessageCodec_heterogenous_list', |
| 72 | + ); |
| 73 | + |
| 74 | + watch.reset(); |
| 75 | + watch.start(); |
| 76 | + for (int i = 0; i < _kNumIterations; i += 1) { |
| 77 | + codec.encodeMessage(<String, Object>{ |
| 78 | + 'integer': 1234, |
| 79 | + 'string': 'This is a performance test.', |
| 80 | + 'float': 1.25, |
| 81 | + 'boolean': true |
| 82 | + }); |
| 83 | + } |
| 84 | + watch.stop(); |
| 85 | + |
| 86 | + printer.addResult( |
| 87 | + description: 'StandardMessageCodec heterogenous map', |
| 88 | + value: watch.elapsedMicroseconds.toDouble() / _kNumIterations, |
| 89 | + unit: 'us per iteration', |
| 90 | + name: 'StandardMessageCodec_heterogenous_map', |
| 91 | + ); |
| 92 | + |
| 93 | + watch.reset(); |
| 94 | + |
| 95 | + printer.printToStdout(); |
| 96 | +} |
0 commit comments