3 // Figure out if we need to stagger the tests, or not
4 var doDelay
= typeof window
!= "undefined" && window
.location
&& window
.location
.search
== "?delay";
6 // Get the output pre (if it exists, and if we're embedded in a runner page)
7 var pre
= typeof document
!= "undefined" && document
.getElementsByTagName
&&
8 document
.getElementsByTagName("pre")[0];
10 // The number of iterations to do
11 // and the number of seconds to wait for a timeout
12 var numTests
= 100, timeout
= !doDelay
? 20000 : 4500;
14 var title
, testName
, summary
= 0, queue
= [];
16 // Initialize a batch of tests
17 // name = The name of the test collection
18 this.startTest = function(name
){
21 if ( typeof onlyName
== "undefined" )
22 log([ "__start_report", testName
]);
25 // End the tests and finalize the report
26 this.endTest = function(){
27 // Save the summary output until all the test are complete
28 queue
.push(function(){
29 if ( typeof onlyName
== "undefined" ) {
30 log([ "__summarystats", summary
]);
31 log([ "__end_report" ]);
33 // Log the time to the special Talos function, if it exists
34 } else if ( typeof tpRecordTime
!= "undefined" )
35 tpRecordTime( summary
);
37 // Otherwise, we're only interested in the results from a single function
39 log([ "__start_report" + summary
+ "__end_report" ]);
41 // Force an application quit (for the Mozilla perf test runner)
42 if ( typeof goQuitApplication
!= "undefined" )
46 // Start running the first test
51 // name = The unique name of the test
52 // num = The 'length' of the test (length of string, # of tests, etc.)
53 // fn = A function holding the test to run
54 this.test = function(name
, num
, fn
){
55 // Done't run the test if we're limiting to just one
56 if ( typeof onlyName
== "undefined" || (name
== onlyName
&& num
== onlyNum
) ) {
57 // Don't execute the test immediately
58 queue
.push(function(){
59 doTest( name
, num
, fn
);
64 // The actual testing function (only to be called via the queue control)
65 function doTest(name
, num
, fn
){
67 var times
= [], start
, diff
, sum
= 0, min
= -1, max
= -1,
77 // We need to let the test time out
78 var testStart
= (new Date()).getTime();
80 for ( var i
= 0; i
< numTests
; i
++ ) {
81 start
= (new Date()).getTime();
83 var cur
= (new Date()).getTime();
90 if ( min
== -1 || diff
< min
)
94 if ( max
== -1 || diff
> max
)
97 // For making Median and Variance
100 // Check to see if the test has run for too long
101 if ( timeout
> 0 && cur
- testStart
> timeout
)
105 if ( typeof onlyName
== "undefined" )
106 return log( [ title
, num
, NaN
, NaN
, NaN
, NaN
, NaN
] );
108 return log( [ "__FAIL" + e
+ "__FAIL" ] );
112 mean
= sum
/ times
.length
;
114 // Keep a running summary going
118 times
= times
.sort(function(a
,b
){
122 if ( times
.length
% 2 == 0 )
123 median
= (times
[(times
.length
/2)-1] + times[times.length/2]) / 2;
125 median
= times
[(times
.length
/2)];
129 for ( var i
= 0; i
< times
.length
; i
++ )
130 variance
+= Math
.pow(times
[i
] - mean
, 2);
131 variance
/= times
.length
- 1;
133 // Make Standard Deviation
134 std
= Math
.sqrt( variance
);
136 if ( typeof onlyName
== "undefined" )
137 log( [ title
, num
, median
, mean
, min
, max
, std
] );
139 // Execute the next test
143 // Remove the next test from the queue and execute it
145 // If we're in a browser, and the user wants to delay the tests,
146 // then we should throw it in a setTimeout
147 if ( doDelay
&& typeof setTimeout
!= "undefined" )
148 setTimeout(function(){
152 // Otherwise execute the test immediately
158 function log( arr
) {
159 // If we're in an embedded runner, output to the output pre
161 pre
.innerHTML
+= arr
.join(":") + "\n";
163 // Otherwise, just document.write out the results
164 else if ( typeof document
!= "undefined" && document
.write
&& !doDelay
)
165 document
.write( arr
.join(":") + "\n" );
167 // Otherwise, we're probably in a shell of some sort
168 else if ( typeof window
== "undefined" && typeof print
!= "undefined" )
169 print( arr
.join(":") );