io library
File and process helpers for benchmark reports, on platforms with
dart:io (not the web): stream a report into a file, embed it in a
README, run benchmark programs on several compilers, and run the
microbench command.
This library also exports package:microbench/microbench.dart.
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
runMainreads 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.
- ReportFile
- A StringSink that writes to a file, and optionally echoes to stdout.
- RunStep
- One command of a planned run, or a file to write first.
- 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
- microbenchUsage → const String
- The usage text of microbenchCommand.
- runMainUsage → const String
- The usage text of runMain.
-
runTargets
→ const List<
String> -
The runtimes planRun can target:
jit(dart run),aot(a native executable),js(dart2js on Node.js) andwasm(dart2wasm on Node.js 22 or later).
Functions
-
compare(
String name, Map< String, FutureOr< variants, {int size = 0, Metric? metric, BenchmarkOptions? options, bool? verify, double? alpha = 0.05, StringSink? out, TableRenderer? renderer}) → Future<Object?> Function()>Comparison> - Compares closures that do the same work, and prints a table.
-
compileAndRun(
String script, {List< String> arguments = const [], String outputDir = 'build'}) → Future<int> -
Compiles
scriptto a native executable withdart compile exe, runs it witharguments, and returns its exit code. -
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
aandbhold equal values, comparing lists (including typed data), sets, maps and other iterables element by element, and anything else with==. AList<int>equals aUint8Listwith the same bytes. -
detectRuntime(
) → String -
How this code runs:
jit(dart run),aot(a compiled executable),js(dart2js) orwasm(dart2wasm). -
embedFile(
{String source = 'BENCHMARK.md', String target = 'README.md', String? startMarker, String? endMarker}) → bool -
Replaces everything between two marker lines of
targetwith the content ofsource, and returns whethertargetchanged. -
environmentSummary(
) → String - One line describing where the benchmarks ran: the Dart version, operating system and processor count. Benchmark numbers mean little without it.
-
findBenchmarks(
List< String> paths) → List<String> -
The benchmark programs in
paths: each file as given, and every*_benchmark.dartfile under each directory, sorted. -
formatBitRate(
num bytesPerSecond) → String -
A byte rate as bits per second in decimal units:
formatBitRate(125000)is1 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)is1.23M. -
formatDecimal(
num value, [int precision = 2]) → String -
valuewith at mostprecisiondecimals and no trailing zeros:formatDecimal(1.50)is1.5,formatDecimal(2.0)is2. -
formatDuration(
num micros) → String -
A duration given in microseconds, in the largest fitting unit among
ns,us,msands:formatDuration(1500)is1.5 ms. -
formatSize(
num bytes) → String -
A byte count in binary units:
formatSize(1536)is1.5KB. -
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
aandbif both came from the same distribution. -
matrix<
T> (String name, {required List< T> cases, required Map<String, FutureOr< variants, String label(T input)?, int size(T input)?, Metric? metric, BenchmarkOptions? options, bool? verify, double? alpha = 0.05, StringSink? out, TableRenderer? renderer}) → Future<Object?> Function(T input)>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
benchmarkstogether and returns their results in order, withnullfor each SkippedBenchmark. -
microbenchCommand(
List< String> args, {void log(String line) = print}) → Future<int> -
Runs the
microbenchcommand withargsand returns its exit code. -
parseComparisons(
String text) → List< Comparison> -
Parses tables written as JSON: one table per line (as
JsonTableRenderer and
--savewrite them), a JSON array of tables, or a single table. -
planRun(
String script, String target, {List< String> arguments = const [], String outputDir = 'build/microbench'}) → List<RunStep> -
The steps that compile (if needed) and run the Dart program
scriptontarget, one of runTargets, passing itarguments. -
ratioText(
double score, double baseline, [String prefix = '', String suffix = '']) → String -
2.5x fasteror2.5x slowerforscoreagainstbaseline, wrapped inprefixandsuffix; empty when either is not positive or they are equal to two decimals. -
runBenchmarks(
List< String> paths, {List<String> targets = const ['aot'], List<String> arguments = const [], String outputDir = 'build/microbench', void log(String line) = print}) → Future<int> -
Runs every benchmark program in
paths(see findBenchmarks) on each oftargets, one at a time, and returns 0 if all succeed, else 1 (also when there is no program to run). -
runComparison(
{required List< String> columns, required Map<String, List< 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<Benchmark> Function(int column)>Comparison> - Measures a comparison table and returns it.
-
runMain(
List< String> args, FutureOr<void> body()) → Future<void> -
Runs a benchmark program's
bodywith options fromargs, the command line given tomain.
Exceptions / Errors
- OutputMismatchError
-
Thrown when benchmarks that should compute the same thing return
different results (see the
verifyoption of measureAll).