@@ -12,6 +12,21 @@ class Benchmark {
1212 final watch = Stopwatch ();
1313 final Emitter emitter;
1414
15+ /// Create a benchmark with the given [name] , starts measuring total run time.
16+ ///
17+ /// ```dart
18+ /// await Benchmark('Name', iterations: 1, coefficient: 1 / count).report();
19+ /// ```
20+ ///
21+ /// Call [report] on this to await results.
22+ ///
23+ /// Runs the [runIteration] function [iterations] times, defaults to 1.
24+ ///
25+ /// Set a fraction in [coefficient] to multiply the measured value of a run
26+ /// with, defaults to 1. Use this if a run calls a to be measured function
27+ /// multiple times (e.g. `1 / times` ) to get the duration of a single call.
28+ ///
29+ /// Results are printed to the command line.
1530 Benchmark (this .name, {this .iterations = 1 , this .coefficient = 1 })
1631 : emitter = Emitter (iterations, coefficient) {
1732 print ('-------------------------------------------------------------' );
@@ -24,9 +39,13 @@ class Benchmark {
2439 watch.start ();
2540 }
2641
27- // Not measured setup code executed prior to the benchmark runs .
42+ /// Not measured setup code executed prior to [run] getting called .
2843 void setup () {}
2944
45+ /// Called after all [run] calls have completed, measures total time of the
46+ /// benchmark.
47+ ///
48+ /// A method overriding this must call this.
3049 @mustCallSuper
3150 void teardown () {
3251 final millis = watch.elapsedMilliseconds;
@@ -35,16 +54,18 @@ class Benchmark {
3554 '$color ${Emitter ._format (millis .toDouble (), suffix : ' ms' )}\x 1B[0m' );
3655 }
3756
57+ /// Calls [runIteration] [iterations] of times.
3858 Future <void > run () async {
3959 for (var i = 0 ; i < iterations; i++ ) runIteration (i);
4060 return Future .value ();
4161 }
4262
63+ /// A single test iteration, given [iteration] index starting from 0.
4364 void runIteration (int iteration) {
4465 throw UnimplementedError ('Please override runIteration() or run()' );
4566 }
4667
47- // Runs [f] for at least [minimumMillis] milliseconds.
68+ /// Runs [f] for at least [minimumMillis] milliseconds.
4869 static Future <double > _measureFor (Function f, int minimumMillis) async {
4970 final minimumMicros = minimumMillis * 1000 ;
5071 var iter = 0 ;
@@ -58,7 +79,9 @@ class Benchmark {
5879 return elapsed / iter;
5980 }
6081
61- // Measures the score for the benchmark and returns it.
82+ /// Measures the score for the benchmark and returns it.
83+ ///
84+ /// See [report] for details.
6285 @nonVirtual
6386 Future <double > _measure () async {
6487 setup ();
@@ -70,6 +93,13 @@ class Benchmark {
7093 return result;
7194 }
7295
96+ /// Starts the benchmark and waits for the result.
97+ ///
98+ /// - Calls [setup] , then
99+ /// - repeatedly calls [run] for at least 100 ms to warm up,
100+ /// - then calls [run] repeatedly for at least 2000 ms and collects the
101+ /// average elapsed time of a call (if run multiple times), then
102+ /// - calls [teardown] and returns the result.
73103 @nonVirtual
74104 Future <void > report () async {
75105 emitter.emit (name, await _measure ());
0 commit comments