@@ -198,15 +198,33 @@ BenchmarkSuite.prototype.NotifyError = function(error) {
198198
199199// Runs a single benchmark for at least a second and computes the
200200// average time it takes to run a single iteration.
201- BenchmarkSuite . prototype . RunSingleBenchmark = function ( benchmark ) {
202- var elapsed = 0 ;
203- var start = new Date ( ) ;
204- for ( var n = 0 ; elapsed < 1000 ; n ++ ) {
205- benchmark . run ( ) ;
206- elapsed = new Date ( ) - start ;
201+ BenchmarkSuite . prototype . RunSingleBenchmark = function ( benchmark , data ) {
202+ function Measure ( data ) {
203+ var elapsed = 0 ;
204+ var start = new Date ( ) ;
205+ for ( var n = 0 ; elapsed < 1000 ; n ++ ) {
206+ benchmark . run ( ) ;
207+ elapsed = new Date ( ) - start ;
208+ }
209+ if ( data != null ) {
210+ data . runs += n ;
211+ data . elapsed += elapsed ;
212+ }
213+ }
214+
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 ;
207227 }
208- var usec = ( elapsed * 1000 ) / n ;
209- this . NotifyStep ( new BenchmarkResult ( benchmark , usec ) ) ;
210228}
211229
212230
@@ -220,6 +238,7 @@ BenchmarkSuite.prototype.RunStep = function(runner) {
220238 var length = this . benchmarks . length ;
221239 var index = 0 ;
222240 var suite = this ;
241+ var data ;
223242
224243 // Run the setup, the actual benchmark, and the tear down in three
225244 // separate steps to allow the framework to yield between any of the
@@ -241,12 +260,13 @@ BenchmarkSuite.prototype.RunStep = function(runner) {
241260
242261 function RunNextBenchmark ( ) {
243262 try {
244- suite . RunSingleBenchmark ( suite . benchmarks [ index ] ) ;
263+ data = suite . RunSingleBenchmark ( suite . benchmarks [ index ] , data ) ;
245264 } catch ( e ) {
246265 suite . NotifyError ( e ) ;
247266 return null ;
248267 }
249- return RunNextTearDown ;
268+ // If data is null, we're done with this benchmark.
269+ return ( data == null ) ? RunNextTearDown : RunNextBenchmark ( ) ;
250270 }
251271
252272 function RunNextTearDown ( ) {
0 commit comments