benchmark static method

Future<BenchmarkResult> benchmark(
  1. String name,
  2. Future<void> fn(), {
  3. int iterations = 10,
  4. 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,
  );
}