benchmark static method
Future<BenchmarkResult>
benchmark(
- String name,
- Future<
void> fn(), { - int iterations = 10,
- required BenchmarkCategory category,
Run multiple iterations and measure avg/min/max
Implementation
static Future<BenchmarkResult> benchmark(
String name,
Future<void> Function() fn, {
int iterations = 10,
required BenchmarkCategory category,
}) async {
final times = <double>[];
// Warmup (exclude first run)
await fn();
for (var i = 0; i < iterations; i++) {
final ms = await measureMs(fn);
times.add(ms);
}
times.sort();
final avg = times.reduce((a, b) => a + b) / times.length;
return BenchmarkResult(
name: name,
avgMs: avg,
minMs: times.first,
maxMs: times.last,
iterations: iterations,
category: category,
);
}