|
| 1 | +[](https://github.com/dart-lang/tools/actions/workflows/benchmark_harness.yaml) |
| 2 | +[](https://pub.dev/packages/benchmark_harness) |
| 3 | +[](https://pub.dev/packages/benchmark_harness/publisher) |
| 4 | + |
| 5 | +The Dart project benchmark harness is the recommended starting point when |
| 6 | +building a benchmark for Dart. |
| 7 | + |
| 8 | +## Interpreting Results |
| 9 | + |
| 10 | +By default, the reported runtime in `BenchmarkBase` is not for a single call to |
| 11 | +`run()`, but for the average time it takes to call `run()` __10 times__ for |
| 12 | +legacy reasons. The benchmark harness executes a 10-call timing loop repeatedly |
| 13 | +until 2 seconds have elapsed; the reported result is the average of the runtimes |
| 14 | +for each loop. This behavior will change in a future major version. |
| 15 | + |
| 16 | +Benchmarks extending `BenchmarkBase` can opt into the reporting the average time |
| 17 | +to call `run()` once by overriding the `exercise` method: |
| 18 | + |
| 19 | +```dart |
| 20 | + @override |
| 21 | + void exercise() => run(); |
| 22 | +``` |
| 23 | + |
| 24 | +`AsyncBenchmarkBase` already reports the average time to call `run()` __once__. |
| 25 | + |
| 26 | +## Comparing Results |
| 27 | + |
| 28 | +If you are running the same benchmark, on the same machine, running the same OS, |
| 29 | +the reported run times can be carefully compared across runs. |
| 30 | +Carefully because there are a variety of factors which |
| 31 | +could cause error in the run time, for example, the load from |
| 32 | +other applications running on your machine could alter the result. |
| 33 | + |
| 34 | +Comparing the run time of different benchmarks is not recommended. |
| 35 | +In other words, don't compare apples with oranges. |
| 36 | + |
| 37 | +## Features |
| 38 | + |
| 39 | +* `BenchmarkBase` class that all new benchmarks should `extend`. |
| 40 | +* `AsyncBenchmarkBase` for asynchronous benchmarks. |
| 41 | +* Template benchmark that you can copy and paste when building new benchmarks. |
| 42 | + |
| 43 | +## Getting Started |
| 44 | + |
| 45 | +1\. Add the following to your project's **pubspec.yaml** |
| 46 | + |
| 47 | +```yaml |
| 48 | +dependencies: |
| 49 | + benchmark_harness: any |
| 50 | +``` |
| 51 | +
|
| 52 | +2\. Install pub packages |
| 53 | +
|
| 54 | +```sh |
| 55 | +dart pub install |
| 56 | +``` |
| 57 | + |
| 58 | +3\. Add the following import: |
| 59 | + |
| 60 | +```dart |
| 61 | +import 'package:benchmark_harness/benchmark_harness.dart'; |
| 62 | +``` |
| 63 | + |
| 64 | +4\. Create a benchmark class which inherits from `BenchmarkBase` or |
| 65 | + `AsyncBenchmarkBase`. |
| 66 | + |
| 67 | +## Example |
| 68 | + |
| 69 | +Create a dart file in the |
| 70 | +[`benchmark/`](https://dart.dev/tools/pub/package-layout#tests-and-benchmarks) |
| 71 | +folder of your package. |
| 72 | + |
| 73 | +```dart |
| 74 | +// Import BenchmarkBase class. |
| 75 | +import 'package:benchmark_harness/benchmark_harness.dart'; |
| 76 | +
|
| 77 | +// Create a new benchmark by extending BenchmarkBase |
| 78 | +class TemplateBenchmark extends BenchmarkBase { |
| 79 | + const TemplateBenchmark() : super('Template'); |
| 80 | +
|
| 81 | + static void main() { |
| 82 | + const TemplateBenchmark().report(); |
| 83 | + } |
| 84 | +
|
| 85 | + // The benchmark code. |
| 86 | + @override |
| 87 | + void run() {} |
| 88 | +
|
| 89 | + // Not measured setup code executed prior to the benchmark runs. |
| 90 | + @override |
| 91 | + void setup() {} |
| 92 | +
|
| 93 | + // Not measured teardown code executed after the benchmark runs. |
| 94 | + @override |
| 95 | + void teardown() {} |
| 96 | +
|
| 97 | + // To opt into the reporting the time per run() instead of per 10 run() calls. |
| 98 | + //@override |
| 99 | + //void exercise() => run(); |
| 100 | +} |
| 101 | +
|
| 102 | +void main() { |
| 103 | + // Run TemplateBenchmark |
| 104 | + TemplateBenchmark.main(); |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Output |
| 109 | + |
| 110 | +```console |
| 111 | +Template(RunTime): 0.1568472448997197 us. |
| 112 | +``` |
| 113 | + |
| 114 | +This is the average amount of time it takes to run `run()` 10 times for |
| 115 | +`BenchmarkBase` and once for `AsyncBenchmarkBase`. |
| 116 | +> µs is an abbreviation for microseconds. |
0 commit comments