benchmark function

void benchmark(
  1. String name,
  2. FutureOr<Object?> run(), {
  3. int size = 0,
  4. FutureOr<void> setup()?,
  5. FutureOr<void> teardown()?,
})

Declares a benchmark of run, measured once main has returned (or at the end of runSuite) and printed as a table with the others. After a plain main, options come from the MICROBENCH_ARGS environment variable (see runMainUsage).

A run that returns a Future is awaited on every run. Return the result of the work, so it is not optimized away.

Parameters:

  • name: the row label.
  • run: the code to measure.
  • size: units processed per run; when every benchmark sets it, the table shows throughput instead of runs per second.
  • setup, teardown: unmeasured code run before and after.

Implementation

void benchmark(
  String name,
  FutureOr<Object?> Function() run, {
  int size = 0,
  FutureOr<void> Function()? setup,
  FutureOr<void> Function()? teardown,
}) {
  _entries.add(_Entry(
    _groups.join(' '),
    closureBenchmark(name, run, size: size, setup: setup, teardown: teardown),
  ));
  if (!_scheduled) {
    _scheduled = true;
    Timer.run(() {
      _scheduled = false;
      // Already run by runSuite or runMain, or by an earlier call.
      if (_entries.isEmpty) return;
      runMain(const [], () {});
    });
  }
}