main.dart 8.18 KB
Newer Older
1 2 3 4
// 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.

5
import 'dart:async';
6 7 8 9 10 11 12
import 'dart:math' as math;

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

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

40
Future<double> _runBasicStandardSmall(
41 42 43
  BasicMessageChannel<Object?> basicStandard,
  int count,
) async {
44 45 46 47
  final Stopwatch watch = Stopwatch();
  watch.start();
  for (int i = 0; i < count; ++i) {
    await basicStandard.send(1234);
48
  }
49 50 51
  watch.stop();
  return watch.elapsedMicroseconds / count;
}
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
class _Counter {
  int count = 0;
}

void _runBasicStandardParallelRecurse(
  BasicMessageChannel<Object?> basicStandard,
  _Counter counter,
  int count,
  Completer<int> completer,
  Object? payload,
) {
  counter.count += 1;
  if (counter.count == count) {
    completer.complete(counter.count);
  } else if (counter.count < count) {
    basicStandard.send(payload).then((Object? result) {
      _runBasicStandardParallelRecurse(
          basicStandard, counter, count, completer, payload);
    });
  }
}

Future<double> _runBasicStandardParallel(
  BasicMessageChannel<Object?> basicStandard,
  int count,
  Object? payload,
  int parallel,
) async {
  final Stopwatch watch = Stopwatch();
  final Completer<int> completer = Completer<int>();
  final _Counter counter = _Counter();
  watch.start();
  for (int i = 0; i < parallel; ++i) {
    basicStandard.send(payload).then((Object? result) {
      _runBasicStandardParallelRecurse(
          basicStandard, counter, count, completer, payload);
    });
  }
  await completer.future;
  watch.stop();
  return watch.elapsedMicroseconds / count;
}

Future<double> _runBasicStandardLarge(
  BasicMessageChannel<Object?> basicStandard,
  List<Object?> largeBuffer,
  int count,
) async {
101
  int size = 0;
102 103
  final Stopwatch watch = Stopwatch();
  watch.start();
104
  for (int i = 0; i < count; ++i) {
105 106
    final List<Object?>? result =
        await basicStandard.send(largeBuffer) as List<Object?>?;
107 108
    // This check should be tiny compared to the actual channel send/receive.
    size += (result == null) ? 0 : result.length;
109 110 111
  }
  watch.stop();

112 113
  if (size != largeBuffer.length * count) {
    throw Exception(
114
      "There is an error with the echo channel, the results don't add up: $size",
115 116 117 118 119
    );
  }

  return watch.elapsedMicroseconds / count;
}
120

121 122 123 124 125
Future<double> _runBasicBinary(
  BasicMessageChannel<ByteData> basicBinary,
  ByteData buffer,
  int count,
) async {
126
  int size = 0;
127
  final Stopwatch watch = Stopwatch();
128
  watch.start();
129
  for (int i = 0; i < count; ++i) {
130
    final ByteData? result = await basicBinary.send(buffer);
131
    // This check should be tiny compared to the actual channel send/receive.
132
    size += (result == null) ? 0 : result.lengthInBytes;
133 134
  }
  watch.stop();
135 136
  if (size != buffer.lengthInBytes * count) {
    throw Exception(
137
      "There is an error with the echo channel, the results don't add up: $size",
138 139 140 141 142
    );
  }

  return watch.elapsedMicroseconds / count;
}
143

144 145 146 147 148 149 150 151
Future<void> _runTest({
  required Future<double> Function(int) test,
  required BasicMessageChannel<Object?> resetChannel,
  required BenchmarkResultPrinter printer,
  required String description,
  required String name,
  required int numMessages,
}) async {
152
  print('running $name');
153 154 155 156 157 158 159 160 161 162 163
  resetChannel.send(true);
  // Prime test.
  await test(1);
  printer.addResult(
    description: description,
    value: await test(numMessages),
    unit: 'µs',
    name: name,
  );
}

