main.dart 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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 'dart:math' as math;
import 'dart:typed_data';

import 'package:flutter/foundation.dart' show kDebugMode;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:microbenchmarks/common.dart';

15 16
List<Object?> _makeTestBuffer(int size) {
  final List<Object?> answer = <Object?>[];
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
  for (int i = 0; i < size; ++i) {
    switch (i % 9) {
      case 0:
        answer.add(1);
        break;
      case 1:
        answer.add(math.pow(2, 65));
        break;
      case 2:
        answer.add(1234.0);
        break;
      case 3:
        answer.add(null);
        break;
      case 4:
        answer.add(<int>[1234]);
        break;
      case 5:
        answer.add(<String, int>{'hello': 1234});
        break;
      case 6:
        answer.add('this is a test');
        break;
      case 7:
        answer.add(true);
        break;
      case 8:
        answer.add(Uint8List(64));
        break;
    }
  }
  return answer;
}

51
Future<double> _runBasicStandardSmall(
52
    BasicMessageChannel<Object?> basicStandard, int count) async {
53 54 55 56
  final Stopwatch watch = Stopwatch();
  watch.start();
  for (int i = 0; i < count; ++i) {
    await basicStandard.send(1234);
57
  }
58 59 60
  watch.stop();
  return watch.elapsedMicroseconds / count;
}
61

62 63
Future<double> _runBasicStandardLarge(BasicMessageChannel<Object?> basicStandard,
    List<Object?> largeBuffer, int count) async {
64
  int size = 0;
65 66
  final Stopwatch watch = Stopwatch();
  watch.start();
67
  for (int i = 0; i < count; ++i) {
68 69
    final List<Object?>? result =
        await basicStandard.send(largeBuffer) as List<Object?>?;
70 71
    // This check should be tiny compared to the actual channel send/receive.
    size += (result == null) ? 0 : result.length;
72 73 74
  }
  watch.stop();

75 76
  if (size != largeBuffer.length * count) {
    throw Exception(
77
      "There is an error with the echo channel, the results don't add up: $size",
78 79 80 81 82
    );
  }

  return watch.elapsedMicroseconds / count;
}
83

84 85
Future<double> _runBasicBinary(BasicMessageChannel<ByteData> basicBinary,
    ByteData buffer, int count) async {
86
  int size = 0;
87
  final Stopwatch watch = Stopwatch();
88
  watch.start();
89
  for (int i = 0; i < count; ++i) {
90
    final ByteData? result = await basicBinary.send(buffer);
91
    // This check should be tiny compared to the actual channel send/receive.
92
    size += (result == null) ? 0 : result.lengthInBytes;
93 94
  }
  watch.stop();
95 96
  if (size != buffer.lengthInBytes * count) {
    throw Exception(
97
      "There is an error with the echo channel, the results don't add up: $size",
98 99 100 101 102
    );
  }

  return watch.elapsedMicroseconds / count;
}
103

104 105
Future<void> _runTests() async {
  if (kDebugMode) {
106
    throw Exception(
107 108
      "Must be run in profile mode! Use 'flutter run --profile'.",
    );
109 110
  }

111
  const BasicMessageChannel<Object?> resetChannel = BasicMessageChannel<Object?>(
112 113 114
    'dev.flutter.echo.reset',
    StandardMessageCodec(),
  );
115
  const BasicMessageChannel<Object?> basicStandard = BasicMessageChannel<Object?>(
116 117 118 119 120 121 122 123 124 125 126 127
    'dev.flutter.echo.basic.standard',
    StandardMessageCodec(),
  );
  const BasicMessageChannel<ByteData> basicBinary =
      BasicMessageChannel<ByteData>(
    'dev.flutter.echo.basic.binary',
    BinaryCodec(),
  );

  /// WARNING: Don't change the following line of code, it will invalidate
  /// `Large` tests.  Instead make a different test.  The size of largeBuffer
  /// serialized is 14214 bytes.
128
  final List<Object?> largeBuffer = _makeTestBuffer(1000);
129
  final ByteData largeBufferBytes =
130
      const StandardMessageCodec().encodeMessage(largeBuffer)!;
131 132 133 134
  final ByteData oneMB = ByteData(1024 * 1024);

  const int numMessages = 2500;

135
  final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
136
  resetChannel.send(true);
137
  await _runBasicStandardSmall(basicStandard, 1); // Warmup.
138 139
  printer.addResult(
    description: 'BasicMessageChannel/StandardMessageCodec/Flutter->Host/Small',
140
    value: await _runBasicStandardSmall(basicStandard, numMessages),
141 142 143
    unit: 'µs',
    name: 'platform_channel_basic_standard_2host_small',
  );
144
  resetChannel.send(true);
145
  await _runBasicStandardLarge(basicStandard, largeBuffer, 1); // Warmup.
146 147
  printer.addResult(
    description: 'BasicMessageChannel/StandardMessageCodec/Flutter->Host/Large',
148 149
    value:
        await _runBasicStandardLarge(basicStandard, largeBuffer, numMessages),
150 151 152
    unit: 'µs',
    name: 'platform_channel_basic_standard_2host_large',
  );
153
  resetChannel.send(true);
154 155 156 157 158 159 160
  await _runBasicBinary(basicBinary, largeBufferBytes, 1); // Warmup.
  printer.addResult(
    description: 'BasicMessageChannel/BinaryCodec/Flutter->Host/Large',
    value: await _runBasicBinary(basicBinary, largeBufferBytes, numMessages),
    unit: 'µs',
    name: 'platform_channel_basic_binary_2host_large',
  );
161
  resetChannel.send(true);
162 163 164 165 166 167 168
  await _runBasicBinary(basicBinary, oneMB, 1); // Warmup.
  printer.addResult(
    description: 'BasicMessageChannel/BinaryCodec/Flutter->Host/1MB',
    value: await _runBasicBinary(basicBinary, oneMB, numMessages),
    unit: 'µs',
    name: 'platform_channel_basic_binary_2host_1MB',
  );
169 170 171 172
  printer.printToStdout();
}

class _BenchmarkWidget extends StatefulWidget {
173
  const _BenchmarkWidget(this.tests, {Key? key}) : super(key: key);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

  final Future<void> Function() tests;

  @override
  _BenchmarkWidgetState createState() => _BenchmarkWidgetState();
}

class _BenchmarkWidgetState extends State<_BenchmarkWidget> {
  @override
  void initState() {
    widget.tests();
    super.initState();
  }

  @override
  Widget build(BuildContext context) => Container();
}

void main() {
  runApp(const _BenchmarkWidget(_runTests));
}