OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // modification, are permitted provided that the following conditions are | 3 // found in the LICENSE file. |
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 | 4 |
28 | 5 |
| 6 // Performance.now is used in latency benchmarks, the fallback is Date.now. |
| 7 var performance = performance || {}; |
| 8 performance.now = (function() { |
| 9 return performance.now || |
| 10 performance.mozNow || |
| 11 performance.msNow || |
| 12 performance.oNow || |
| 13 performance.webkitNow || |
| 14 Date.now; |
| 15 })(); |
| 16 |
29 // Simple framework for running the benchmark suites and | 17 // Simple framework for running the benchmark suites and |
30 // computing a score based on the timing measurements. | 18 // computing a score based on the timing measurements. |
31 | 19 |
32 | 20 |
33 // A benchmark has a name (string) and a function that will be run to | 21 // A benchmark has a name (string) and a function that will be run to |
34 // do the performance measurement. The optional setup and tearDown | 22 // do the performance measurement. The optional setup and tearDown |
35 // arguments are functions that will be invoked before and after | 23 // arguments are functions that will be invoked before and after |
36 // running the benchmark, but the running time of these functions will | 24 // running the benchmark, but the running time of these functions will |
37 // not be accounted for in the benchmark score. | 25 // not be accounted for in the benchmark score. |
38 function Benchmark(name, run, setup, tearDown) { | 26 function Benchmark(name, doWarmup, doDeterministic, deterministicIterations, |
| 27 run, setup, tearDown, rmsResult, minIterations) { |
39 this.name = name; | 28 this.name = name; |
| 29 this.doWarmup = doWarmup; |
| 30 this.doDeterministic = doDeterministic; |
| 31 this.deterministicIterations = deterministicIterations; |
40 this.run = run; | 32 this.run = run; |
41 this.Setup = setup ? setup : function() { }; | 33 this.Setup = setup ? setup : function() { }; |
42 this.TearDown = tearDown ? tearDown : function() { }; | 34 this.TearDown = tearDown ? tearDown : function() { }; |
| 35 this.rmsResult = rmsResult ? rmsResult : null; |
| 36 this.minIterations = minIterations ? minIterations : 32; |
43 } | 37 } |
44 | 38 |
45 | 39 |
46 // Benchmark results hold the benchmark and the measured time used to | 40 // Benchmark results hold the benchmark and the measured time used to |
47 // run the benchmark. The benchmark score is computed later once a | 41 // run the benchmark. The benchmark score is computed later once a |
48 // full benchmark suite has run to completion. | 42 // full benchmark suite has run to completion. If latency is set to 0 |
49 function BenchmarkResult(benchmark, time) { | 43 // then there is no latency score for this benchmark. |
| 44 function BenchmarkResult(benchmark, time, latency) { |
50 this.benchmark = benchmark; | 45 this.benchmark = benchmark; |
51 this.time = time; | 46 this.time = time; |
| 47 this.latency = latency; |
52 } | 48 } |
53 | 49 |
54 | 50 |
55 // Automatically convert results to numbers. Used by the geometric | 51 // Automatically convert results to numbers. Used by the geometric |
56 // mean computation. | 52 // mean computation. |
57 BenchmarkResult.prototype.valueOf = function() { | 53 BenchmarkResult.prototype.valueOf = function() { |
58 return this.time; | 54 return this.time; |
59 } | 55 } |
60 | 56 |
61 | 57 |
62 // Suites of benchmarks consist of a name and the set of benchmarks in | 58 // Suites of benchmarks consist of a name and the set of benchmarks in |
63 // addition to the reference timing that the final score will be based | 59 // addition to the reference timing that the final score will be based |
64 // on. This way, all scores are relative to a reference run and higher | 60 // on. This way, all scores are relative to a reference run and higher |
65 // scores implies better performance. | 61 // scores implies better performance. |
66 function BenchmarkSuite(name, reference, benchmarks) { | 62 function BenchmarkSuite(name, reference, benchmarks) { |
67 this.name = name; | 63 this.name = name; |
68 this.reference = reference; | 64 this.reference = reference; |
69 this.benchmarks = benchmarks; | 65 this.benchmarks = benchmarks; |
70 BenchmarkSuite.suites.push(this); | 66 BenchmarkSuite.suites.push(this); |
71 } | 67 } |
72 | 68 |
73 | 69 |
74 // Keep track of all declared benchmark suites. | 70 // Keep track of all declared benchmark suites. |
75 BenchmarkSuite.suites = []; | 71 BenchmarkSuite.suites = []; |
76 | 72 |
77 | |
78 // Scores are not comparable across versions. Bump the version if | 73 // Scores are not comparable across versions. Bump the version if |
79 // you're making changes that will affect that scores, e.g. if you add | 74 // you're making changes that will affect that scores, e.g. if you add |
80 // a new benchmark or change an existing one. | 75 // a new benchmark or change an existing one. |
81 BenchmarkSuite.version = '7'; | 76 BenchmarkSuite.version = '1'; |
| 77 |
| 78 |
| 79 // Defines global benchsuite running mode that overrides benchmark suite |
| 80 // behavior. Intended to be set by the benchmark driver. Undefined |
| 81 // values here allow a benchmark to define behaviour itself. |
| 82 BenchmarkSuite.config = { |
| 83 doWarmup: undefined, |
| 84 doDeterministic: undefined |
| 85 }; |
| 86 |
| 87 |
| 88 // Override the alert function to throw an exception instead. |
| 89 alert = function(s) { |
| 90 throw "Alert called with argument: " + s; |
| 91 }; |
82 | 92 |
83 | 93 |
84 // To make the benchmark results predictable, we replace Math.random | 94 // To make the benchmark results predictable, we replace Math.random |
85 // with a 100% deterministic alternative. | 95 // with a 100% deterministic alternative. |
86 Math.random = (function() { | 96 BenchmarkSuite.ResetRNG = function() { |
87 var seed = 49734321; | 97 Math.random = (function() { |
88 return function() { | 98 var seed = 49734321; |
89 // Robert Jenkins' 32 bit integer hash function. | 99 return function() { |
90 seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff; | 100 // Robert Jenkins' 32 bit integer hash function. |
91 seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff; | 101 seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff; |
92 seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff; | 102 seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff; |
93 seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff; | 103 seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff; |
94 seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff; | 104 seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff; |
95 seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff; | 105 seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff; |
96 return (seed & 0xfffffff) / 0x10000000; | 106 seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff; |
97 }; | 107 return (seed & 0xfffffff) / 0x10000000; |
98 })(); | 108 }; |
| 109 })(); |
| 110 } |
99 | 111 |
100 | 112 |
101 // Runs all registered benchmark suites and optionally yields between | 113 // Runs all registered benchmark suites and optionally yields between |
102 // each individual benchmark to avoid running for too long in the | 114 // each individual benchmark to avoid running for too long in the |
103 // context of browsers. Once done, the final score is reported to the | 115 // context of browsers. Once done, the final score is reported to the |
104 // runner. | 116 // runner. |
105 BenchmarkSuite.RunSuites = function(runner) { | 117 BenchmarkSuite.RunSuites = function(runner, skipBenchmarks) { |
| 118 skipBenchmarks = typeof skipBenchmarks === 'undefined' ? [] : skipBenchmarks; |
106 var continuation = null; | 119 var continuation = null; |
107 var suites = BenchmarkSuite.suites; | 120 var suites = BenchmarkSuite.suites; |
108 var length = suites.length; | 121 var length = suites.length; |
109 BenchmarkSuite.scores = []; | 122 BenchmarkSuite.scores = []; |
110 var index = 0; | 123 var index = 0; |
111 function RunStep() { | 124 function RunStep() { |
112 while (continuation || index < length) { | 125 while (continuation || index < length) { |
113 if (continuation) { | 126 if (continuation) { |
114 continuation = continuation(); | 127 continuation = continuation(); |
115 } else { | 128 } else { |
116 var suite = suites[index++]; | 129 var suite = suites[index++]; |
117 if (runner.NotifyStart) runner.NotifyStart(suite.name); | 130 if (runner.NotifyStart) runner.NotifyStart(suite.name); |
118 continuation = suite.RunStep(runner); | 131 if (skipBenchmarks.indexOf(suite.name) > -1) { |
| 132 suite.NotifySkipped(runner); |
| 133 } else { |
| 134 continuation = suite.RunStep(runner); |
| 135 } |
119 } | 136 } |
120 if (continuation && typeof window != 'undefined' && window.setTimeout) { | 137 if (continuation && typeof window != 'undefined' && window.setTimeout) { |
121 window.setTimeout(RunStep, 25); | 138 window.setTimeout(RunStep, 25); |
122 return; | 139 return; |
123 } | 140 } |
124 } | 141 } |
| 142 |
| 143 // show final result |
125 if (runner.NotifyScore) { | 144 if (runner.NotifyScore) { |
126 var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores); | 145 var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores); |
127 var formatted = BenchmarkSuite.FormatScore(100 * score); | 146 var formatted = BenchmarkSuite.FormatScore(100 * score); |
128 runner.NotifyScore(formatted); | 147 runner.NotifyScore(formatted); |
129 } | 148 } |
130 } | 149 } |
131 RunStep(); | 150 RunStep(); |
132 } | 151 } |
133 | 152 |
134 | 153 |
(...skipping 12 matching lines...) Expand all Loading... |
147 // Computes the geometric mean of a set of numbers. | 166 // Computes the geometric mean of a set of numbers. |
148 BenchmarkSuite.GeometricMean = function(numbers) { | 167 BenchmarkSuite.GeometricMean = function(numbers) { |
149 var log = 0; | 168 var log = 0; |
150 for (var i = 0; i < numbers.length; i++) { | 169 for (var i = 0; i < numbers.length; i++) { |
151 log += Math.log(numbers[i]); | 170 log += Math.log(numbers[i]); |
152 } | 171 } |
153 return Math.pow(Math.E, log / numbers.length); | 172 return Math.pow(Math.E, log / numbers.length); |
154 } | 173 } |
155 | 174 |
156 | 175 |
| 176 // Computes the geometric mean of a set of throughput time measurements. |
| 177 BenchmarkSuite.GeometricMeanTime = function(measurements) { |
| 178 var log = 0; |
| 179 for (var i = 0; i < measurements.length; i++) { |
| 180 log += Math.log(measurements[i].time); |
| 181 } |
| 182 return Math.pow(Math.E, log / measurements.length); |
| 183 } |
| 184 |
| 185 |
| 186 // Computes the geometric mean of a set of rms measurements. |
| 187 BenchmarkSuite.GeometricMeanLatency = function(measurements) { |
| 188 var log = 0; |
| 189 var hasLatencyResult = false; |
| 190 for (var i = 0; i < measurements.length; i++) { |
| 191 if (measurements[i].latency != 0) { |
| 192 log += Math.log(measurements[i].latency); |
| 193 hasLatencyResult = true; |
| 194 } |
| 195 } |
| 196 if (hasLatencyResult) { |
| 197 return Math.pow(Math.E, log / measurements.length); |
| 198 } else { |
| 199 return 0; |
| 200 } |
| 201 } |
| 202 |
| 203 |
157 // Converts a score value to a string with at least three significant | 204 // Converts a score value to a string with at least three significant |
158 // digits. | 205 // digits. |
159 BenchmarkSuite.FormatScore = function(value) { | 206 BenchmarkSuite.FormatScore = function(value) { |
160 if (value > 100) { | 207 if (value > 100) { |
161 return value.toFixed(0); | 208 return value.toFixed(0); |
162 } else { | 209 } else { |
163 return value.toPrecision(3); | 210 return value.toPrecision(3); |
164 } | 211 } |
165 } | 212 } |
166 | 213 |
167 // Notifies the runner that we're done running a single benchmark in | 214 // Notifies the runner that we're done running a single benchmark in |
168 // the benchmark suite. This can be useful to report progress. | 215 // the benchmark suite. This can be useful to report progress. |
169 BenchmarkSuite.prototype.NotifyStep = function(result) { | 216 BenchmarkSuite.prototype.NotifyStep = function(result) { |
170 this.results.push(result); | 217 this.results.push(result); |
171 if (this.runner.NotifyStep) this.runner.NotifyStep(result.benchmark.name); | 218 if (this.runner.NotifyStep) this.runner.NotifyStep(result.benchmark.name); |
172 } | 219 } |
173 | 220 |
174 | 221 |
175 // Notifies the runner that we're done with running a suite and that | 222 // Notifies the runner that we're done with running a suite and that |
176 // we have a result which can be reported to the user if needed. | 223 // we have a result which can be reported to the user if needed. |
177 BenchmarkSuite.prototype.NotifyResult = function() { | 224 BenchmarkSuite.prototype.NotifyResult = function() { |
178 var mean = BenchmarkSuite.GeometricMean(this.results); | 225 var mean = BenchmarkSuite.GeometricMeanTime(this.results); |
179 var score = this.reference / mean; | 226 var score = this.reference[0] / mean; |
180 BenchmarkSuite.scores.push(score); | 227 BenchmarkSuite.scores.push(score); |
181 if (this.runner.NotifyResult) { | 228 if (this.runner.NotifyResult) { |
182 var formatted = BenchmarkSuite.FormatScore(100 * score); | 229 var formatted = BenchmarkSuite.FormatScore(100 * score); |
183 this.runner.NotifyResult(this.name, formatted); | 230 this.runner.NotifyResult(this.name, formatted); |
184 } | 231 } |
| 232 if (this.reference.length == 2) { |
| 233 var meanLatency = BenchmarkSuite.GeometricMeanLatency(this.results); |
| 234 if (meanLatency != 0) { |
| 235 var scoreLatency = this.reference[1] / meanLatency; |
| 236 BenchmarkSuite.scores.push(scoreLatency); |
| 237 if (this.runner.NotifyResult) { |
| 238 var formattedLatency = BenchmarkSuite.FormatScore(100 * scoreLatency) |
| 239 this.runner.NotifyResult(this.name + "Latency", formattedLatency); |
| 240 } |
| 241 } |
| 242 } |
185 } | 243 } |
186 | 244 |
187 | 245 |
| 246 BenchmarkSuite.prototype.NotifySkipped = function(runner) { |
| 247 BenchmarkSuite.scores.push(1); // push default reference score. |
| 248 if (runner.NotifyResult) { |
| 249 runner.NotifyResult(this.name, "Skipped"); |
| 250 } |
| 251 } |
| 252 |
| 253 |
188 // Notifies the runner that running a benchmark resulted in an error. | 254 // Notifies the runner that running a benchmark resulted in an error. |
189 BenchmarkSuite.prototype.NotifyError = function(error) { | 255 BenchmarkSuite.prototype.NotifyError = function(error) { |
190 if (this.runner.NotifyError) { | 256 if (this.runner.NotifyError) { |
191 this.runner.NotifyError(this.name, error); | 257 this.runner.NotifyError(this.name, error); |
192 } | 258 } |
193 if (this.runner.NotifyStep) { | 259 if (this.runner.NotifyStep) { |
194 this.runner.NotifyStep(this.name); | 260 this.runner.NotifyStep(this.name); |
195 } | 261 } |
196 } | 262 } |
197 | 263 |
198 | 264 |
199 // Runs a single benchmark for at least a second and computes the | 265 // Runs a single benchmark for at least a second and computes the |
200 // average time it takes to run a single iteration. | 266 // average time it takes to run a single iteration. |
201 BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark, data) { | 267 BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark, data) { |
| 268 var config = BenchmarkSuite.config; |
| 269 var doWarmup = config.doWarmup !== undefined |
| 270 ? config.doWarmup |
| 271 : benchmark.doWarmup; |
| 272 var doDeterministic = config.doDeterministic !== undefined |
| 273 ? config.doDeterministic |
| 274 : benchmark.doDeterministic; |
| 275 |
202 function Measure(data) { | 276 function Measure(data) { |
203 var elapsed = 0; | 277 var elapsed = 0; |
204 var start = new Date(); | 278 var start = new Date(); |
205 for (var n = 0; elapsed < 1000; n++) { | 279 |
| 280 // Run either for 1 second or for the number of iterations specified |
| 281 // by minIterations, depending on the config flag doDeterministic. |
| 282 for (var i = 0; (doDeterministic ? |
| 283 i<benchmark.deterministicIterations : elapsed < 1000); i++) { |
206 benchmark.run(); | 284 benchmark.run(); |
207 elapsed = new Date() - start; | 285 elapsed = new Date() - start; |
208 } | 286 } |
209 if (data != null) { | 287 if (data != null) { |
210 data.runs += n; | 288 data.runs += i; |
211 data.elapsed += elapsed; | 289 data.elapsed += elapsed; |
212 } | 290 } |
213 } | 291 } |
214 | 292 |
| 293 // Sets up data in order to skip or not the warmup phase. |
| 294 if (!doWarmup && data == null) { |
| 295 data = { runs: 0, elapsed: 0 }; |
| 296 } |
| 297 |
215 if (data == null) { | 298 if (data == null) { |
216 // Measure the benchmark once for warm up and throw the result | |
217 // away. Return a fresh data object. | |
218 Measure(null); | 299 Measure(null); |
219 return { runs: 0, elapsed: 0 }; | 300 return { runs: 0, elapsed: 0 }; |
220 } else { | 301 } else { |
221 Measure(data); | 302 Measure(data); |
222 // If we've run too few iterations, we continue for another second. | 303 // If we've run too few iterations, we continue for another second. |
223 if (data.runs < 32) return data; | 304 if (data.runs < benchmark.minIterations) return data; |
224 var usec = (data.elapsed * 1000) / data.runs; | 305 var usec = (data.elapsed * 1000) / data.runs; |
225 this.NotifyStep(new BenchmarkResult(benchmark, usec)); | 306 var rms = (benchmark.rmsResult != null) ? benchmark.rmsResult() : 0; |
| 307 this.NotifyStep(new BenchmarkResult(benchmark, usec, rms)); |
226 return null; | 308 return null; |
227 } | 309 } |
228 } | 310 } |
229 | 311 |
230 | 312 |
231 // This function starts running a suite, but stops between each | 313 // This function starts running a suite, but stops between each |
232 // individual benchmark in the suite and returns a continuation | 314 // individual benchmark in the suite and returns a continuation |
233 // function which can be invoked to run the next benchmark. Once the | 315 // function which can be invoked to run the next benchmark. Once the |
234 // last benchmark has been executed, null is returned. | 316 // last benchmark has been executed, null is returned. |
235 BenchmarkSuite.prototype.RunStep = function(runner) { | 317 BenchmarkSuite.prototype.RunStep = function(runner) { |
| 318 BenchmarkSuite.ResetRNG(); |
236 this.results = []; | 319 this.results = []; |
237 this.runner = runner; | 320 this.runner = runner; |
238 var length = this.benchmarks.length; | 321 var length = this.benchmarks.length; |
239 var index = 0; | 322 var index = 0; |
240 var suite = this; | 323 var suite = this; |
241 var data; | 324 var data; |
242 | 325 |
243 // Run the setup, the actual benchmark, and the tear down in three | 326 // Run the setup, the actual benchmark, and the tear down in three |
244 // separate steps to allow the framework to yield between any of the | 327 // separate steps to allow the framework to yield between any of the |
245 // steps. | 328 // steps. |
(...skipping 29 matching lines...) Expand all Loading... |
275 } catch (e) { | 358 } catch (e) { |
276 suite.NotifyError(e); | 359 suite.NotifyError(e); |
277 return null; | 360 return null; |
278 } | 361 } |
279 return RunNextSetup; | 362 return RunNextSetup; |
280 } | 363 } |
281 | 364 |
282 // Start out running the setup. | 365 // Start out running the setup. |
283 return RunNextSetup(); | 366 return RunNextSetup(); |
284 } | 367 } |
OLD | NEW |