asyncBenchmark function
Future<void>
asyncBenchmark(
- String description,
- Future<
void> run(), { - Future<
void> setup() = futureDoNothing, - Future<
void> teardown() = futureDoNothing, - ScoreEmitter scoreEmitter = const StatsEmitter(),
- Duration warmUpDuration = const Duration(milliseconds: 200),
- SampleSize? sampleSize,
- bool runInIsolate = true,
Runs an asynchronous benchmark.
run: the benchmarked function,setup: executed once before the benchmark,teardown: executed once after the benchmark runs.runInIsolate: Set totrueto run benchmark in a separate isolate.scoreEmitter: A custom score emitter.warmUpDuration: The duration used to create a score estimate.sampleSize: An optional parameter of type SampleSize that is used to specify thelengthof the benchmark score list and theinnerIterations(the number of timerunis averaged over to generate a score entry).
Implementation
Future<void> asyncBenchmark(
String description,
Future<void> Function() run, {
Future<void> Function() setup = futureDoNothing,
Future<void> Function() teardown = futureDoNothing,
ScoreEmitter scoreEmitter = const StatsEmitter(),
final Duration warmUpDuration = const Duration(milliseconds: 200),
SampleSize? sampleSize,
bool runInIsolate = true,
}) async {
final group = Zone.current[#group] as Group?;
final groupDescription = group == null
? ''
: '${group.description.addSeparator(':')} ';
final scoreGenerator = AsyncScoreGenerator(
run: run,
setup: setup,
teardown: teardown,
);
description =
groupDescription +
(hourGlass + description).style(ColorProfile.asyncBenchmark);
final watch = Stopwatch()..start();
await runZonedGuarded(
() async {
try {
if (runInIsolate) {
await Isolate.run(
() async => scoreEmitter.emit(
description: description,
score: await scoreGenerator.score(
warmUpDuration: warmUpDuration,
sampleSize: sampleSize,
),
),
);
} else {
scoreEmitter.emit(
description: description,
score: await scoreGenerator.score(
warmUpDuration: warmUpDuration,
sampleSize: sampleSize,
),
);
}
addSuccessMark();
} catch (error, stack) {
reportError(
error,
stack,
description: description,
duration: watch.elapsed,
errorMark: benchmarkError,
);
}
},
((error, stack) {
// Safequard: Errors should be caught in the try block above.
reportError(
error,
stack,
description: description,
duration: watch.elapsed,
errorMark: benchmarkError,
);
}),
);
}