OLD | NEW |
(Empty) | |
| 1 // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 |
| 29 // Simple framework for running the benchmark suites and |
| 30 // computing a score based on the timing measurements. |
| 31 |
| 32 |
| 33 // A benchmark has a name (string) and a function that will be run to |
| 34 // do the performance measurement. |
| 35 function Benchmark(name, run) { |
| 36 this.name = name; |
| 37 this.run = run; |
| 38 } |
| 39 |
| 40 |
| 41 // Benchmark results hold the benchmark and the measured time used to |
| 42 // run the benchmark. The benchmark score is computed later once a |
| 43 // full benchmark suite has run to completion. |
| 44 function BenchmarkResult(benchmark, time) { |
| 45 this.benchmark = benchmark; |
| 46 this.time = time; |
| 47 } |
| 48 |
| 49 |
| 50 // Automatically convert results to numbers. Used by the geometric |
| 51 // mean computation. |
| 52 BenchmarkResult.prototype.valueOf = function() { |
| 53 return this.time; |
| 54 } |
| 55 |
| 56 |
| 57 // Suites of benchmarks consist of a name and the set of benchmarks in |
| 58 // addition to the reference timing that the final score will be based |
| 59 // on. This way, all scores are relative to a reference run and higher |
| 60 // scores implies better performance. |
| 61 function BenchmarkSuite(name, reference, benchmarks) { |
| 62 this.name = name; |
| 63 this.reference = reference; |
| 64 this.benchmarks = benchmarks; |
| 65 BenchmarkSuite.suites.push(this); |
| 66 } |
| 67 |
| 68 |
| 69 // Keep track of all declared benchmark suites. |
| 70 BenchmarkSuite.suites = []; |
| 71 |
| 72 |
| 73 // Scores are not comparable across versions. Bump the version if |
| 74 // you're making changes that will affect that scores, e.g. if you add |
| 75 // a new benchmark or change an existing one. |
| 76 BenchmarkSuite.version = '3'; |
| 77 |
| 78 |
| 79 // To make the benchmark results predictable, we replace Math.random |
| 80 // with a 100% deterministic alternative. |
| 81 Math.random = (function() { |
| 82 var seed = 49734321; |
| 83 return function() { |
| 84 // Robert Jenkins' 32 bit integer hash function. |
| 85 seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff; |
| 86 seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff; |
| 87 seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff; |
| 88 seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff; |
| 89 seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff; |
| 90 seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff; |
| 91 seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff; |
| 92 return (seed & 0xfffffff) / 0x10000000; |
| 93 }; |
| 94 })(); |
| 95 |
| 96 |
| 97 // Runs all registered benchmark suites and optionally yields between |
| 98 // each individual benchmark to avoid running for too long in the |
| 99 // context of browsers. Once done, the final score is reported to the |
| 100 // runner. |
| 101 BenchmarkSuite.RunSuites = function(runner) { |
| 102 var continuation = null; |
| 103 var suites = BenchmarkSuite.suites; |
| 104 var length = suites.length; |
| 105 BenchmarkSuite.scores = []; |
| 106 var index = 0; |
| 107 function RunStep() { |
| 108 while (continuation || index < length) { |
| 109 if (continuation) { |
| 110 continuation = continuation(); |
| 111 } else { |
| 112 var suite = suites[index++]; |
| 113 if (runner.NotifyStart) runner.NotifyStart(suite.name); |
| 114 continuation = suite.RunStep(runner); |
| 115 } |
| 116 if (continuation && typeof window != 'undefined' && window.setTimeout) { |
| 117 window.setTimeout(RunStep, 100); |
| 118 return; |
| 119 } |
| 120 } |
| 121 if (runner.NotifyScore) { |
| 122 var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores); |
| 123 var formatted = BenchmarkSuite.FormatScore(100 * score); |
| 124 runner.NotifyScore(formatted); |
| 125 } |
| 126 } |
| 127 RunStep(); |
| 128 } |
| 129 |
| 130 |
| 131 // Counts the total number of registered benchmarks. Useful for |
| 132 // showing progress as a percentage. |
| 133 BenchmarkSuite.CountBenchmarks = function() { |
| 134 var result = 0; |
| 135 var suites = BenchmarkSuite.suites; |
| 136 for (var i = 0; i < suites.length; i++) { |
| 137 result += suites[i].benchmarks.length; |
| 138 } |
| 139 return result; |
| 140 } |
| 141 |
| 142 |
| 143 // Computes the geometric mean of a set of numbers. |
| 144 BenchmarkSuite.GeometricMean = function(numbers) { |
| 145 var log = 0; |
| 146 for (var i = 0; i < numbers.length; i++) { |
| 147 log += Math.log(numbers[i]); |
| 148 } |
| 149 return Math.pow(Math.E, log / numbers.length); |
| 150 } |
| 151 |
| 152 |
| 153 // Converts a score value to a string with at least three significant |
| 154 // digits. |
| 155 BenchmarkSuite.FormatScore = function(value) { |
| 156 if (value > 100) { |
| 157 return value.toFixed(0); |
| 158 } else { |
| 159 return value.toPrecision(3); |
| 160 } |
| 161 } |
| 162 |
| 163 // Notifies the runner that we're done running a single benchmark in |
| 164 // the benchmark suite. This can be useful to report progress. |
| 165 BenchmarkSuite.prototype.NotifyStep = function(result) { |
| 166 this.results.push(result); |
| 167 if (this.runner.NotifyStep) this.runner.NotifyStep(result.benchmark.name); |
| 168 } |
| 169 |
| 170 |
| 171 // Notifies the runner that we're done with running a suite and that |
| 172 // we have a result which can be reported to the user if needed. |
| 173 BenchmarkSuite.prototype.NotifyResult = function() { |
| 174 var mean = BenchmarkSuite.GeometricMean(this.results); |
| 175 var score = this.reference / mean; |
| 176 BenchmarkSuite.scores.push(score); |
| 177 if (this.runner.NotifyResult) { |
| 178 var formatted = BenchmarkSuite.FormatScore(100 * score); |
| 179 this.runner.NotifyResult(this.name, formatted); |
| 180 } |
| 181 } |
| 182 |
| 183 |
| 184 // Notifies the runner that running a benchmark resulted in an error. |
| 185 BenchmarkSuite.prototype.NotifyError = function(error) { |
| 186 if (this.runner.NotifyError) { |
| 187 this.runner.NotifyError(this.name, error); |
| 188 } |
| 189 if (this.runner.NotifyStep) { |
| 190 this.runner.NotifyStep(this.name); |
| 191 } |
| 192 } |
| 193 |
| 194 |
| 195 // Runs a single benchmark for at least a second and computes the |
| 196 // average time it takes to run a single iteration. |
| 197 BenchmarkSuite.prototype.RunSingle = function(benchmark) { |
| 198 var elapsed = 0; |
| 199 var start = new Date(); |
| 200 for (var n = 0; elapsed < 1000; n++) { |
| 201 benchmark.run(); |
| 202 elapsed = new Date() - start; |
| 203 } |
| 204 var usec = (elapsed * 1000) / n; |
| 205 this.NotifyStep(new BenchmarkResult(benchmark, usec)); |
| 206 } |
| 207 |
| 208 |
| 209 // This function starts running a suite, but stops between each |
| 210 // individual benchmark in the suite and returns a continuation |
| 211 // function which can be invoked to run the next benchmark. Once the |
| 212 // last benchmark has been executed, null is returned. |
| 213 BenchmarkSuite.prototype.RunStep = function(runner) { |
| 214 this.results = []; |
| 215 this.runner = runner; |
| 216 var length = this.benchmarks.length; |
| 217 var index = 0; |
| 218 var suite = this; |
| 219 function RunNext() { |
| 220 if (index < length) { |
| 221 try { |
| 222 suite.RunSingle(suite.benchmarks[index++]); |
| 223 } catch (e) { |
| 224 suite.NotifyError(e); |
| 225 return null; |
| 226 } |
| 227 return RunNext; |
| 228 } |
| 229 suite.NotifyResult(); |
| 230 return null; |
| 231 } |
| 232 return RunNext(); |
| 233 } |
OLD | NEW |