Unverified Commit 8694e19a authored by gaaclarke's avatar gaaclarke Committed by GitHub

Added binary platform channels test (#81874)

parent 4b65c058
...@@ -7,13 +7,21 @@ package com.example.platform_channels_benchmarks ...@@ -7,13 +7,21 @@ package com.example.platform_channels_benchmarks
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.BasicMessageChannel import io.flutter.plugin.common.BasicMessageChannel
import io.flutter.plugin.common.BinaryCodec
import io.flutter.plugin.common.StandardMessageCodec import io.flutter.plugin.common.StandardMessageCodec
import java.nio.ByteBuffer
class MainActivity: FlutterActivity() { class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) { override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
val basicStandard = BasicMessageChannel(flutterEngine.dartExecutor, "dev.flutter.echo.basic.standard", StandardMessageCodec.INSTANCE) val basicStandard = BasicMessageChannel(flutterEngine.dartExecutor, "dev.flutter.echo.basic.standard", StandardMessageCodec.INSTANCE)
basicStandard.setMessageHandler { message, reply -> reply.reply(message) } 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) super.configureFlutterEngine(flutterEngine)
} }
} }
...@@ -20,6 +20,12 @@ import UIKit ...@@ -20,6 +20,12 @@ import UIKit
basicStandard.setMessageHandler { (input, reply) in basicStandard.setMessageHandler { (input, reply) in
reply(input) 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) return super.application(application, didFinishLaunchingWithOptions: launchOptions)
} }
......
...@@ -48,60 +48,116 @@ List<Object> _makeTestBuffer(int size) { ...@@ -48,60 +48,116 @@ List<Object> _makeTestBuffer(int size) {
return answer; return answer;
} }
Future<void> _runTests() async { Future<double> _runBasicStandardSmall(
if (kDebugMode) { BasicMessageChannel<Object> basicStandard, int count) async {
throw Exception( final Stopwatch watch = Stopwatch();
"Must be run in profile mode! Use 'flutter run --profile'."); watch.start();
for (int i = 0; i < count; ++i) {
await basicStandard.send(1234);
} }
const int numMessages = 2500; watch.stop();
return watch.elapsedMicroseconds / count;
}
const BasicMessageChannel<Object> channel = BasicMessageChannel<Object>( Future<double> _runBasicStandardLarge(BasicMessageChannel<Object> basicStandard,
'dev.flutter.echo.basic.standard', List<Object> largeBuffer, int count) async {
StandardMessageCodec(), int size = 0;
);
final Stopwatch watch = Stopwatch(); final Stopwatch watch = Stopwatch();
watch.start(); watch.start();
for (int i = 0; i < numMessages; ++i) { for (int i = 0; i < count; ++i) {
await channel.send(1234); final List<Object> result =
await basicStandard.send(largeBuffer) as List<Object>;
// This check should be tiny compared to the actual channel send/receive.
size += (result == null) ? 0 : result.length;
} }
watch.stop(); watch.stop();
final double smallPayloadTime = watch.elapsedMicroseconds / numMessages;
watch.reset(); if (size != largeBuffer.length * count) {
/// WARNING: Don't change the following line of code, it will invalidate throw Exception(
/// `Large` tests. Instead make a different test. The size of largeBuffer 'There is an error with the echo channel, the results don\'t add up: $size',
/// serialized is 14214 bytes. );
final List<Object> largeBuffer = _makeTestBuffer(1000); }
return watch.elapsedMicroseconds / count;
}
Future<double> _runBasicBinary(BasicMessageChannel<ByteData> basicBinary,
ByteData buffer, int count) async {
int size = 0; int size = 0;
final Stopwatch watch = Stopwatch();
watch.start(); watch.start();
for (int i = 0; i < numMessages; ++i) { for (int i = 0; i < count; ++i) {
final List<Object> result = await channel.send(largeBuffer) as List<Object>; final ByteData result = await basicBinary.send(buffer);
// This check should be tiny compared to the actual channel send/receive. // This check should be tiny compared to the actual channel send/receive.
size += (result == null) ? 0 : result.length; size += (result == null) ? 0 : result.lengthInBytes;
} }
watch.stop(); watch.stop();
final double largePayloadTime = watch.elapsedMicroseconds / numMessages; 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;
}
if (size != largeBuffer.length * numMessages) { Future<void> _runTests() async {
if (kDebugMode) {
throw Exception( throw Exception(
'There is an error with the echo channel, the results don\'t add up: $size'); "Must be run in profile mode! Use 'flutter run --profile'.",
);
} }
const BasicMessageChannel<Object> basicStandard = BasicMessageChannel<Object>(
'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.
final List<Object> largeBuffer = _makeTestBuffer(1000);
final ByteData largeBufferBytes =
const StandardMessageCodec().encodeMessage(largeBuffer);
final ByteData oneMB = ByteData(1024 * 1024);
const int numMessages = 2500;
final BenchmarkResultPrinter printer = BenchmarkResultPrinter(); final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
await _runBasicStandardSmall(basicStandard, 1); // Warmup.
printer.addResult( printer.addResult(
description: 'BasicMessageChannel/StandardMessageCodec/Flutter->Host/Small', description: 'BasicMessageChannel/StandardMessageCodec/Flutter->Host/Small',
value: smallPayloadTime, value: await _runBasicStandardSmall(basicStandard, numMessages),
unit: 'µs', unit: 'µs',
name: 'platform_channel_basic_standard_2host_small', name: 'platform_channel_basic_standard_2host_small',
); );
await _runBasicStandardLarge(basicStandard, largeBuffer, 1); // Warmup.
printer.addResult( printer.addResult(
description: 'BasicMessageChannel/StandardMessageCodec/Flutter->Host/Large', description: 'BasicMessageChannel/StandardMessageCodec/Flutter->Host/Large',
value: largePayloadTime, value:
await _runBasicStandardLarge(basicStandard, largeBuffer, numMessages),
unit: 'µs', unit: 'µs',
name: 'platform_channel_basic_standard_2host_large', 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(); printer.printToStdout();
} }
......
...@@ -373,7 +373,7 @@ ...@@ -373,7 +373,7 @@
"flaky": false "flaky": false
}, },
{ {
"name": "Linux platform channels benchmarks", "name": "Linux platform_channels_benchmarks",
"repo": "flutter", "repo": "flutter",
"task_name": "linux_platform_channels_benchmarks", "task_name": "linux_platform_channels_benchmarks",
"flaky": true "flaky": true
...@@ -1159,7 +1159,7 @@ ...@@ -1159,7 +1159,7 @@
"flaky": false "flaky": false
}, },
{ {
"name": "Mac_ios platform channels benchmarks", "name": "Mac_ios platform_channels_benchmarks",
"repo": "flutter", "repo": "flutter",
"task_name": "mac_ios_platform_channels_benchmarks", "task_name": "mac_ios_platform_channels_benchmarks",
"flaky": true "flaky": true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment