36c03c4425a4649789fbb9048b363724d93a93e4
[WebKit-https.git] / Websites / browserbench.org / Speedometer2.0 / resources / benchmark-runner.js
1 // FIXME: Use the real promise if available.
2 // FIXME: Make sure this interface is compatible with the real Promise.
3 function SimplePromise() {
4     this._chainedPromise = null;
5     this._callback = null;
6 }
7
8 SimplePromise.prototype.then = function (callback) {
9     if (this._callback)
10         throw "SimplePromise doesn't support multiple calls to then";
11     this._callback = callback;
12     this._chainedPromise = new SimplePromise;
13     
14     if (this._resolved)
15         this.resolve(this._resolvedValue);
16
17     return this._chainedPromise;
18 }
19
20 SimplePromise.prototype.resolve = function (value) {
21     if (!this._callback) {
22         this._resolved = true;
23         this._resolvedValue = value;
24         return;
25     }
26
27     var result = this._callback(value);
28     if (result instanceof SimplePromise) {
29         var chainedPromise = this._chainedPromise;
30         result.then(function (result) { chainedPromise.resolve(result); });
31     } else
32         this._chainedPromise.resolve(result);
33 }
34
35 function BenchmarkTestStep(testName, testFunction) {
36     this.name = testName;
37     this.run = testFunction;
38 }
39
40 function BenchmarkRunner(suites, client) {
41     this._suites = suites;
42     this._prepareReturnValue = null;
43     this._client = client;
44 }
45
46 BenchmarkRunner.prototype.waitForElement = function (selector) {
47     var promise = new SimplePromise;
48     var contentDocument = this._frame.contentDocument;
49
50     function resolveIfReady() {
51         var element = contentDocument.querySelector(selector);
52         if (element)
53             return promise.resolve(element);
54         setTimeout(resolveIfReady, 50);
55     }
56
57     resolveIfReady();
58     return promise;
59 }
60
61 BenchmarkRunner.prototype._removeFrame = function () {
62     if (this._frame) {
63         this._frame.parentNode.removeChild(this._frame);
64         this._frame = null;
65     }
66 }
67
68 BenchmarkRunner.prototype._appendFrame = function (src) {
69     var frame = document.createElement('iframe');
70     frame.style.width = '800px';
71     frame.style.height = '600px';
72     frame.style.border = '0px none';
73     frame.style.position = 'absolute';
74     frame.setAttribute('scrolling', 'no');
75
76     var marginLeft = parseInt(getComputedStyle(document.body).marginLeft);
77     var marginTop = parseInt(getComputedStyle(document.body).marginTop);
78     if (window.innerWidth > 800 + marginLeft && window.innerHeight > 600 + marginTop) {
79         frame.style.left = marginLeft + 'px';
80         frame.style.top = marginTop + 'px';
81     } else {
82         frame.style.left = '0px';
83         frame.style.top = '0px';
84     }
85
86     if (this._client && this._client.willAddTestFrame)
87         this._client.willAddTestFrame(frame);
88
89     document.body.insertBefore(frame, document.body.firstChild);
90     this._frame = frame;
91     return frame;
92 }
93
94 BenchmarkRunner.prototype._waitAndWarmUp = function () {
95     var startTime = Date.now();
96
97     function Fibonacci(n) {
98         if (Date.now() - startTime > 100)
99             return;
100         if (n <= 0)
101             return 0;
102         else if (n == 1)
103             return 1;
104         return Fibonacci(n - 2) + Fibonacci(n - 1);
105     }
106
107     var promise = new SimplePromise;
108     setTimeout(function () {
109         Fibonacci(100);
110         promise.resolve();
111     }, 200);
112     return promise;
113 }
114
115 // This function ought be as simple as possible. Don't even use SimplePromise.
116 BenchmarkRunner.prototype._runTest = function(suite, testFunction, prepareReturnValue, callback)
117 {
118     var now = window.performance && window.performance.now ? function () { return window.performance.now(); } : Date.now;
119
120     var contentWindow = this._frame.contentWindow;
121     var contentDocument = this._frame.contentDocument;
122
123     var startTime = now();
124     testFunction(prepareReturnValue, contentWindow, contentDocument);
125     var endTime = now();
126     var syncTime = endTime - startTime;
127
128     var startTime = now();
129     setTimeout(function () {
130         var endTime = now();
131         callback(syncTime, endTime - startTime);
132     }, 0);
133 }
134
135 function BenchmarkState(suites) {
136     this._suites = suites;
137     this._suiteIndex = -1;
138     this._testIndex = 0;
139     this.next();
140 }
141
142 BenchmarkState.prototype.currentSuite = function() {
143     return this._suites[this._suiteIndex];
144 }
145
146 BenchmarkState.prototype.currentTest = function () {
147     var suite = this.currentSuite();
148     return suite ? suite.tests[this._testIndex] : null;
149 }
150
151 BenchmarkState.prototype.next = function () {
152     this._testIndex++;
153
154     var suite = this._suites[this._suiteIndex];
155     if (suite && this._testIndex < suite.tests.length)
156         return this;
157
158     this._testIndex = 0;
159     do {
160         this._suiteIndex++;
161     } while (this._suiteIndex < this._suites.length && this._suites[this._suiteIndex].disabled);
162
163     return this;
164 }
165
166 BenchmarkState.prototype.isFirstTest = function () {
167     return !this._testIndex;
168 }
169
170 BenchmarkState.prototype.prepareCurrentSuite = function (runner, frame) {
171     var suite = this.currentSuite();
172     var promise = new SimplePromise;
173     frame.onload = function () {
174         suite.prepare(runner, frame.contentWindow, frame.contentDocument).then(function (result) { promise.resolve(result); });
175     }
176     frame.src = 'resources/' + suite.url;
177     return promise;
178 }
179
180 BenchmarkRunner.prototype.step = function (state) {
181     if (!state) {
182         state = new BenchmarkState(this._suites);
183         this._measuredValues = {tests: {}, total: 0};
184     }
185
186     var suite = state.currentSuite();
187     if (!suite) {
188         this._finalize();
189         var promise = new SimplePromise;
190         promise.resolve();
191         return promise;
192     }
193
194     if (state.isFirstTest()) {
195         this._masuredValuesForCurrentSuite = {};
196         var self = this;
197         return state.prepareCurrentSuite(this, this._appendFrame()).then(function (prepareReturnValue) {
198             self._prepareReturnValue = prepareReturnValue;
199             return self._runTestAndRecordResults(state);
200         });
201     }
202
203     return this._runTestAndRecordResults(state);
204 }
205
206 BenchmarkRunner.prototype.runAllSteps = function (startingState) {
207     var nextCallee = this.runAllSteps.bind(this);
208     this.step(startingState).then(function (nextState) {
209         if (nextState)
210             nextCallee(nextState);
211     });
212 }
213
214 BenchmarkRunner.prototype.runMultipleIterations = function (iterationCount) {
215     var self = this;
216     var currentIteration = 0;
217
218     this._runNextIteration = function () {
219         currentIteration++;
220         if (currentIteration < iterationCount)
221             self.runAllSteps();
222         else if (this._client && this._client.didFinishLastIteration)
223             this._client.didFinishLastIteration();
224     }
225
226     if (this._client && this._client.willStartFirstIteration)
227         this._client.willStartFirstIteration(iterationCount);
228
229     self.runAllSteps();
230 }
231
232 BenchmarkRunner.prototype._runTestAndRecordResults = function (state) {
233     var promise = new SimplePromise;
234     var suite = state.currentSuite();
235     var test = state.currentTest();
236
237     if (this._client && this._client.willRunTest)
238         this._client.willRunTest(suite, test);
239
240     var self = this;
241     setTimeout(function () {
242         self._runTest(suite, test.run, self._prepareReturnValue, function (syncTime, asyncTime) {
243             var suiteResults = self._measuredValues.tests[suite.name] || {tests:{}, total: 0};
244             var total = syncTime + asyncTime;
245             self._measuredValues.tests[suite.name] = suiteResults;
246             suiteResults.tests[test.name] = {tests: {'Sync': syncTime, 'Async': asyncTime}, total: total};
247             suiteResults.total += total;
248             self._measuredValues.total += total;
249
250             if (self._client && self._client.didRunTest)
251                 self._client.didRunTest(suite, test);
252
253             state.next();
254             if (state.currentSuite() != suite)
255                 self._removeFrame();
256             promise.resolve(state);
257         });
258     }, 0);
259     return promise;
260 }
261
262 BenchmarkRunner.prototype._finalize = function () {
263     this._removeFrame();
264
265     if (this._client && this._client.didRunSuites)
266         this._client.didRunSuites(this._measuredValues);
267
268     if (this._runNextIteration)
269         this._runNextIteration();
270 }