Last active
February 10, 2022 18:44
-
-
Save PlugFox/5edf313d937151cb7aa780d3d80b5041 to your computer and use it in GitHub Desktop.
Real GetStream benchmark test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CHROME