p-hatan/ mirror

pub repository

mirror.p-hatan.ir

benchmark_harness

2.4.0

The official Dart project benchmark harness.

Download archive

Readme

Build Status pub package package publisher

The Dart project benchmark harness is the recommended starting point when building a benchmark for Dart.

Interpreting Results

By default, the reported runtime in BenchmarkBase is not for a single call to run(), but for the average time it takes to call run() 10 times for legacy reasons. The benchmark harness executes a 10-call timing loop repeatedly until 2 seconds have elapsed; the reported result is the average of the runtimes for each loop. This behavior will change in a future major version.

Benchmarks extending BenchmarkBase can opt into the reporting the average time to call run() once by overriding the exercise method:

  @override
  void exercise() => run();

AsyncBenchmarkBase already reports the average time to call run() once.

Comparing Results

If you are running the same benchmark, on the same machine, running the same OS, the reported run times can be carefully compared across runs. Carefully because there are a variety of factors which could cause error in the run time, for example, the load from other applications running on your machine could alter the result.

Comparing the run time of different benchmarks is not recommended. In other words, don't compare apples with oranges.

Features

  • BenchmarkBase class that all new benchmarks should extend.
  • AsyncBenchmarkBase for asynchronous benchmarks.
  • Template benchmark that you can copy and paste when building new benchmarks.

Getting Started

  1. Add the following to your project's pubspec.yaml
dependencies:
    benchmark_harness: any
  1. Install pub packages
dart pub install
  1. Add the following import:
import 'package:benchmark_harness/benchmark_harness.dart';
  1. Create a benchmark class which inherits from BenchmarkBase or AsyncBenchmarkBase.

Example

Create a dart file in the benchmark/ folder of your package.

// Import BenchmarkBase class.
import 'package:benchmark_harness/benchmark_harness.dart';

// Create a new benchmark by extending BenchmarkBase
class TemplateBenchmark extends BenchmarkBase {
  const TemplateBenchmark() : super('Template');

  static void main() {
    const TemplateBenchmark().report();
  }

  // The benchmark code.
  @override
  void run() {}

  // Not measured setup code executed prior to the benchmark runs.
  @override
  void setup() {}

  // Not measured teardown code executed after the benchmark runs.
  @override
  void teardown() {}

  // To opt into the reporting the time per run() instead of per 10 run() calls.
  //@override
  //void exercise() => run();
}

void main() {
  // Run TemplateBenchmark
  TemplateBenchmark.main();
}

Output

Template(RunTime): 0.1568472448997197 us.

This is the average amount of time it takes to run run() 10 times for BenchmarkBase and once for AsyncBenchmarkBase.

us is an abbreviation for microseconds.

bench command

A convenience command available in package:benchmark_harness.

If a package depends on benchmark_harness, invoke the command by running

dart run benchmark_harness:bench

If not, you can use this command by activating it.

dart pub global activate benchmark_harness
dart pub global run benchmark_harness:bench

Output from dart run benchmark_harness:bench --help

Runs a dart script in a number of runtimes.

Meant to make it easy to run a benchmark executable across runtimes to validate
performance impacts.

-f, --flavor
          [aot]     Compile and run as a native binary.
          [jit]     Run as-is without compilation, using the just-in-time (JIT) runtime.
          [js]      Compile to JavaScript and run on node.
          [wasm]    Compile to WebAssembly and run on node.

    --target        The target script to compile and run.
                    (defaults to "benchmark/benchmark.dart")
-h, --help          Print usage information and quit.
-v, --verbose       Print the full stack trace if an exception is thrown.

Example usage:

dart run benchmark_harness:bench --flavor aot --target example/template.dart

AOT - COMPILE
/dart_installation/dart-sdk/bin/dart compile exe example/template.dart -o /temp_dir/bench_1747680526905_GtfAeM/out.exe

Generated: /temp_dir/bench_1747680526905_GtfAeM/out.exe

AOT - RUN
/temp_dir/bench_1747680526905_GtfAeM/out.exe

Template(RunTime): 0.005620051244379949 us.
Changelog

2.4.0

  • Added a bench command.
  • Require sdk: ^3.10.0.

2.3.1

  • Move to dart-lang/tools monorepo.

2.3.0

  • Require Dart 3.2.
  • Add ScoreEmitterV2 interface, documented with the intention to change ScoreEmitter interface to match it in the next major release, a breaking change.
  • Add PerfBenchmarkBase class which runs the 'perf stat' command from linux-tools on a benchmark and reports metrics from the hardware performance counters and the iteration count, as well as the run time measurement reported by BenchmarkBase.

2.2.2

  • Added package topics to the pubspec file.
  • Require Dart 2.19.

2.2.1

  • Improve convergence speed of BenchmarkBase measuring algorithm by allowing some degree of measuring jitter.

2.2.0

  • Change measuring algorithm in BenchmarkBase to avoid calling stopwatch methods repeatedly in the measuring loop. This makes measurement work better for run methods which are small themselves.

2.1.0

  • Add AsyncBenchmarkBase.

2.0.0

  • Stable null safety release.

2.0.0-nullsafety.0

  • Opt in to null safety.

1.0.6

  • Require at least Dart 2.1.

1.0.5

  • Updates to support Dart 2.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add benchmark_harness
pubspec.yaml
name: benchmark_harness
version: 2.4.0
description: The official Dart project benchmark harness.
repository: https://github.com/dart-lang/tools/tree/main/pkgs/benchmark_harness
issue_tracker: https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Abenchmark_harness

topics:
  - benchmarking

environment:
  sdk: ^3.10.0

dependencies:
  args: ^2.5.0

dev_dependencies:
  dart_flutter_team_lints: ^3.0.0
  path: ^1.9.0
  test: ^1.25.7

executables:
  bench:

Version history

VersionPublishedArchiveSHA-256
1.0.02012-11-20T20:35:57.470500ZDownloadc1b39858cd0c644f2e77392000bb7e113f45ab7fe43fd9c15cd85294672e5208
1.0.12012-11-26T23:35:52.164560ZDownload12f16523a9c17a0d01d6eabd453e7326c6abf26b02a117a6eee1bde71a420f7c
1.0.22012-12-20T15:32:37.592300ZDownload806298d7f0cad8ffdd715a895cf1f7b151467b71643bc80724e5f05171eabb87
1.0.32014-01-10T22:18:23.017320ZDownload47e42ab30e48c056c5e603d4d15cc887ce48470584cdaec25f202adb31aef42c
1.0.42014-02-14T00:59:26.997070ZDownload992f666f3412d7c1c6fd3de835be43259213c20e702cbf98106cd0feeb4e96d1
1.0.52018-07-26T18:22:56.907758ZDownload811240744c8065bdfb5b95bea83a1b565acdf73bfc29bcadb590d66dd7687c4c
2.0.0-nullsafety.02020-11-10T23:36:44.597010ZDownload72ea8bcf1d71764571cc72969a7187123e15becc528dbba11d53ad9f5c86df84
2.0.02021-02-05T20:58:47.672178ZDownloade8e153466b38af37038a23a94c94618b864038f4163156f7d06b8b7ebfda76b0
2.1.02022-05-03T12:16:52.401589ZDownload72b770741a03b657bf830bda92682f8e1c8d1c459ff2859168f0cb737d00da0d
2.2.02022-07-06T10:54:03.351406ZDownload200beeddb5e8b3611ebb0bc8a730d63967afc3c9a5898791245eb46712a83a24
2.2.12023-02-02T16:09:31.837887ZDownload683bdcf9d96ec6f244d383a8b07f329fe64f0d3ef51739b82417ce904dd29d03
2.2.22023-05-08T23:55:08.922857ZDownloada12bc021ec45c05f60ad65a6cec1be881d4d84842699b2c4f4aa59f1089de242
2.2.32024-04-19T10:55:50.052240ZDownloadb0a9b0f6d3ac39f372c17494716acb39bf9517a789217b7f28256eb0ca011872
2.3.12024-10-29T14:44:27.000066ZDownload83f65107165883ba8623eb822daacb23dcf9f795c66841de758c9dd7c5a0cf28
2.4.02025-11-25T22:05:59.874420ZDownloada2d3c4c83cac0126bf38e41eaf7bd9ed4f6635f1ee1a0cbc6f79fa9736c62cbd

Dependencies

args "^2.5.0"