From ade6e1f96dc9351d2f9fdcaef256d9fc221cb66a Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Mon, 24 May 2021 10:45:49 -0700 Subject: [PATCH] Made the android platform channel benchmarks comparable to iOS (#83110) --- .../MainActivity.kt | 17 +++++++++++++---- .../ios/Runner/AppDelegate.swift | 5 +++++ .../platform_channels_benchmarks/lib/main.dart | 8 ++++++++ 3 files changed, 26 insertions(+), 4 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 7305e07c4f..4049f21a32 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 @@ -12,15 +12,24 @@ import io.flutter.plugin.common.StandardMessageCodec import java.nio.ByteBuffer class MainActivity: FlutterActivity() { - + // We allow for the caching of a response in the binary channel case since + // the reply requires a direct buffer, but the input is not a direct buffer. + // We can't directly send the input back to the reply currently. + private var byteBufferCache : ByteBuffer? = null override fun configureFlutterEngine(flutterEngine: FlutterEngine) { + val reset = BasicMessageChannel(flutterEngine.dartExecutor, "dev.flutter.echo.reset", StandardMessageCodec.INSTANCE) + reset.setMessageHandler { message, reply -> run { + byteBufferCache = null + } } 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) + if (byteBufferCache == null) { + byteBufferCache = ByteBuffer.allocateDirect(message!!.capacity()) + byteBufferCache!!.put(message) + } + reply.reply(byteBufferCache) } } 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 851d73133e..0e3b462228 100644 --- a/dev/benchmarks/platform_channels_benchmarks/ios/Runner/AppDelegate.swift +++ b/dev/benchmarks/platform_channels_benchmarks/ios/Runner/AppDelegate.swift @@ -14,6 +14,11 @@ import UIKit GeneratedPluginRegistrant.register(with: self) let registrar = self.registrar(forPlugin: "Echo")! + let reset = FlutterBasicMessageChannel( + name: "dev.flutter.echo.reset", binaryMessenger: registrar.messenger()) + reset.setMessageHandler { (input, reply) in + // noop + } let basicStandard = FlutterBasicMessageChannel( name: "dev.flutter.echo.basic.standard", binaryMessenger: registrar.messenger(), codec: FlutterStandardMessageCodec.sharedInstance()) diff --git a/dev/benchmarks/platform_channels_benchmarks/lib/main.dart b/dev/benchmarks/platform_channels_benchmarks/lib/main.dart index a10675c1ca..530e53ab32 100644 --- a/dev/benchmarks/platform_channels_benchmarks/lib/main.dart +++ b/dev/benchmarks/platform_channels_benchmarks/lib/main.dart @@ -108,6 +108,10 @@ Future _runTests() async { ); } + const BasicMessageChannel resetChannel = BasicMessageChannel( + 'dev.flutter.echo.reset', + StandardMessageCodec(), + ); const BasicMessageChannel basicStandard = BasicMessageChannel( 'dev.flutter.echo.basic.standard', StandardMessageCodec(), @@ -129,6 +133,7 @@ Future _runTests() async { const int numMessages = 2500; final BenchmarkResultPrinter printer = BenchmarkResultPrinter(); + resetChannel.send(true); await _runBasicStandardSmall(basicStandard, 1); // Warmup. printer.addResult( description: 'BasicMessageChannel/StandardMessageCodec/Flutter->Host/Small', @@ -136,6 +141,7 @@ Future _runTests() async { unit: 'µs', name: 'platform_channel_basic_standard_2host_small', ); + resetChannel.send(true); await _runBasicStandardLarge(basicStandard, largeBuffer, 1); // Warmup. printer.addResult( description: 'BasicMessageChannel/StandardMessageCodec/Flutter->Host/Large', @@ -144,6 +150,7 @@ Future _runTests() async { unit: 'µs', name: 'platform_channel_basic_standard_2host_large', ); + resetChannel.send(true); await _runBasicBinary(basicBinary, largeBufferBytes, 1); // Warmup. printer.addResult( description: 'BasicMessageChannel/BinaryCodec/Flutter->Host/Large', @@ -151,6 +158,7 @@ Future _runTests() async { unit: 'µs', name: 'platform_channel_basic_binary_2host_large', ); + resetChannel.send(true); await _runBasicBinary(basicBinary, oneMB, 1); // Warmup. printer.addResult( description: 'BasicMessageChannel/BinaryCodec/Flutter->Host/1MB',