3131
3232
3333// A benchmark has a name (string) and a function that will be run to
34- // do the performance measurement.
35- function Benchmark ( name , run ) {
34+ // do the performance measurement. The optional setup and tearDown
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 ) {
3639 this . name = name ;
3740 this . run = run ;
41+ this . Setup = setup ? setup : function ( ) { } ;
42+ this . TearDown = tearDown ? tearDown : function ( ) { } ;
3843}
3944
4045
@@ -73,7 +78,7 @@ BenchmarkSuite.suites = [];
7378// Scores are not comparable across versions. Bump the version if
7479// you're making changes that will affect that scores, e.g. if you add
7580// a new benchmark or change an existing one.
76- BenchmarkSuite . version = '3 ' ;
81+ BenchmarkSuite . version = '5 ' ;
7782
7883
7984// To make the benchmark results predictable, we replace Math.random
@@ -86,7 +91,6 @@ Math.random = (function() {
8691 seed = ( ( seed ^ 0xc761c23c ) ^ ( seed >>> 19 ) ) & 0xffffffff ;
8792 seed = ( ( seed + 0x165667b1 ) + ( seed << 5 ) ) & 0xffffffff ;
8893 seed = ( ( seed + 0xd3a2646c ) ^ ( seed << 9 ) ) & 0xffffffff ;
89- seed = ( ( seed + 0xd3a2646c ) ^ ( seed << 9 ) ) & 0xffffffff ;
9094 seed = ( ( seed + 0xfd7046c5 ) + ( seed << 3 ) ) & 0xffffffff ;
9195 seed = ( ( seed ^ 0xb55a4f09 ) ^ ( seed >>> 16 ) ) & 0xffffffff ;
9296 return ( seed & 0xfffffff ) / 0x10000000 ;
@@ -114,7 +118,7 @@ BenchmarkSuite.RunSuites = function(runner) {
114118 continuation = suite . RunStep ( runner ) ;
115119 }
116120 if ( continuation && typeof window != 'undefined' && window . setTimeout ) {
117- window . setTimeout ( RunStep , 100 ) ;
121+ window . setTimeout ( RunStep , 25 ) ;
118122 return ;
119123 }
120124 }
@@ -194,7 +198,7 @@ BenchmarkSuite.prototype.NotifyError = function(error) {
194198
195199// Runs a single benchmark for at least a second and computes the
196200// average time it takes to run a single iteration.
197- BenchmarkSuite . prototype . RunSingle = function ( benchmark ) {
201+ BenchmarkSuite . prototype . RunSingleBenchmark = function ( benchmark ) {
198202 var elapsed = 0 ;
199203 var start = new Date ( ) ;
200204 for ( var n = 0 ; elapsed < 1000 ; n ++ ) {
@@ -216,18 +220,45 @@ BenchmarkSuite.prototype.RunStep = function(runner) {
216220 var length = this . benchmarks . length ;
217221 var index = 0 ;
218222 var suite = this ;
219- function RunNext ( ) {
223+
224+ // 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
226+ // steps.
227+
228+ function RunNextSetup ( ) {
220229 if ( index < length ) {
221230 try {
222- suite . RunSingle ( suite . benchmarks [ index ++ ] ) ;
231+ suite . benchmarks [ index ] . Setup ( ) ;
223232 } catch ( e ) {
224233 suite . NotifyError ( e ) ;
225234 return null ;
226235 }
227- return RunNext ;
236+ return RunNextBenchmark ;
228237 }
229238 suite . NotifyResult ( ) ;
230239 return null ;
231240 }
232- return RunNext ( ) ;
241+
242+ function RunNextBenchmark ( ) {
243+ try {
244+ suite . RunSingleBenchmark ( suite . benchmarks [ index ] ) ;
245+ } catch ( e ) {
246+ suite . NotifyError ( e ) ;
247+ return null ;
248+ }
249+ return RunNextTearDown ;
250+ }
251+
252+ function RunNextTearDown ( ) {
253+ try {
254+ suite . benchmarks [ index ++ ] . TearDown ( ) ;
255+ } catch ( e ) {
256+ suite . NotifyError ( e ) ;
257+ return null ;
258+ }
259+ return RunNextSetup ;
260+ }
261+
262+ // Start out running the setup.
263+ return RunNextSetup ( ) ;
233264}
0 commit comments