Skip to content
Snippets Groups Projects
README.md 2.15 KiB
Newer Older
Seth Ladd's avatar
Seth Ladd committed
# Dart Benchmark Harness
John McCutchan's avatar
John McCutchan committed

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

Seth Ladd's avatar
Seth Ladd committed
## Learning more

You can read more about [Benchmarking the Dart VM](https://www.dartlang.org/articles/benchmarking/).

## Interpreting Results
John McCutchan's avatar
John McCutchan committed

John McCutchan's avatar
John McCutchan committed
By default, the reported runtime is not for a single call to `run()`, but for
the average time it takes to call `run()` __10 times__. 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.
John McCutchan's avatar
John McCutchan committed

Seth Ladd's avatar
Seth Ladd committed
## Comparing Results
John McCutchan's avatar
John McCutchan committed

John McCutchan's avatar
John McCutchan committed
If you are running the same benchmark, on the same machine, running the same OS,
John McCutchan's avatar
John McCutchan committed
the reported run times can be carefully compared across runs.
Carefully because there are a variety of factors which
John McCutchan's avatar
John McCutchan committed
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.

Seth Ladd's avatar
Seth Ladd committed
## Features
John McCutchan's avatar
John McCutchan committed

John McCutchan's avatar
John McCutchan committed
* `BenchmarkBase` class that all new benchmarks should `extend`
John McCutchan's avatar
John McCutchan committed
* Two sample benchmarks (DeltaBlue & Richards)
* Template benchmark that you can copy and paste when building new benchmarks

Seth Ladd's avatar
Seth Ladd committed
## Getting Started
John McCutchan's avatar
John McCutchan committed

1\. Add the following to your project's **pubspec.yaml**

```
dependencies:
Seth Ladd's avatar
Seth Ladd committed
    benchmark_harness: any
John McCutchan's avatar
John McCutchan committed
```

2\. Install pub packages

```
pub install
```

3\. Add the following import:

```
import 'package:benchmark_harness/benchmark_harness.dart';
```

John McCutchan's avatar
John McCutchan committed
4\. Create a benchmark class which inherits from `BenchmarkBase`
John McCutchan's avatar
John McCutchan committed

Seth Ladd's avatar
Seth Ladd committed
## Example
John McCutchan's avatar
John McCutchan committed

```
// 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() {
    new TemplateBenchmark().report();
  }

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

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

  // Not measures teardown code executed after the benchark runs.
  void teardown() { }
}

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