Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 library code_transformers.src.async_benchmark_base; | |
| 5 | |
| 6 import 'dart:async'; | |
| 7 | |
| 8 /// An adaptation of [BenchmarkBase] from the `benchmark_harness` package that | |
| 9 /// works for async benchmarks. | |
|
Siggi Cherem (dart-lang)
2015/04/13 20:46:02
maybe move this to benchmark_harness at this point
jakemac
2015/04/13 21:04:19
I will add a todo for it, but I don't want it to b
| |
| 10 class AsyncBenchmarkBase { | |
| 11 // Empty constructor. | |
| 12 const AsyncBenchmarkBase(); | |
| 13 | |
| 14 // The benchmark code. | |
| 15 // This function is not used, if both [warmup] and [exercise] are overwritten. | |
| 16 Future run() => new Future.value(); | |
| 17 | |
| 18 // Runs a short version of the benchmark. By default invokes [run] once. | |
| 19 Future warmup() => run(); | |
| 20 | |
| 21 // Exercices the benchmark. By default invokes [run] 10 times. | |
| 22 Future exercise({int iterations: 10}) { | |
| 23 var i = 0; | |
| 24 return Future.doWhile(() { | |
| 25 if (i >= iterations) return new Future.value(false); | |
| 26 i++; | |
| 27 return run().then((_) => true); | |
| 28 }); | |
| 29 } | |
| 30 | |
| 31 // Not measured setup code executed prior to the benchmark runs. | |
| 32 Future setup() => new Future.value(); | |
| 33 | |
| 34 // Not measures teardown code executed after the benchark runs. | |
| 35 Future teardown() => new Future.value(); | |
| 36 | |
| 37 // Measures the score for this benchmark by executing it repeatedly until | |
| 38 // time minimum has been reached. | |
| 39 static Future<double> measureFor(Function f, int minimumMillis) { | |
| 40 int minimumMicros = minimumMillis * 1000; | |
| 41 int iter = 0; | |
| 42 Stopwatch watch = new Stopwatch(); | |
| 43 watch.start(); | |
| 44 int elapsed = 0; | |
| 45 return Future.doWhile(() { | |
| 46 if (elapsed > minimumMicros) return new Future.value(false); | |
| 47 return f().then((_) { | |
| 48 elapsed = watch.elapsedMicroseconds; | |
| 49 iter++; | |
| 50 return true; | |
| 51 }); | |
| 52 }).then((_) => elapsed / iter); | |
| 53 } | |
| 54 | |
| 55 // Measures the average time to call `run` once and returns it. | |
| 56 Future<double> measure({int iterations: 10}) { | |
| 57 // Unmeasured setup code. | |
| 58 return setup().then((_) { | |
| 59 // Warmup for at least 100ms. Discard result. | |
| 60 return measureFor(() => warmup(), 100); | |
| 61 }).then((_) { | |
| 62 // Run the benchmark for at least 2000ms. | |
| 63 return measureFor(() => exercise(iterations: iterations), 2000); | |
| 64 }).then((result) { | |
| 65 // Tear down the test (unmeasured) and return the result divided by the | |
| 66 // number of iterations. | |
| 67 return teardown().then((_) => result / iterations); | |
| 68 }); | |
| 69 } | |
| 70 } | |
| OLD | NEW |