runComparison function

Future<Comparison> runComparison({
  1. required List<String> columns,
  2. required Map<String, List<Benchmark> Function(int column)> groups,
  3. String? title,
  4. String groupHeader = 'Benchmark',
  5. String rowHeader = 'Variant',
  6. Metric metric = Metric.throughput,
  7. int? baseline = 0,
  8. double? alpha = 0.05,
  9. BenchmarkOptions? options,
  10. bool? verify,
  11. StringSink? out,
  12. TableRenderer? renderer,
  13. void onMeasure(
    1. String group,
    2. Benchmark benchmark,
    3. int column,
    4. Measurement?,
    )?,
})

Measures a comparison table and returns it.

For each entry of groups, in order, the builder is called once per column with the column index and returns that column's benchmarks, one per row. Builders run only when their group is measured, so large inputs can be freed between groups. Every column of a group must return the same number of benchmarks; row names come from the first column. Return a SkippedBenchmark to leave a cell empty.

The benchmarks of one column are measured together with measureAll, in alternating rounds. When out is given, the table is written to it with renderer as it is measured: the header first, then each group as soon as it finishes.

Arguments left null come from BenchmarkSettings.current, which also filters rows and receives the finished table.

Parameters:

  • columns: the column headers.
  • groups: group name to per-column benchmark builder.
  • title: an optional heading.
  • groupHeader, rowHeader: headers of the two label columns.
  • metric: what the cells rank by.
  • baseline: row index others are compared with; null for no ratios.
  • alpha: the significance level for ratios; null to show all ratios.
  • options: timing parameters for every benchmark.
  • verify: check that the rows of each group and column return equal results before measuring them.
  • out: where to stream the rendered table.
  • renderer: the output format for out; Markdown by default.
  • onMeasure: called after each cell, for progress reporting.

Throws:

Implementation

Future<Comparison> runComparison({
  required List<String> columns,
  required Map<String, List<Benchmark> Function(int column)> groups,
  String? title,
  String groupHeader = 'Benchmark',
  String rowHeader = 'Variant',
  Metric metric = Metric.throughput,
  int? baseline = 0,
  double? alpha = 0.05,
  BenchmarkOptions? options,
  bool? verify,
  StringSink? out,
  TableRenderer? renderer,
  void Function(String group, Benchmark benchmark, int column, Measurement?)?
      onMeasure,
}) async {
  final settings = BenchmarkSettings.current;
  final opts = options ?? settings.options ?? BenchmarkOptions.standard;
  final check = verify ?? settings.verify;
  final sink = out ?? settings.out;
  final format = renderer ?? settings.renderer ?? const MarkdownTableRenderer();
  final table = Comparison(
    columns: columns,
    title: title,
    groupHeader: groupHeader,
    rowHeader: rowHeader,
    metric: metric,
    baseline: baseline,
    alpha: alpha,
    environment: currentEnvironment(),
  );
  sink?.write(format.header(table));
  for (final entry in groups.entries) {
    var keep = <int>[];
    var cells = <List<Measurement?>>[];
    var names = <String>[];
    for (var c = 0; c < columns.length; c++) {
      final benchmarks = entry.value(c);
      if (c == 0) {
        names = [for (final b in benchmarks) b.name];
        keep = [
          for (var r = 0; r < benchmarks.length; r++)
            if (settings.includes(title, entry.key, names[r])) r,
        ];
        cells = [
          for (final _ in keep) List<Measurement?>.filled(columns.length, null),
        ];
      } else if (benchmarks.length != names.length) {
        throw ArgumentError(
          'Group "${entry.key}" has ${benchmarks.length} benchmarks in column '
              '$c but ${names.length} in column 0',
          'groups',
        );
      }
      final picked = [for (final r in keep) benchmarks[r]];
      final results = await measureAll(picked, options: opts, verify: check);
      for (var i = 0; i < picked.length; i++) {
        cells[i][c] = results[i];
        onMeasure?.call(entry.key, picked[i], c, results[i]);
      }
    }
    if (keep.isEmpty && names.isNotEmpty) continue;
    final group = ComparisonGroup._(
        entry.key,
        [
          for (var i = 0; i < keep.length; i++)
            ComparisonRow(names[keep[i]], cells[i]),
        ],
        baseline != null && keep.length != names.length
            ? keep.indexOf(baseline)
            : null);
    table.groups.add(group);
    sink?.write(format.group(table, group));
  }
  sink?.write(format.footer(table));
  settings.onComparison?.call(table);
  return table;
}