measureAll function

Future<List<Measurement?>> measureAll(
  1. List<Benchmark> benchmarks, {
  2. BenchmarkOptions options = BenchmarkOptions.standard,
  3. bool verify = false,
  4. bool equals(
    1. Object? a,
    2. Object? b
    ) = deepEquals,
})

Measures benchmarks together and returns their results in order, with null for each SkippedBenchmark.

Every benchmark is set up and run once first (and checked, if verify is set), then warmed up, then measured in options.rounds rounds visited in A B C, C B A, ... order, so slow drift such as CPU heating affects all of them alike. A Benchmark that only overrides Benchmark.measure is measured afterwards in one go.

Parameters:

  • benchmarks: the benchmarks to measure.
  • options: timing parameters for each benchmark.
  • verify: check, before measuring, that every benchmark's first result equals the first benchmark's, using equals.
  • equals: the equality for verify; deepEquals by default.

Throws:

Implementation

Future<List<Measurement?>> measureAll(
  List<Benchmark> benchmarks, {
  BenchmarkOptions options = BenchmarkOptions.standard,
  bool verify = false,
  bool Function(Object? a, Object? b) equals = deepEquals,
}) async {
  final sessions = [
    for (final b in benchmarks)
      switch (b) {
        SyncBenchmark() ||
        AsyncBenchmark() ||
        ClosureBenchmark() =>
          _Session(b, options),
        _ => null,
      },
  ];
  final open = <_Session>[];
  Object? failure;
  StackTrace? trace;
  try {
    for (final s in sessions) {
      if (s == null) continue;
      open.add(s);
      await s.open();
    }
    if (verify) _verify(open, equals);
    await _padCallSites(Stopwatch()..start());
    // Warm up in turns, one batch each, so the JIT sees every benchmark of
    // the comparison equally often before it optimizes the timing loop.
    for (var warming = true; warming;) {
      warming = false;
      for (final s in open) {
        if (await s.step()) warming = true;
      }
    }
    final (:rounds, :perRound, :budget) = _schedule(options);
    for (var r = 0; r < rounds; r++) {
      for (final s in r.isEven ? open : open.reversed) {
        s.startRound(perRound, budget);
        await s.drive();
      }
    }
  } catch (error, stack) {
    failure = error;
    trace = stack;
  } finally {
    for (final s in open) {
      try {
        await s.close();
      } catch (error, stack) {
        failure ??= error;
        trace ??= stack;
      }
    }
  }
  if (failure != null) Error.throwWithStackTrace(failure, trace!);
  return [
    for (var i = 0; i < benchmarks.length; i++)
      switch ((sessions[i], benchmarks[i])) {
        (final s?, _) => s.measurement(),
        (_, SkippedBenchmark()) => null,
        (_, final b) => await b.measure(options),
      },
  ];
}