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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 this.runner.NotifyError(this.name, error); | 191 this.runner.NotifyError(this.name, error); |
192 } | 192 } |
193 if (this.runner.NotifyStep) { | 193 if (this.runner.NotifyStep) { |
194 this.runner.NotifyStep(this.name); | 194 this.runner.NotifyStep(this.name); |
195 } | 195 } |
196 } | 196 } |
197 | 197 |
198 | 198 |
199 // Runs a single benchmark for at least a second and computes the | 199 // Runs a single benchmark for at least a second and computes the |
200 // average time it takes to run a single iteration. | 200 // average time it takes to run a single iteration. |
201 BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark) { | 201 BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark, data) { |
202 var elapsed = 0; | 202 function Measure(data) { |
203 var start = new Date(); | 203 var elapsed = 0; |
204 for (var n = 0; elapsed < 1000; n++) { | 204 var start = new Date(); |
205 benchmark.run(); | 205 for (var n = 0; elapsed < 1000; n++) { |
206 elapsed = new Date() - start; | 206 benchmark.run(); |
| 207 elapsed = new Date() - start; |
| 208 } |
| 209 if (data != null) { |
| 210 data.runs += n; |
| 211 data.elapsed += elapsed; |
| 212 } |
207 } | 213 } |
208 var usec = (elapsed * 1000) / n; | 214 |
209 this.NotifyStep(new BenchmarkResult(benchmark, usec)); | 215 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); |
| 219 return { runs: 0, elapsed: 0 }; |
| 220 } else { |
| 221 Measure(data); |
| 222 // If we've run too few iterations, we continue for another second. |
| 223 if (data.runs < 32) return data; |
| 224 var usec = (data.elapsed * 1000) / data.runs; |
| 225 this.NotifyStep(new BenchmarkResult(benchmark, usec)); |
| 226 return null; |
| 227 } |
210 } | 228 } |
211 | 229 |
212 | 230 |
213 // This function starts running a suite, but stops between each | 231 // This function starts running a suite, but stops between each |
214 // individual benchmark in the suite and returns a continuation | 232 // individual benchmark in the suite and returns a continuation |
215 // function which can be invoked to run the next benchmark. Once the | 233 // function which can be invoked to run the next benchmark. Once the |
216 // last benchmark has been executed, null is returned. | 234 // last benchmark has been executed, null is returned. |
217 BenchmarkSuite.prototype.RunStep = function(runner) { | 235 BenchmarkSuite.prototype.RunStep = function(runner) { |
218 this.results = []; | 236 this.results = []; |
219 this.runner = runner; | 237 this.runner = runner; |
220 var length = this.benchmarks.length; | 238 var length = this.benchmarks.length; |
221 var index = 0; | 239 var index = 0; |
222 var suite = this; | 240 var suite = this; |
| 241 var data; |
223 | 242 |
224 // Run the setup, the actual benchmark, and the tear down in three | 243 // Run the setup, the actual benchmark, and the tear down in three |
225 // separate steps to allow the framework to yield between any of the | 244 // separate steps to allow the framework to yield between any of the |
226 // steps. | 245 // steps. |
227 | 246 |
228 function RunNextSetup() { | 247 function RunNextSetup() { |
229 if (index < length) { | 248 if (index < length) { |
230 try { | 249 try { |
231 suite.benchmarks[index].Setup(); | 250 suite.benchmarks[index].Setup(); |
232 } catch (e) { | 251 } catch (e) { |
233 suite.NotifyError(e); | 252 suite.NotifyError(e); |
234 return null; | 253 return null; |
235 } | 254 } |
236 return RunNextBenchmark; | 255 return RunNextBenchmark; |
237 } | 256 } |
238 suite.NotifyResult(); | 257 suite.NotifyResult(); |
239 return null; | 258 return null; |
240 } | 259 } |
241 | 260 |
242 function RunNextBenchmark() { | 261 function RunNextBenchmark() { |
243 try { | 262 try { |
244 suite.RunSingleBenchmark(suite.benchmarks[index]); | 263 data = suite.RunSingleBenchmark(suite.benchmarks[index], data); |
245 } catch (e) { | 264 } catch (e) { |
246 suite.NotifyError(e); | 265 suite.NotifyError(e); |
247 return null; | 266 return null; |
248 } | 267 } |
249 return RunNextTearDown; | 268 // If data is null, we're done with this benchmark. |
| 269 return (data == null) ? RunNextTearDown : RunNextBenchmark(); |
250 } | 270 } |
251 | 271 |
252 function RunNextTearDown() { | 272 function RunNextTearDown() { |
253 try { | 273 try { |
254 suite.benchmarks[index++].TearDown(); | 274 suite.benchmarks[index++].TearDown(); |
255 } catch (e) { | 275 } catch (e) { |
256 suite.NotifyError(e); | 276 suite.NotifyError(e); |
257 return null; | 277 return null; |
258 } | 278 } |
259 return RunNextSetup; | 279 return RunNextSetup; |
260 } | 280 } |
261 | 281 |
262 // Start out running the setup. | 282 // Start out running the setup. |
263 return RunNextSetup(); | 283 return RunNextSetup(); |
264 } | 284 } |
OLD | NEW |