Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011, 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 | |
| 5 /** | |
| 6 * The superclass from which all benchmarks inherit from. | |
|
Siggi Cherem (dart-lang)
2011/12/15 19:37:33
nit: we tend to do the 1-line style doc (/** comme
Emily Fortuna
2011/12/15 23:16:10
Done.
| |
| 7 */ | |
| 8 class BenchmarkBase { | |
| 9 /* Benchmark name. */ | |
|
Siggi Cherem (dart-lang)
2011/12/15 19:37:33
nit: -> add one more * (/**)
(here and below)
Emily Fortuna
2011/12/15 23:16:10
Done.
| |
| 10 final String name; | |
| 11 | |
| 12 const BenchmarkBase(String name) : this.name = name; | |
| 13 | |
| 14 /** | |
| 15 * The benchmark code. | |
| 16 * This function is not used, if both [warmup] and [exercise] are overwritten. | |
| 17 */ | |
| 18 void run() { } | |
| 19 | |
| 20 /** | |
| 21 * Runs a short version of the benchmark. By default invokes [run] once. | |
| 22 */ | |
| 23 void warmup() { | |
| 24 run(); | |
| 25 } | |
| 26 | |
| 27 /** | |
| 28 * Exercices the benchmark. By default invokes [run] 10 times. | |
| 29 */ | |
| 30 void exercise() { | |
| 31 for (int i = 0; i < 10; i++) { | |
| 32 run(); | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 /** | |
| 37 * Not measured setup code executed prior to the benchmark runs. | |
| 38 */ | |
| 39 void setup() { } | |
| 40 | |
| 41 /** | |
| 42 * Not measures teardown code executed after the benchark runs. | |
| 43 */ | |
| 44 void teardown() { } | |
| 45 | |
| 46 /** | |
| 47 * Measures the score for this benchmark by executing it repeately until | |
| 48 * time minimum has been reached. | |
| 49 */ | |
| 50 static double measureFor(Function f, int timeMinimum) { | |
| 51 int time = 0; | |
| 52 int iter = 0; | |
| 53 Stopwatch watch = new Stopwatch(); | |
| 54 watch.start(); | |
| 55 int elapsed = 0; | |
| 56 while (elapsed < timeMinimum || iter < 32) { | |
| 57 f(); | |
| 58 elapsed = watch.elapsedInMs(); | |
| 59 iter++; | |
| 60 } | |
| 61 return (1000.0 * iter) / elapsed; | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * Measures the score for the benchmark and returns it. | |
| 66 * We measure iterations / sec (so bigger = better!). | |
| 67 */ | |
| 68 double measure() { | |
| 69 setup(); | |
| 70 // Warmup for at least 1000ms. Discard result. | |
| 71 measureFor(() { this.warmup(); }, 1000); | |
| 72 // Run the benchmark for at least 1000ms. | |
| 73 double result = measureFor(() { this.exercise(); }, 1000); | |
| 74 teardown(); | |
| 75 return result; | |
| 76 } | |
| 77 | |
| 78 void report() { | |
| 79 num score = measure(); | |
| 80 Map<String, int> normalizingDict = {'Smoketest': 100}; | |
| 81 window.console.log(name + " " + score.toString()); | |
| 82 score = score / normalizingDict[name]; | |
| 83 BenchmarkSuite.ONLY.updateIndividualScore(name, score); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * The controller class that runs all of the benchmarks. | |
| 89 */ | |
| 90 class BenchmarkSuite { | |
| 91 /* The set of benchmarks that have yet to run. */ | |
| 92 List<Function> benchmarks; | |
| 93 /** | |
|
Siggi Cherem (dart-lang)
2011/12/15 19:37:33
nit: + empty line
Emily Fortuna
2011/12/15 23:16:10
Done.
| |
| 94 * The set of scores from the benchmarks that have already run. (Used for | |
| 95 * calculating the Geometric mean). | |
| 96 */ | |
| 97 List<num> scores; | |
| 98 | |
| 99 /* The total number of benchmarks we will be running. */ | |
| 100 int totalBenchmarks; | |
| 101 | |
| 102 /* Singleton pattern: There's only one BenchmarkSuite. */ | |
| 103 static BenchmarkSuite _ONLY = null; | |
| 104 | |
| 105 BenchmarkSuite._internal() { | |
| 106 scores = []; | |
| 107 benchmarks = [() => Smoketest.main()]; | |
| 108 totalBenchmarks = benchmarks.length; | |
| 109 } | |
| 110 | |
| 111 /* Accessor for our Singleton variable. */ | |
| 112 static BenchmarkSuite get ONLY() { | |
|
Siggi Cherem (dart-lang)
2011/12/15 19:37:33
now that we have top-level getters, this would loo
Emily Fortuna
2011/12/15 23:16:10
Done.
| |
| 113 if (_ONLY == null) { | |
| 114 _ONLY = new BenchmarkSuite._internal(); | |
| 115 } | |
| 116 return _ONLY; | |
| 117 } | |
| 118 | |
| 119 /* Run all of the benchmarks that we have in our benchmarks list. */ | |
| 120 runBenchmarks() { | |
| 121 runBenchmarksHelper(benchmarks); | |
| 122 } | |
| 123 | |
| 124 /** | |
| 125 * Run the remaining benchmarks in our list. We chain the calls providing | |
| 126 * little breaks for the main page to gain control, so we don't force the | |
| 127 * entire page to hang the whole time. | |
| 128 */ | |
| 129 runBenchmarksHelper(benchmarks) { | |
|
Siggi Cherem (dart-lang)
2011/12/15 19:37:33
here a type for the argument would be helpful (e.g
Emily Fortuna
2011/12/15 23:16:10
I need to pass this argument so that there are bri
| |
| 130 // Remove the last benchmark, and run it. | |
| 131 var benchmark = benchmarks.removeLast(); | |
| 132 benchmark(); | |
| 133 if (benchmarks.length > 0) { | |
| 134 /* Provide small breaks between each benchmark, so that the browser | |
| 135 doesn't get unhappy about long running scripts, and so the user | |
| 136 can regain control of the UI to kill the page as needed. */ | |
| 137 window.setTimeout(() => runBenchmarksHelper(benchmarks), 25); | |
| 138 } else if (benchmarks.length == 0) { | |
| 139 // We've run all of the benchmarks. Update the page with the score. | |
| 140 BenchmarkView.ONLY.setScore(geometricMean(scores)); | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 /* Store the results of a single benchmark run. */ | |
| 145 updateIndividualScore(String name, num score) { | |
| 146 scores.add(score); | |
| 147 BenchmarkView.ONLY.incrementProgress(name, score, totalBenchmarks); | |
| 148 } | |
| 149 | |
| 150 /* Computes the geometric mean of a set of numbers. */ | |
| 151 geometricMean(numbers) { | |
| 152 num log = 0; | |
| 153 for (num n in numbers) { | |
| 154 log += Math.log(n); | |
| 155 } | |
| 156 return Math.pow(Math.E, log / numbers.length); | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 /* Controls how results are displayed to the user, by updating the HTML. */ | |
| 161 class BenchmarkView { | |
| 162 | |
| 163 /* The number of benchmarks that have finished executing. */ | |
| 164 int numCompleted = 0; | |
| 165 | |
| 166 /* Singleton pattern: There's only one BenchmarkSuite. */ | |
| 167 static BenchmarkView _ONLY = null; | |
| 168 | |
| 169 BenchmarkView._internal(); | |
| 170 | |
| 171 /* Accessor for our Singleton variable. */ | |
| 172 static BenchmarkView get ONLY() { | |
|
Siggi Cherem (dart-lang)
2011/12/15 19:37:33
same here (move to top-level)
Emily Fortuna
2011/12/15 23:16:10
Done.
| |
| 173 if (_ONLY == null) { | |
| 174 _ONLY = new BenchmarkView._internal(); | |
| 175 } | |
| 176 return _ONLY; | |
| 177 } | |
| 178 | |
| 179 /* Update the page HTML to show the calculated score. */ | |
| 180 setScore(num score) { | |
| 181 String newScore = formatScore(score * 100.0); | |
| 182 Element status = document.query("#status"); | |
| 183 status.innerHTML = "Score: $newScore <br>"; | |
| 184 } | |
| 185 | |
| 186 /** | |
| 187 * Update the page HTML to show how much progress we've made through the | |
| 188 * benchmarks. | |
| 189 */ | |
| 190 incrementProgress(String name, num score, num totalBenchmarks) { | |
| 191 String newScore = formatScore(score * 100.0); | |
| 192 Element results = document.query("#results"); | |
| 193 results.innerHTML += "$name: $newScore <br>"; | |
| 194 | |
| 195 Element status = document.query("#status"); | |
| 196 numCompleted++; | |
| 197 // Slightly incorrect (truncating) percentage, but this is just to show | |
| 198 // the user we're making progress. | |
| 199 num percentage = 100 * numCompleted ~/ totalBenchmarks; | |
| 200 status.innerHTML = "Running: $percentage% completed."; | |
| 201 } | |
| 202 | |
| 203 /** | |
| 204 * Rounds the score to have at least three significant digits (hopefully) | |
| 205 * helping readability of the scores. | |
| 206 */ | |
| 207 String formatScore(num value) { | |
| 208 if (value > 100) { | |
| 209 return value.toStringAsFixed(0); | |
| 210 } else { | |
| 211 return value.toStringAsFixed(2); | |
| 212 } | |
| 213 } | |
| 214 } | |
| OLD | NEW |