runBenchmark method

Future<BenchmarkResult> runBenchmark({
  1. required Future<void> operation(),
  2. required int iterations,
  3. required String name,
})

Runs a benchmark for the given operation.

The operation parameter is the operation to benchmark. The iterations parameter is the number of times to run the operation. The name parameter is the name of the benchmark.

Returns the benchmark result.

Implementation

Future<BenchmarkResult> runBenchmark({
  required Future<void> Function() operation,
  required int iterations,
  required String name,
}) async {
  _log.info('Starting benchmark: $name');

  final stopwatch = Stopwatch()..start();

  for (int i = 0; i < iterations; i++) {
    await operation();
  }

  stopwatch.stop();

  final result = BenchmarkResult(
    name: name,
    iterations: iterations,
    totalTimeMs: stopwatch.elapsedMilliseconds,
    averageTimeMs: stopwatch.elapsedMilliseconds / iterations,
  );

  _log.info('Benchmark completed: $name');
  _log.info('Result: $result');

  return result;
}