measure method

Future<BenchmarkResult> measure(
  1. Duration measureDuration
)

Measures the benchmark for a given duration and returns the result.

Implementation

Future<BenchmarkResult> measure(Duration measureDuration) async {
final results = [];
List<int> iterationsMicroseconds = [];
for (int i = 0; i < 10; i++) {
  await onSetup();
  await warmup();
  final stopwatch = Stopwatch()..start();
  int iterations = 0;
  while (stopwatch.elapsed < measureDuration) {
    final iterationStopwatch = Stopwatch()..start();
    await onRun();
    iterationStopwatch.stop();
    iterationsMicroseconds.add(iterationStopwatch.elapsedTicks);
    iterations++;
  }
  await onTeardown();
  stopwatch.stop();
  final result = iterations.toDouble();
  results.add(result);
}
final mean = results.reduce((a, b) => a + b) / results.length;
final meanPerSecond = mean / measureDuration.inEffectiveSeconds;
final variance = results
        .map((e) => pow(
            ((e / measureDuration.inEffectiveSeconds) - meanPerSecond), 2))
        .reduce((a, b) => a + b) /
    results.length;
final stdDev = sqrt(variance);
final stdDevPercentage = (stdDev / meanPerSecond) * 100;
final sortedIterations = iterationsMicroseconds..sort();
final p75Time = sortedIterations[(sortedIterations.length * 0.75).toInt()];
final p95Time = sortedIterations[(sortedIterations.length * 0.95).toInt()];
final p99Time = sortedIterations[(sortedIterations.length * 0.99).toInt()];
final p999Time =
    sortedIterations[(sortedIterations.length * 0.999).toInt()];
final minTime = sortedIterations.first;
final maxTime = sortedIterations.last;
final meanTime = iterationsMicroseconds.reduce((a, b) => a + b) /
    iterationsMicroseconds.length;
return BenchmarkResult(
  name,
  group,
  mean,
  meanPerSecond,
  stdDevPercentage,
  stdDev,
  meanTime,
  minTime.toDouble(),
  maxTime.toDouble(),
  p75Time.toDouble(),
  p95Time.toDouble(),
  p99Time.toDouble(),
  p999Time.toDouble(),
);
  }