164 165
Future<void> _runTests() async {
  if (kDebugMode) {
166
    throw Exception(
167 168
      "Must be run in profile mode! Use 'flutter run --profile'.",
    );
169 170
  }

171 172
  const BasicMessageChannel<Object?> resetChannel =
      BasicMessageChannel<Object?>(
173 174 175
    'dev.flutter.echo.reset',
    StandardMessageCodec(),
  );
176 177
  const BasicMessageChannel<Object?> basicStandard =
      BasicMessageChannel<Object?>(
178 179 180 181 182 183 184 185 186 187
    '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
188
  /// `Large` tests. Instead make a different test. The size of largeBuffer
189
  /// serialized is 14214 bytes.
190
  final List<Object?> largeBuffer = _makeTestBuffer(1000);
191
  final ByteData largeBufferBytes =
192
      const StandardMessageCodec().encodeMessage(largeBuffer)!;
193 194 195 196
  final ByteData oneMB = ByteData(1024 * 1024);

  const int numMessages = 2500;

197
  final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
198 199 200 201
  await _runTest(
    test: (int x) => _runBasicStandardSmall(basicStandard, x),
    resetChannel: resetChannel,
    printer: printer,
202 203
    description: 'BasicMessageChannel/StandardMessageCodec/Flutter->Host/Small',
    name: 'platform_channel_basic_standard_2host_small',
204
    numMessages: numMessages,
205
  );
206 207 208 209
  await _runTest(
    test: (int x) => _runBasicStandardLarge(basicStandard, largeBuffer, x),
    resetChannel: resetChannel,
    printer: printer,
210 211
    description: 'BasicMessageChannel/StandardMessageCodec/Flutter->Host/Large',
    name: 'platform_channel_basic_standard_2host_large',
212
    numMessages: numMessages,
213
  );
214 215 216 217
  await _runTest(
    test: (int x) => _runBasicBinary(basicBinary, largeBufferBytes, x),
    resetChannel: resetChannel,
    printer: printer,
218 219
    description: 'BasicMessageChannel/BinaryCodec/Flutter->Host/Large',
    name: 'platform_channel_basic_binary_2host_large',
220
    numMessages: numMessages,
221
  );
222 223 224 225
  await _runTest(
    test: (int x) => _runBasicBinary(basicBinary, oneMB, x),
    resetChannel: resetChannel,
    printer: printer,
226 227
    description: 'BasicMessageChannel/BinaryCodec/Flutter->Host/1MB',
    name: 'platform_channel_basic_binary_2host_1MB',
228 229 230 231 232 233 234 235 236 237
    numMessages: numMessages,
  );
  await _runTest(
    test: (int x) => _runBasicStandardParallel(basicStandard, x, 1234, 3),
    resetChannel: resetChannel,
    printer: printer,
    description:
        'BasicMessageChannel/StandardMessageCodec/Flutter->Host/SmallParallel3',
    name: 'platform_channel_basic_standard_2host_small_parallel_3',
    numMessages: numMessages,
238
  );
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  // Background platform channels aren't yet implemented for iOS.
  const BasicMessageChannel<Object?> backgroundStandard =
      BasicMessageChannel<Object?>(
    'dev.flutter.echo.background.standard',
    StandardMessageCodec(),
  );
  await _runTest(
    test: (int x) => _runBasicStandardSmall(backgroundStandard, x),
    resetChannel: resetChannel,
    printer: printer,
    description:
        'BasicMessageChannel/StandardMessageCodec/Flutter->Host (background)/Small',
    name: 'platform_channel_basic_standard_2hostbackground_small',
    numMessages: numMessages,
  );
  await _runTest(
    test: (int x) => _runBasicStandardParallel(backgroundStandard, x, 1234, 3),
    resetChannel: resetChannel,
    printer: printer,
    description:
        'BasicMessageChannel/StandardMessageCodec/Flutter->Host (background)/SmallParallel3',
    name: 'platform_channel_basic_standard_2hostbackground_small_parallel_3',
    numMessages: numMessages,
  );
263 264 265 266
  printer.printToStdout();
}

class _BenchmarkWidget extends StatefulWidget {
267
  const _BenchmarkWidget(this.tests);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

  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));
}