From 8694e19ac757b91c84c44e025937963b7fd59556 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Fri, 7 May 2021 14:21:00 -0700 Subject: [PATCH] Added binary platform channels test (#81874) --- .../MainActivity.kt | 8 ++ .../ios/Runner/AppDelegate.swift | 6 + .../lib/main.dart | 112 +++++++++++++----- dev/prod_builders.json | 4 +- 4 files changed, 100 insertions(+), 30 deletions(-) diff --git a/dev/benchmarks/platform_channels_benchmarks/android/app/src/main/kotlin/com/example/platform_channels_benchmarks/MainActivity.kt b/dev/benchmarks/platform_channels_benchmarks/android/app/src/main/kotlin/com/example/platform_channels_benchmarks/MainActivity.kt index c75a6da863..7305e07c4f 100644 --- a/dev/benchmarks/platform_channels_benchmarks/android/app/src/main/kotlin/com/example/platform_channels_benchmarks/MainActivity.kt +++ b/dev/benchmarks/platform_channels_benchmarks/android/app/src/main/kotlin/com/example/platform_channels_benchmarks/MainActivity.kt @@ -7,13 +7,21 @@ package com.example.platform_channels_benchmarks import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.engine.FlutterEngine import io.flutter.plugin.common.BasicMessageChannel +import io.flutter.plugin.common.BinaryCodec import io.flutter.plugin.common.StandardMessageCodec +import java.nio.ByteBuffer class MainActivity: FlutterActivity() { override fun configureFlutterEngine(flutterEngine: FlutterEngine) { val basicStandard = BasicMessageChannel(flutterEngine.dartExecutor, "dev.flutter.echo.basic.standard", StandardMessageCodec.INSTANCE) basicStandard.setMessageHandler { message, reply -> reply.reply(message) } + val basicBinary = BasicMessageChannel(flutterEngine.dartExecutor, "dev.flutter.echo.basic.binary", BinaryCodec.INSTANCE) + basicBinary.setMessageHandler { message, reply -> run { + val result = ByteBuffer.allocateDirect(message!!.capacity()) + result.put(message) + reply.reply(result) + } } super.configureFlutterEngine(flutterEngine) } } diff --git a/dev/benchmarks/platform_channels_benchmarks/ios/Runner/AppDelegate.swift b/dev/benchmarks/platform_channels_benchmarks/ios/Runner/AppDelegate.swift index 8cb092ebd4..851d73133e 100644 --- a/dev/benchmarks/platform_channels_benchmarks/ios/Runner/AppDelegate.swift +++ b/dev/benchmarks/platform_channels_benchmarks/ios/Runner/AppDelegate.swift @@ -20,6 +20,12 @@ import UIKit basicStandard.setMessageHandler { (input, reply) in reply(input) } + let basicBinary = FlutterBasicMessageChannel( + name: "dev.flutter.echo.basic.binary", binaryMessenger: registrar.messenger(), + codec: FlutterBinaryCodec()) + basicBinary.setMessageHandler { (input, reply) in + reply(input) + } return super.application(application, didFinishLaunchingWithOptions: launchOptions) } diff --git a/dev/benchmarks/platform_channels_benchmarks/lib/main.dart b/dev/benchmarks/platform_channels_benchmarks/lib/main.dart index 2ea21d057d..716955be5e 100644 --- a/dev/benchmarks/platform_channels_benchmarks/lib/main.dart +++ b/dev/benchmarks/platform_channels_benchmarks/lib/main.dart @@ -48,60 +48,116 @@ List _makeTestBuffer(int size) { return answer; } -Future _runTests() async { - if (kDebugMode) { - throw Exception( - "Must be run in profile mode! Use 'flutter run --profile'."); - } - const int numMessages = 2500; - - const BasicMessageChannel channel = BasicMessageChannel( - 'dev.flutter.echo.basic.standard', - StandardMessageCodec(), - ); +Future _runBasicStandardSmall( + BasicMessageChannel basicStandard, int count) async { final Stopwatch watch = Stopwatch(); - watch.start(); - for (int i = 0; i < numMessages; ++i) { - await channel.send(1234); + for (int i = 0; i < count; ++i) { + await basicStandard.send(1234); } watch.stop(); - final double smallPayloadTime = watch.elapsedMicroseconds / numMessages; - - watch.reset(); - /// 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. - final List largeBuffer = _makeTestBuffer(1000); + return watch.elapsedMicroseconds / count; +} +Future _runBasicStandardLarge(BasicMessageChannel basicStandard, + List largeBuffer, int count) async { int size = 0; + final Stopwatch watch = Stopwatch(); watch.start(); - for (int i = 0; i < numMessages; ++i) { - final List result = await channel.send(largeBuffer) as List; + for (int i = 0; i < count; ++i) { + final List result = + await basicStandard.send(largeBuffer) as List; // This check should be tiny compared to the actual channel send/receive. size += (result == null) ? 0 : result.length; } watch.stop(); - final double largePayloadTime = watch.elapsedMicroseconds / numMessages; - if (size != largeBuffer.length * numMessages) { + if (size != largeBuffer.length * count) { throw Exception( - 'There is an error with the echo channel, the results don\'t add up: $size'); + 'There is an error with the echo channel, the results don\'t add up: $size', + ); } + return watch.elapsedMicroseconds / count; +} + +Future _runBasicBinary(BasicMessageChannel basicBinary, + ByteData buffer, int count) async { + int size = 0; + final Stopwatch watch = Stopwatch(); + watch.start(); + for (int i = 0; i < count; ++i) { + final ByteData result = await basicBinary.send(buffer); + // This check should be tiny compared to the actual channel send/receive. + size += (result == null) ? 0 : result.lengthInBytes; + } + watch.stop(); + if (size != buffer.lengthInBytes * count) { + throw Exception( + 'There is an error with the echo channel, the results don\'t add up: $size', + ); + } + + return watch.elapsedMicroseconds / count; +} + +Future _runTests() async { + if (kDebugMode) { + throw Exception( + "Must be run in profile mode! Use 'flutter run --profile'.", + ); + } + + const BasicMessageChannel basicStandard = BasicMessageChannel( + 'dev.flutter.echo.basic.standard', + StandardMessageCodec(), + ); + const BasicMessageChannel basicBinary = + BasicMessageChannel( + '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. + final List largeBuffer = _makeTestBuffer(1000); + final ByteData largeBufferBytes = + const StandardMessageCodec().encodeMessage(largeBuffer); + final ByteData oneMB = ByteData(1024 * 1024); + + const int numMessages = 2500; + final BenchmarkResultPrinter printer = BenchmarkResultPrinter(); + await _runBasicStandardSmall(basicStandard, 1); // Warmup. printer.addResult( description: 'BasicMessageChannel/StandardMessageCodec/Flutter->Host/Small', - value: smallPayloadTime, + value: await _runBasicStandardSmall(basicStandard, numMessages), unit: 'µs', name: 'platform_channel_basic_standard_2host_small', ); + await _runBasicStandardLarge(basicStandard, largeBuffer, 1); // Warmup. printer.addResult( description: 'BasicMessageChannel/StandardMessageCodec/Flutter->Host/Large', - value: largePayloadTime, + value: + await _runBasicStandardLarge(basicStandard, largeBuffer, numMessages), unit: 'µs', name: 'platform_channel_basic_standard_2host_large', ); + 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', + ); + 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', + ); printer.printToStdout(); } diff --git a/dev/prod_builders.json b/dev/prod_builders.json index 11fb8b214e..d06818050b 100644 --- a/dev/prod_builders.json +++ b/dev/prod_builders.json @@ -373,7 +373,7 @@ "flaky": false }, { - "name": "Linux platform channels benchmarks", + "name": "Linux platform_channels_benchmarks", "repo": "flutter", "task_name": "linux_platform_channels_benchmarks", "flaky": true @@ -1159,7 +1159,7 @@ "flaky": false }, { - "name": "Mac_ios platform channels benchmarks", + "name": "Mac_ios platform_channels_benchmarks", "repo": "flutter", "task_name": "mac_ios_platform_channels_benchmarks", "flaky": true