OLD | NEW |
| (Empty) |
1 // An version of Dromaeo's original htmlrunner adapted for the | |
2 // Dart-based test driver. | |
3 | |
4 var _operations = []; | |
5 var _N_RUNS = 5; | |
6 var _nRanTests = 0; | |
7 var _nTests = 0; | |
8 var _T_DISTRIBUTION = 2.776; | |
9 | |
10 function startTest() { | |
11 window.addEventListener( | |
12 'message', | |
13 function (event) { | |
14 if (event.data == 'start') { | |
15 _run(); | |
16 } else { | |
17 window.alert('Unknown command: ' + event.data); | |
18 } | |
19 }, | |
20 false); | |
21 } | |
22 | |
23 function _run() { | |
24 var currentOperation = 0; | |
25 function handler() { | |
26 if (currentOperation < _operations.length) { | |
27 _operations[currentOperation](); | |
28 currentOperation++; | |
29 window.setTimeout(handler, 1); | |
30 } else { | |
31 _postMessage('over'); | |
32 } | |
33 } | |
34 window.setTimeout(handler, 0); | |
35 } | |
36 | |
37 function _postMessage(command, data) { | |
38 var payload = { 'command': command }; | |
39 if (data) { | |
40 payload['data'] = data; | |
41 } | |
42 window.parent.postMessage(JSON.stringify(payload), '*'); | |
43 } | |
44 | |
45 function test(name, fn) { | |
46 _nTests++; | |
47 _operations.push(function () { | |
48 // List of number of runs in seconds. | |
49 var runsPerSecond = []; | |
50 // Run the test several times. | |
51 try { | |
52 // TODO(antonm): use .setTimeout to schedule next run as JS | |
53 // version does. That allows to report the intermediate results | |
54 // more smoothly as well. | |
55 for (var i = 0; i < _N_RUNS; i++) { | |
56 var runs = 0; | |
57 var start = Date.now(); | |
58 | |
59 var cur = Date.now(); | |
60 while ((cur - start) < 1000) { | |
61 fn(); | |
62 cur = Date.now(); | |
63 runs++; | |
64 } | |
65 | |
66 runsPerSecond.push((runs * 1000.0) / (cur - start)); | |
67 } | |
68 } catch(e) { | |
69 window.alert('Exception : ' + e); | |
70 return; | |
71 } | |
72 _reportTestResults(name, runsPerSecond); | |
73 }); | |
74 } | |
75 | |
76 // Adapted from Dromaeo's webrunner. | |
77 function _compute(times){ | |
78 var results = {runs: times.length}, num = times.length; | |
79 | |
80 times = times.sort(function(a,b){ | |
81 return a - b; | |
82 }); | |
83 | |
84 // Make Sum | |
85 results.sum = 0; | |
86 | |
87 for ( var i = 0; i < num; i++ ) | |
88 results.sum += times[i]; | |
89 | |
90 // Make Min | |
91 results.min = times[0]; | |
92 | |
93 // Make Max | |
94 results.max = times[ num - 1 ]; | |
95 | |
96 // Make Mean | |
97 results.mean = results.sum / num; | |
98 | |
99 // Make Median | |
100 results.median = num % 2 == 0 ? | |
101 (times[Math.floor(num/2)] + times[Math.ceil(num/2)]) / 2 : | |
102 times[Math.round(num/2)]; | |
103 | |
104 // Make Variance | |
105 results.variance = 0; | |
106 | |
107 for ( var i = 0; i < num; i++ ) | |
108 results.variance += Math.pow(times[i] - results.mean, 2); | |
109 | |
110 results.variance /= num - 1; | |
111 | |
112 // Make Standard Deviation | |
113 results.deviation = Math.sqrt( results.variance ); | |
114 | |
115 // Compute Standard Errors Mean | |
116 results.sem = (results.deviation / Math.sqrt(results.runs)) * _T_DISTRIBUTION; | |
117 | |
118 // Error | |
119 results.error = ((results.sem / results.mean) * 100) || 0; | |
120 | |
121 return results; | |
122 } | |
123 | |
124 function _reportTestResults(name, times) { | |
125 _nRanTests++; | |
126 var results = _compute(times); | |
127 | |
128 _postMessage('result', { | |
129 'testName': name, | |
130 'mean': results.mean, | |
131 'error': results.error, | |
132 'percent': (100.0 * _nRanTests / _nTests) | |
133 }); | |
134 } | |
135 | |
136 function endTest() { | |
137 _postMessage('inited', { 'nTests': _nTests }); | |
138 } | |
139 | |
140 function prep(fn) { | |
141 _operations.push(fn); | |
142 } | |
OLD | NEW |