OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 13 matching lines...) Expand all Loading... |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 | 28 |
29 // Simple framework for running the benchmark suites and | 29 // Simple framework for running the benchmark suites and |
30 // computing a score based on the timing measurements. | 30 // computing a score based on the timing measurements. |
31 | 31 |
32 | 32 |
33 // A benchmark has a name (string) and a function that will be run to | 33 // A benchmark has a name (string) and a function that will be run to |
34 // do the performance measurement. | 34 // do the performance measurement. The optional setup and tearDown |
35 function Benchmark(name, run) { | 35 // arguments are functions that will be invoked before and after |
| 36 // running the benchmark, but the running time of these functions will |
| 37 // not be accounted for in the benchmark score. |
| 38 function Benchmark(name, run, setup, tearDown) { |
36 this.name = name; | 39 this.name = name; |
37 this.run = run; | 40 this.run = run; |
| 41 this.Setup = setup ? setup : function() { }; |
| 42 this.TearDown = tearDown ? tearDown : function() { }; |
38 } | 43 } |
39 | 44 |
40 | 45 |
41 // Benchmark results hold the benchmark and the measured time used to | 46 // Benchmark results hold the benchmark and the measured time used to |
42 // run the benchmark. The benchmark score is computed later once a | 47 // run the benchmark. The benchmark score is computed later once a |
43 // full benchmark suite has run to completion. | 48 // full benchmark suite has run to completion. |
44 function BenchmarkResult(benchmark, time) { | 49 function BenchmarkResult(benchmark, time) { |
45 this.benchmark = benchmark; | 50 this.benchmark = benchmark; |
46 this.time = time; | 51 this.time = time; |
47 } | 52 } |
(...skipping 18 matching lines...) Expand all Loading... |
66 } | 71 } |
67 | 72 |
68 | 73 |
69 // Keep track of all declared benchmark suites. | 74 // Keep track of all declared benchmark suites. |
70 BenchmarkSuite.suites = []; | 75 BenchmarkSuite.suites = []; |
71 | 76 |
72 | 77 |
73 // Scores are not comparable across versions. Bump the version if | 78 // 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 | 79 // you're making changes that will affect that scores, e.g. if you add |
75 // a new benchmark or change an existing one. | 80 // a new benchmark or change an existing one. |
76 BenchmarkSuite.version = '3'; | 81 BenchmarkSuite.version = '4'; |
77 | 82 |
78 | 83 |
79 // To make the benchmark results predictable, we replace Math.random | 84 // To make the benchmark results predictable, we replace Math.random |
80 // with a 100% deterministic alternative. | 85 // with a 100% deterministic alternative. |
81 Math.random = (function() { | 86 Math.random = (function() { |
82 var seed = 49734321; | 87 var seed = 49734321; |
83 return function() { | 88 return function() { |
84 // Robert Jenkins' 32 bit integer hash function. | 89 // Robert Jenkins' 32 bit integer hash function. |
85 seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff; | 90 seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff; |
86 seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff; | 91 seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff; |
(...skipping 20 matching lines...) Expand all Loading... |
107 function RunStep() { | 112 function RunStep() { |
108 while (continuation || index < length) { | 113 while (continuation || index < length) { |
109 if (continuation) { | 114 if (continuation) { |
110 continuation = continuation(); | 115 continuation = continuation(); |
111 } else { | 116 } else { |
112 var suite = suites[index++]; | 117 var suite = suites[index++]; |
113 if (runner.NotifyStart) runner.NotifyStart(suite.name); | 118 if (runner.NotifyStart) runner.NotifyStart(suite.name); |
114 continuation = suite.RunStep(runner); | 119 continuation = suite.RunStep(runner); |
115 } | 120 } |
116 if (continuation && typeof window != 'undefined' && window.setTimeout) { | 121 if (continuation && typeof window != 'undefined' && window.setTimeout) { |
117 window.setTimeout(RunStep, 100); | 122 window.setTimeout(RunStep, 25); |
118 return; | 123 return; |
119 } | 124 } |
120 } | 125 } |
121 if (runner.NotifyScore) { | 126 if (runner.NotifyScore) { |
122 var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores); | 127 var score = BenchmarkSuite.GeometricMean(BenchmarkSuite.scores); |
123 var formatted = BenchmarkSuite.FormatScore(100 * score); | 128 var formatted = BenchmarkSuite.FormatScore(100 * score); |
124 runner.NotifyScore(formatted); | 129 runner.NotifyScore(formatted); |
125 } | 130 } |
126 } | 131 } |
127 RunStep(); | 132 RunStep(); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 this.runner.NotifyError(this.name, error); | 192 this.runner.NotifyError(this.name, error); |
188 } | 193 } |
189 if (this.runner.NotifyStep) { | 194 if (this.runner.NotifyStep) { |
190 this.runner.NotifyStep(this.name); | 195 this.runner.NotifyStep(this.name); |
191 } | 196 } |
192 } | 197 } |
193 | 198 |
194 | 199 |
195 // Runs a single benchmark for at least a second and computes the | 200 // Runs a single benchmark for at least a second and computes the |
196 // average time it takes to run a single iteration. | 201 // average time it takes to run a single iteration. |
197 BenchmarkSuite.prototype.RunSingle = function(benchmark) { | 202 BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark) { |
198 var elapsed = 0; | 203 var elapsed = 0; |
199 var start = new Date(); | 204 var start = new Date(); |
200 for (var n = 0; elapsed < 1000; n++) { | 205 for (var n = 0; elapsed < 1000; n++) { |
201 benchmark.run(); | 206 benchmark.run(); |
202 elapsed = new Date() - start; | 207 elapsed = new Date() - start; |
203 } | 208 } |
204 var usec = (elapsed * 1000) / n; | 209 var usec = (elapsed * 1000) / n; |
205 this.NotifyStep(new BenchmarkResult(benchmark, usec)); | 210 this.NotifyStep(new BenchmarkResult(benchmark, usec)); |
206 } | 211 } |
207 | 212 |
208 | 213 |
209 // This function starts running a suite, but stops between each | 214 // This function starts running a suite, but stops between each |
210 // individual benchmark in the suite and returns a continuation | 215 // individual benchmark in the suite and returns a continuation |
211 // function which can be invoked to run the next benchmark. Once the | 216 // function which can be invoked to run the next benchmark. Once the |
212 // last benchmark has been executed, null is returned. | 217 // last benchmark has been executed, null is returned. |
213 BenchmarkSuite.prototype.RunStep = function(runner) { | 218 BenchmarkSuite.prototype.RunStep = function(runner) { |
214 this.results = []; | 219 this.results = []; |
215 this.runner = runner; | 220 this.runner = runner; |
216 var length = this.benchmarks.length; | 221 var length = this.benchmarks.length; |
217 var index = 0; | 222 var index = 0; |
218 var suite = this; | 223 var suite = this; |
219 function RunNext() { | 224 |
| 225 // Run the setup, the actual benchmark, and the tear down in three |
| 226 // separate steps to allow the framework to yield between any of the |
| 227 // steps. |
| 228 |
| 229 function RunNextSetup() { |
220 if (index < length) { | 230 if (index < length) { |
221 try { | 231 try { |
222 suite.RunSingle(suite.benchmarks[index++]); | 232 suite.benchmarks[index].Setup(); |
223 } catch (e) { | 233 } catch (e) { |
224 suite.NotifyError(e); | 234 suite.NotifyError(e); |
225 return null; | 235 return null; |
226 } | 236 } |
227 return RunNext; | 237 return RunNextBenchmark; |
228 } | 238 } |
229 suite.NotifyResult(); | 239 suite.NotifyResult(); |
230 return null; | 240 return null; |
231 } | 241 } |
232 return RunNext(); | 242 |
| 243 function RunNextBenchmark() { |
| 244 try { |
| 245 suite.RunSingleBenchmark(suite.benchmarks[index]); |
| 246 } catch (e) { |
| 247 suite.NotifyError(e); |
| 248 return null; |
| 249 } |
| 250 return RunNextTearDown; |
| 251 } |
| 252 |
| 253 function RunNextTearDown() { |
| 254 try { |
| 255 suite.benchmarks[index++].TearDown(); |
| 256 } catch (e) { |
| 257 suite.NotifyError(e); |
| 258 return null; |
| 259 } |
| 260 return RunNextSetup; |
| 261 } |
| 262 |
| 263 // Start out running the setup. |
| 264 return RunNextSetup(); |
233 } | 265 } |
OLD | NEW |