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) {
27 if (!document.getElementById("log")) {
28 var pre = document.createElement('pre');
30 document.body.appendChild(pre);
32 document.getElementById("log").innerHTML += text + "\n";
33 window.scrollTo(0, document.body.height);
36 PerfTestRunner.info = function (text) {
37 this.log("Info: " + text);
40 PerfTestRunner.logInfo = function (text) {
41 if (!window.layoutTestController)
45 PerfTestRunner.loadFile = function (path) {
46 var xhr = new XMLHttpRequest();
47 xhr.open("GET", path, false);
49 return xhr.responseText;
52 PerfTestRunner.computeStatistics = function (times, unit) {
53 var data = times.slice();
55 // Add values from the smallest to the largest to avoid the loss of significance
56 data.sort(function(a,b){return a-b;});
58 var middle = Math.floor(data.length / 2);
61 max: data[data.length - 1],
62 median: data.length % 2 ? data[middle] : (data[middle - 1] + data[middle]) / 2,
65 // Compute the mean and variance using a numerically stable algorithm.
67 result.mean = data[0];
69 for (var i = 1; i < data.length; ++i) {
71 var delta = x - result.mean;
73 result.mean += delta / sweep;
75 squareSum += delta * delta * (i / sweep);
77 result.variance = squareSum / data.length;
78 result.stdev = Math.sqrt(result.variance);
79 result.unit = unit || "ms";
84 PerfTestRunner.logStatistics = function (times) {
86 var statistics = this.computeStatistics(times, this.unit);
87 this.printStatistics(statistics);
90 PerfTestRunner.printStatistics = function (statistics) {
92 this.log("avg " + statistics.mean + " " + statistics.unit);
93 this.log("median " + statistics.median + " " + statistics.unit);
94 this.log("stdev " + statistics.stdev + " " + statistics.unit);
95 this.log("min " + statistics.min + " " + statistics.unit);
96 this.log("max " + statistics.max + " " + statistics.unit);
99 PerfTestRunner.gc = function () {
100 if (window.GCController)
101 window.GCController.collect();
106 var temp = {i: "ab" + i + (i / 100000)};
110 for (var i = 0; i < 1000; i++)
115 PerfTestRunner._runLoop = function () {
116 if (this._completedRuns < this._runCount) {
118 window.setTimeout(function () { PerfTestRunner._runner(); }, 0);
120 this.logStatistics(this._results);
121 this._doneFunction();
122 if (window.layoutTestController)
123 layoutTestController.notifyDone();
127 PerfTestRunner._runner = function () {
128 var start = Date.now();
131 for (var i = 0; i < this._loopsPerRun; ++i) {
132 var returnValue = this._runFunction.call(window);
133 if (returnValue - 0 === returnValue) {
134 if (returnValue <= 0)
135 this.log("runFunction returned a non-positive value: " + returnValue);
136 totalTime += returnValue;
140 // Assume totalTime can never be zero when _runFunction returns a number.
141 var time = totalTime ? totalTime : Date.now() - start;
143 this.ignoreWarmUpAndLog(time);
147 PerfTestRunner.ignoreWarmUpAndLog = function (result) {
148 this._completedRuns++;
150 var labeledResult = result + " " + this.unit;
151 if (this._completedRuns <= 0)
152 this.log("Ignoring warm-up run (" + labeledResult + ")");
154 this._results.push(result);
155 this.log(labeledResult);
159 PerfTestRunner.initAndStartLoop = function() {
160 this._completedRuns = -1;
161 this.customRunFunction = null;
163 this.log("Running " + this._runCount + " times");
167 PerfTestRunner.run = function (runFunction, loopsPerRun, runCount, doneFunction) {
168 this._runFunction = runFunction;
169 this._loopsPerRun = loopsPerRun || 10;
170 this._runCount = runCount || 20;
171 this._doneFunction = doneFunction || function () {};
173 this.initAndStartLoop();
176 PerfTestRunner.runPerSecond = function (test) {
177 this._doneFunction = function () { if (test.done) test.done(); };
178 this._runCount = test.runCount || 20;
179 this._callsPerIteration = 1;
180 this.unit = 'runs/s';
183 this._runner = this._perSecondRunner;
184 this.initAndStartLoop();
187 PerfTestRunner._perSecondRunner = function () {
188 var timeToRun = this._test.timeToRun || 750;
191 var callsPerIteration = this._callsPerIteration;
193 if (this._test.setup)
196 while (totalTime < timeToRun) {
197 totalTime += this._perSecondRunnerIterator(callsPerIteration);
198 i += callsPerIteration;
199 if (this._completedRuns < 0 && totalTime < 100)
200 callsPerIteration = Math.max(10, 2 * callsPerIteration);
202 this._callsPerIteration = callsPerIteration;
204 this.ignoreWarmUpAndLog(i * 1000 / totalTime);
208 PerfTestRunner._perSecondRunnerIterator = function (callsPerIteration) {
209 var startTime = Date.now();
210 for (var i = 0; i < callsPerIteration; i++)
212 return Date.now() - startTime;
215 if (window.layoutTestController) {
216 layoutTestController.waitUntilDone();
217 layoutTestController.dumpAsText();