standard_method_codec_bench.dart 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2014 The Flutter 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/services.dart';

import '../common.dart';

const int _kNumIterations = 100000;

void main() {
  assert(false,
13
      "Don't run benchmarks in debug mode! Use 'flutter run --release'.");
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  final BenchmarkResultPrinter printer = BenchmarkResultPrinter();

  const StandardMethodCodec codec = StandardMethodCodec();
  final Stopwatch watch = Stopwatch();
  const String methodName = 'something';
  watch.start();
  for (int i = 0; i < _kNumIterations; i += 1) {
    codec.encodeMethodCall(const MethodCall(methodName));
  }
  watch.stop();

  printer.addResult(
    description: 'StandardMethodCodec null',
    value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
    unit: 'us per iteration',
    name: 'StandardMethodCodec_null',
  );

  watch.reset();
  watch.start();
  for (int i = 0; i < _kNumIterations; i += 1) {
    codec.encodeMethodCall(const MethodCall(methodName, 12345));
  }
  watch.stop();

  printer.addResult(
    description: 'StandardMethodCodec int',
    value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
    unit: 'us per iteration',
    name: 'StandardMethodCodec_int',
  );

  watch.reset();

  watch.start();
  for (int i = 0; i < _kNumIterations; i += 1) {
    codec.encodeMethodCall(
        const MethodCall(methodName, 'This is a performance test.'));
  }
  watch.stop();

  printer.addResult(
    description: 'StandardMethodCodec string',
    value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
    unit: 'us per iteration',
    name: 'StandardMethodCodec_string',
  );

  watch.reset();
  watch.start();
  for (int i = 0; i < _kNumIterations; i += 1) {
    codec.encodeMethodCall(const MethodCall(
        methodName, <Object>[1234, 'This is a performance test.', 1.25, true]));
  }
  watch.stop();

  printer.addResult(
    description: 'StandardMethodCodec heterogenous list',
    value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
    unit: 'us per iteration',
    name: 'StandardMethodCodec_heterogenous_list',
  );

  watch.reset();
  watch.start();
  for (int i = 0; i < _kNumIterations; i += 1) {
    codec.encodeMethodCall(const MethodCall(methodName, <String, Object>{
      'integer': 1234,
      'string': 'This is a performance test.',
      'float': 1.25,
84
      'boolean': true,
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    }));
  }
  watch.stop();

  printer.addResult(
    description: 'StandardMethodCodec heterogenous map',
    value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
    unit: 'us per iteration',
    name: 'StandardMethodCodec_heterogenous_map',
  );

  watch.reset();

  printer.printToStdout();
}