suite library

Benchmarks declared like package:test tests, with benchmark and group, and measured once main returns:

import 'package:microbench/suite.dart';

Future<void> main(List<String> args) => runSuite(args, () {
  group('sum', () {
    benchmark('loop', () => sumLoop(data));
    benchmark('fold', () => data.fold<int>(0, (a, b) => a + b));
  });
});

runSuite reads options such as --quick from the command line.

This library also exports package:microbench/microbench.dart. Its group clashes with package:test's; import one of them with a prefix if a file needs both.

Classes

AsyncBenchmark
A benchmark whose run is asynchronous. Every run is awaited before the next one starts.
Benchmark
A named piece of code to measure.
BenchmarkChange
One cell of a comparison, before and after (see BenchmarkDiff).
BenchmarkDiff
The differences between two runs of the same comparisons, matched by table title, group, row and column. Times are compared, so any metric works.
BenchmarkOptions
Timing parameters for measuring one benchmark.
BenchmarkSettings
Defaults for every comparison started inside apply, such as the ones runMain reads from the command line.
CellStyle
How a Markdown or HTML cell shows its ranking.
Comparison
A measured comparison table: groups of rows, with one column per input.
ComparisonCell
One cell of a comparison with its ranking worked out, for renderers.
ComparisonGroup
A group of rows compared against each other, such as all libraries that implement one algorithm.
ComparisonRow
One row of a ComparisonGroup: a benchmark's results, one per column.
HtmlTableRenderer
Renders an HTML <table>, with each group name spanning its rows. HTML tables also render inside Markdown files such as a README.
JsonTableRenderer
Renders each table as one line of JSON (see Comparison.toJson), so a run with several tables writes JSON Lines that parseComparisons reads back. Nothing is written until the table is complete.
MarkdownTableRenderer
Renders a GitHub-flavored Markdown table. The group name is shown on the first row of each group.
Measurement
The timing result of one benchmark.
Metric
What a comparison ranks benchmarks by, and how it prints them.
SkippedBenchmark
A placeholder that keeps a row in a comparison without measuring it, for example when a library does not support one column's input. Its cell is left empty and takes no part in the ranking.
SyncBenchmark
A benchmark whose run is synchronous.
TableRenderer
Turns a Comparison into text, in three parts so a table can be written while it is still being measured: header, one group per group, then footer.
TextTableRenderer
Renders aligned plain text for a terminal. Each group is a small table of its own, headed by the group name and the column headers.

Enums

ChangeKind
How a benchmark changed between two runs (see BenchmarkDiff).

Constants

runMainUsage → const String
The usage text of runMain.

Functions

benchmark(String name, FutureOr<Object?> run(), {int size = 0, FutureOr<void> setup()?, FutureOr<void> teardown()?}) → void
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).
compare(String name, Map<String, FutureOr<Object?> Function()> variants, {int size = 0, Metric? metric, BenchmarkOptions? options, bool? verify, double? alpha = 0.05, StringSink? out, TableRenderer? renderer}) → Future<Comparison>
Compares closures that do the same work, and prints a table.
currentEnvironment() → Map<String, String>
Where the benchmarks run, recorded with every comparison: the runtime (see detectRuntime), the Dart version when known, and the UTC time.
deepEquals(Object? a, Object? b) → bool
Whether a and b hold equal values, comparing lists (including typed data), sets, maps and other iterables element by element, and anything else with ==. A List<int> equals a Uint8List with the same bytes.
detectRuntime() → String
How this code runs: jit (dart run), aot (a compiled executable), js (dart2js) or wasm (dart2wasm).
formatBitRate(num bytesPerSecond) → String
A byte rate as bits per second in decimal units: formatBitRate(125000) is 1 Mbps. Values of 100 or more in their unit are rounded to whole numbers.
formatCount(num value) → String
A count with a decimal suffix: formatCount(1234567) is 1.23M.
formatDecimal(num value, [int precision = 2]) → String
value with at most precision decimals and no trailing zeros: formatDecimal(1.50) is 1.5, formatDecimal(2.0) is 2.
formatDuration(num micros) → String
A duration given in microseconds, in the largest fitting unit among ns, us, ms and s: formatDuration(1500) is 1.5 ms.
formatSize(num bytes) → String
A byte count in binary units: formatSize(1536) is 1.5KB.
group(String name, void body()) → void
Groups the benchmarks declared in body under name. Nested groups join their names with spaces. The benchmarks of a group are compared with the first one in it.
mannWhitneyP(List<double> a, List<double> b) → double?
The two-sided p-value of a Mann-Whitney U test: the probability of seeing a difference at least this large between a and b if both came from the same distribution.
matrix<T>(String name, {required List<T> cases, required Map<String, FutureOr<Object?> Function(T input)> variants, String label(T input)?, int size(T input)?, Metric? metric, BenchmarkOptions? options, bool? verify, double? alpha = 0.05, StringSink? out, TableRenderer? renderer}) → Future<Comparison>
Compares closures that do the same work across several inputs, and prints a table with one column per input.
measureAll(List<Benchmark> benchmarks, {BenchmarkOptions options = BenchmarkOptions.standard, bool verify = false, bool equals(Object? a, Object? b) = deepEquals}) → Future<List<Measurement?>>
Measures benchmarks together and returns their results in order, with null for each SkippedBenchmark.
parseComparisons(String text) → List<Comparison>
Parses tables written as JSON: one table per line (as JsonTableRenderer and --save write them), a JSON array of tables, or a single table.
ratioText(double score, double baseline, [String prefix = '', String suffix = '']) → String
2.5x faster or 2.5x slower for score against baseline, wrapped in prefix and suffix; empty when either is not positive or they are equal to two decimals.
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 onMeasure(String group, Benchmark benchmark, int column, Measurement?)?}) → Future<Comparison>
Measures a comparison table and returns it.
runMain(List<String> args, FutureOr<void> body()) → Future<void>
Runs a benchmark program's body with options from args, the command line given to main.
runRegistered() → Future<Comparison?>
Measures every benchmark declared with benchmark that has not run yet, as one table with a group per group, and returns it; null when there are none. Declared benchmarks run on their own after main returns, so call this only to run them earlier or to get the table.
runSuite(List<String> args, FutureOr<void> body()) → Future<void>
Declares benchmarks in body and runs them with options from args, the command line given to main: --quick, --verify, --filter and the others listed in runMainUsage.

Exceptions / Errors

OutputMismatchError
Thrown when benchmarks that should compute the same thing return different results (see the verify option of measureAll).