Skip to content

Instantly share code, notes, and snippets.

@PlugFox
Last active February 10, 2022 18:44
Show Gist options
  • Save PlugFox/5edf313d937151cb7aa780d3d80b5041 to your computer and use it in GitHub Desktop.
Save PlugFox/5edf313d937151cb7aa780d3d80b5041 to your computer and use it in GitHub Desktop.
Real GetStream benchmark test
/// How to run
/// ```bash
/// flutter test --concurrency=1 --platform tester .\test\real_benchmark.dart
/// flutter test --concurrency=1 --platform chrome .\test\real_benchmark.dart
/// ```
import 'dart:async';
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get/get_rx/src/rx_stream/rx_stream.dart';
const int _kCount = 60000;
void main() {
group(
'Real GetStream benchmark test',
() {
test(
'GetStream performance',
() async {
final benchmark = GetStreamBenchmark();
await benchmark.asyncReport();
},
);
test(
'Notifier performance',
() async {
final benchmark = NotifierBenchmark();
await benchmark.asyncReport();
},
);
},
);
}
class Notifier<T> {
final List<void Function(T)> _listeners = <void Function(T)>[];
void add(T value) {
for (var idx = 0; idx < _listeners.length; idx++) {
_listeners[idx](value);
}
}
void listen(void Function(T) fn) => _listeners.add(fn);
}
class NotifierBenchmark extends AsyncBenchmarkBase with Payload {
NotifierBenchmark() : super('NotifierBenchmark');
// The benchmark code.
@override
Future<void> asyncRun() {
final controller = Notifier<int>();
final completer = Completer<void>();
controller.listen((i) {
_count++;
if (i == _kCount) {
completer.complete();
}
});
for (var i = 1; i <= _kCount; i++) {
controller.add(i);
}
return completer.future;
}
}
class GetStreamBenchmark extends AsyncBenchmarkBase with Payload {
GetStreamBenchmark() : super('GetStreamBenchmark');
// The benchmark code.
@override
Future<void> asyncRun() {
final controller = MiniStream<int>();
final completer = Completer<void>();
controller.listen((i) {
_count++;
if (i == _kCount) {
completer.complete();
}
});
for (var i = 1; i <= _kCount; i++) {
controller.add(i);
}
return completer.future;
}
}
mixin Payload on AsyncBenchmarkBase {
int _count = 0;
@override
void setup() {
_count = 0;
}
@override
void teardown() {
print('$name.count: ${_count ~/ 1000000} kk');
}
}
abstract class AsyncBenchmarkBase = BenchmarkBase with AsyncMeasure;
mixin AsyncMeasure on BenchmarkBase {
// Measures the score for this benchmark by executing it repeatedly until
// time minimum has been reached.
static Future<double> measureFor(
Future<void> Function() fn,
int minimumMillis,
) async {
var minimumMicros = minimumMillis * 1000;
var iter = 0;
var watch = Stopwatch();
watch.start();
var elapsed = 0;
while (elapsed < minimumMicros) {
await fn();
elapsed = watch.elapsedMicroseconds;
iter++;
}
return elapsed / iter;
}
// Measures the score for the benchmark and returns it.
Future<double> asyncMeasure() async {
setup();
// Warmup for at least 100ms. Discard result.
await measureFor(asyncWarmup, 100);
// Run the benchmark for at least 2000ms.
var result = await measureFor(asyncExercise, 2000);
teardown();
return result;
}
// Runs a short version of the benchmark. By default invokes [run] once.
Future<void> asyncWarmup() async {
await asyncRun();
}
// Exercises the benchmark. By default invokes [run] 10 times.
Future<void> asyncExercise() async {
for (var i = 0; i < 10; i++) {
await asyncRun();
}
}
Future<void> asyncRun() => Future<void>(() {});
Future<void> asyncReport() async {
emitter.emit(name, await asyncMeasure());
}
}
@PlugFox
Copy link
Author

PlugFox commented Feb 10, 2022

VM

00:03 +0: Real GetStream benchmark test GetStream performance
GetStreamBenchmark.count: 161 kk
GetStreamBenchmark(RunTime): 7829.88671875 us.
00:05 +1: Real GetStream benchmark test Notifier performance
NotifierBenchmark.count: 347 kk
NotifierBenchmark(RunTime): 3591.3644524236984 us.
00:05 +2: All tests passed!

@PlugFox
Copy link
Author

PlugFox commented Feb 10, 2022

CHROME

GetStreamBenchmark.count: 47 kk
GetStreamBenchmark(RunTime): 26746.666666666668 us.
00:05 +1: Real GetStream benchmark test Notifier performance
NotifierBenchmark.count: 177 kk
NotifierBenchmark(RunTime): 7035.442105263158 us.
00:05 +2: All tests passed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment