1 // There are tests for computeStatistics() located in LayoutTests/fast/harness/perftests
3 var PerfTestRunner = {};
5 // To make the benchmark results predictable, we replace Math.random with a
6 // 100% deterministic alternative.
7 PerfTestRunner.randomSeed = PerfTestRunner.initialRandomSeed = 49734321;
9 PerfTestRunner.resetRandomSeed = function() {
10 PerfTestRunner.randomSeed = PerfTestRunner.initialRandomSeed
13 PerfTestRunner.random = Math.random = function() {
14 // Robert Jenkins' 32 bit integer hash function.
15 var randomSeed = PerfTestRunner.randomSeed;
16 randomSeed = ((randomSeed + 0x7ed55d16) + (randomSeed << 12)) & 0xffffffff;
17 randomSeed = ((randomSeed ^ 0xc761c23c) ^ (randomSeed >>> 19)) & 0xffffffff;
18 randomSeed = ((randomSeed + 0x165667b1) + (randomSeed << 5)) & 0xffffffff;
19 randomSeed = ((randomSeed + 0xd3a2646c) ^ (randomSeed << 9)) & 0xffffffff;
20 randomSeed = ((randomSeed + 0xfd7046c5) + (randomSeed << 3)) & 0xffffffff;
21 randomSeed = ((randomSeed ^ 0xb55a4f09) ^ (randomSeed >>> 16)) & 0xffffffff;
22 PerfTestRunner.randomSeed = randomSeed;
23 return (randomSeed & 0xfffffff) / 0x10000000;
26 PerfTestRunner.log = function (text) {
28 this._logLines.push(text);
31 if (!document.getElementById("log")) {
32 var pre = document.createElement('pre');
34 document.body.appendChild(pre);
36 document.getElementById("log").innerHTML += text + "\n";
37 window.scrollTo(0, document.body.height);
40 PerfTestRunner.info = function (text) {
41 this.log("Info: " + text);
44 PerfTestRunner.logInfo = function (text) {
45 if (!window.layoutTestController)
49 PerfTestRunner.loadFile = function (path) {
50 var xhr = new XMLHttpRequest();
51 xhr.open("GET", path, false);
53 return xhr.responseText;
56 PerfTestRunner.computeStatistics = function (times, unit) {
57 var data = times.slice();
59 // Add values from the smallest to the largest to avoid the loss of significance
60 data.sort(function(a,b){return a-b;});
62 var middle = Math.floor(data.length / 2);
65 max: data[data.length - 1],
66 median: data.length % 2 ? data[middle] : (data[middle - 1] + data[middle]) / 2,
69 // Compute the mean and variance using a numerically stable algorithm.
71 result.mean = data[0];
73 for (var i = 1; i < data.length; ++i) {
75 var delta = x - result.mean;
77 result.mean += delta / sweep;
79 squareSum += delta * delta * (i / sweep);
81 result.variance = squareSum / data.length;
82 result.stdev = Math.sqrt(result.variance);
83 result.unit = unit || "ms";
88 PerfTestRunner.logStatistics = function (times) {
90 var statistics = this.computeStatistics(times, this.unit);
91 this.printStatistics(statistics);
94 PerfTestRunner.printStatistics = function (statistics) {
96 this.log("avg " + statistics.mean + " " + statistics.unit);
97 this.log("median " + statistics.median + " " + statistics.unit);
98 this.log("stdev " + statistics.stdev + " " + statistics.unit);
99 this.log("min " + statistics.min + " " + statistics.unit);
100 this.log("max " + statistics.max + " " + statistics.unit);
103 PerfTestRunner.gc = function () {
104 if (window.GCController)
105 window.GCController.collect();
110 var temp = {i: "ab" + i + (i / 100000)};
114 for (var i = 0; i < 1000; i++)
119 PerfTestRunner._runLoop = function () {
120 if (this._completedRuns < this._runCount) {
122 window.setTimeout(function () { PerfTestRunner._runner(); }, 0);
124 this.logStatistics(this._results);
125 if (this._logLines) {
126 var logLines = this._logLines;
127 this._logLines = null;
129 logLines.forEach(function(text) { self.log(text); });
131 this._doneFunction();
132 if (window.layoutTestController)
133 layoutTestController.notifyDone();
137 PerfTestRunner._runner = function () {
138 var start = Date.now();
141 for (var i = 0; i < this._loopsPerRun; ++i) {
142 var returnValue = this._runFunction.call(window);
143 if (returnValue - 0 === returnValue) {
144 if (returnValue <= 0)
145 this.log("runFunction returned a non-positive value: " + returnValue);
146 totalTime += returnValue;
150 // Assume totalTime can never be zero when _runFunction returns a number.
151 var time = totalTime ? totalTime : Date.now() - start;
153 this.ignoreWarmUpAndLog(time);
157 PerfTestRunner.ignoreWarmUpAndLog = function (result) {
158 this._completedRuns++;
160 var labeledResult = result + " " + this.unit;
161 if (this._completedRuns <= 0)
162 this.log("Ignoring warm-up run (" + labeledResult + ")");
164 this._results.push(result);
165 this.log(labeledResult);
169 PerfTestRunner.initAndStartLoop = function() {
170 this._completedRuns = -1;
171 this.customRunFunction = null;
173 this._logLines = window.layoutTestController ? [] : null;
174 this.log("Running " + this._runCount + " times");
178 PerfTestRunner.run = function (runFunction, loopsPerRun, runCount, doneFunction) {
179 this._runFunction = runFunction;
180 this._loopsPerRun = loopsPerRun || 10;
181 this._runCount = runCount || 20;
182 this._doneFunction = doneFunction || function () {};
184 this.initAndStartLoop();
187 PerfTestRunner.runPerSecond = function (test) {
188 this._doneFunction = function () { if (test.done) test.done(); };
189 this._runCount = test.runCount || 20;
190 this._callsPerIteration = 1;
191 this.unit = 'runs/s';
194 this._runner = this._perSecondRunner;
195 this.initAndStartLoop();
198 PerfTestRunner._perSecondRunner = function () {
199 var timeToRun = this._test.timeToRun || 750;
202 var callsPerIteration = this._callsPerIteration;
204 if (this._test.setup)
207 while (totalTime < timeToRun) {
208 totalTime += this._perSecondRunnerIterator(callsPerIteration);
209 i += callsPerIteration;
210 if (this._completedRuns < 0 && totalTime < 100)
211 callsPerIteration = Math.max(10, 2 * callsPerIteration);
213 this._callsPerIteration = callsPerIteration;
215 this.ignoreWarmUpAndLog(i * 1000 / totalTime);
219 PerfTestRunner._perSecondRunnerIterator = function (callsPerIteration) {
220 var startTime = Date.now();
221 for (var i = 0; i < callsPerIteration; i++)
223 return Date.now() - startTime;
226 if (window.layoutTestController) {
227 layoutTestController.waitUntilDone();
228 layoutTestController.dumpAsText();