computeAverage function

BenchmarkResults computeAverage(
  1. List<BenchmarkResults> results
)

Returns the average of the benchmark results in results.

Each element in results is expected to have identical benchmark names and metrics; otherwise, an Exception will be thrown.

Implementation

BenchmarkResults computeAverage(List<BenchmarkResults> results) {
  if (results.isEmpty) {
    throw ArgumentError('Cannot take average of empty list.');
  }

  final BenchmarkResults totalSum = results.reduce(
    (BenchmarkResults sum, BenchmarkResults next) => sum._sumWith(next),
  );

  final BenchmarkResults average = totalSum;
  for (final String benchmark in totalSum.scores.keys) {
    final List<BenchmarkScore> scoresForBenchmark = totalSum.scores[benchmark]!;
    for (int i = 0; i < scoresForBenchmark.length; i++) {
      final BenchmarkScore score = scoresForBenchmark[i];
      final double averageValue = score.value / results.length;
      average.scores[benchmark]![i] =
          BenchmarkScore(metric: score.metric, value: averageValue);
    }
  }
  return average;
}