Merge r219640 into Speedometer 1.0.
[WebKit.git] / Websites / browserbench.org / Speedometer / 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 BenchmarkRunner.prototype._writeMark = function(name) {
116     if (window.performance && window.performance.mark)
117         window.performance.mark(name);
118 }
119
120 // This function ought be as simple as possible. Don't even use SimplePromise.
121 BenchmarkRunner.prototype._runTest = function(suite, test, prepareReturnValue, callback)
122 {
123     var self = this;
124     var now = window.performance && window.performance.now ? function () { return window.performance.now(); } : Date.now;
125
126     var contentWindow = self._frame.contentWindow;
127     var contentDocument = self._frame.contentDocument;
128
129     self._writeMark(`${suite.name}.${test.name}-start`);
130     var startTime = now();
131     test.run(prepareReturnValue, contentWindow, contentDocument);
132     var endTime = now();
133     self._writeMark(`${suite.name}.${test.name}-sync-end`);
134
135     var syncTime = endTime - startTime;
136
137     var startTime = now();
138     setTimeout(function () {
139         var endTime = now();
140         self._writeMark(`${suite.name}.${test.name}-async-end`);
141         callback(syncTime, endTime - startTime);
142     }, 0);
143 }
144
145 function BenchmarkState(suites) {
146     this._suites = suites;
147     this._suiteIndex = -1;
148     this._testIndex = 0;
149     this.next();
150 }
151
152 BenchmarkState.prototype.currentSuite = function() {
153     return this._suites[this._suiteIndex];
154 }
155
156 BenchmarkState.prototype.currentTest = function () {
157     var suite = this.currentSuite();
158     return suite ? suite.tests[this._testIndex] : null;
159 }
160
161 BenchmarkState.prototype.next = function () {
162     this._testIndex++;
163
164     var suite = this._suites[this._suiteIndex];
165     if (suite && this._testIndex < suite.tests.length)
166         return this;
167
168     this._testIndex = 0;
169     do {
170         this._suiteIndex++;
171     } while (this._suiteIndex < this._suites.length && this._suites[this._suiteIndex].disabled);
172
173     return this;
174 }
175
176 BenchmarkState.prototype.isFirstTest = function () {
177     return !this._testIndex;
178 }
179
180 BenchmarkState.prototype.prepareCurrentSuite = function (runner, frame) {
181     var suite = this.currentSuite();
182     var promise = new SimplePromise;
183     frame.onload = function () {
184         suite.prepare(runner, frame.contentWindow, frame.contentDocument).then(function (result) { promise.resolve(result); });
185     }
186     frame.src = 'resources/' + suite.url;
187     return promise;
188 }
189
190 BenchmarkRunner.prototype.step = function (state) {
191     if (!state) {
192         state = new BenchmarkState(this._suites);
193         this._measuredValues = {tests: {}, total: 0};
194     }
195
196     var suite = state.currentSuite();
197     if (!suite) {
198         this._finalize();
199         var promise = new SimplePromise;
200         promise.resolve();
201         return promise;
202     }
203
204     if (state.isFirstTest()) {
205         this._masuredValuesForCurrentSuite = {};
206         var self = this;
207         return state.prepareCurrentSuite(this, this._appendFrame()).then(function (prepareReturnValue) {
208             self._prepareReturnValue = prepareReturnValue;
209             return self._runTestAndRecordResults(state);
210         });
211     }
212
213     return this._runTestAndRecordResults(state);
214 }
215
216 BenchmarkRunner.prototype.runAllSteps = function (startingState) {
217     var nextCallee = this.runAllSteps.bind(this);
218     this.step(startingState).then(function (nextState) {
219         if (nextState)
220             nextCallee(nextState);
221     });
222 }
223
224 BenchmarkRunner.prototype.runMultipleIterations = function (iterationCount) {
225     var self = this;
226     var currentIteration = 0;
227
228     this._runNextIteration = function () {
229         currentIteration++;
230         if (currentIteration < iterationCount)
231             self.runAllSteps();
232         else if (this._client && this._client.didFinishLastIteration)
233             this._client.didFinishLastIteration();
234     }
235
236     if (this._client && this._client.willStartFirstIteration)
237         this._client.willStartFirstIteration(iterationCount);
238
239     self.runAllSteps();
240 }
241
242 BenchmarkRunner.prototype._runTestAndRecordResults = function (state) {
243     var promise = new SimplePromise;
244     var suite = state.currentSuite();
245     var test = state.currentTest();
246
247     if (this._client && this._client.willRunTest)
248         this._client.willRunTest(suite, test);
249
250     var self = this;
251     setTimeout(function () {
252         self._runTest(suite, test, self._prepareReturnValue, function (syncTime, asyncTime) {
253             var suiteResults = self._measuredValues.tests[suite.name] || {tests:{}, total: 0};
254             var total = syncTime + asyncTime;
255             self._measuredValues.tests[suite.name] = suiteResults;
256             suiteResults.tests[test.name] = {tests: {'Sync': syncTime, 'Async': asyncTime}, total: total};
257             suiteResults.total += total;
258             self._measuredValues.total += total;
259
260             if (self._client && self._client.didRunTest)
261                 self._client.didRunTest(suite, test);
262
263             state.next();
264             if (state.currentSuite() != suite)
265                 self._removeFrame();
266             promise.resolve(state);
267         });
268     }, 0);
269     return promise;
270 }
271
272 BenchmarkRunner.prototype._finalize = function () {
273     this._removeFrame();
274
275     if (this._client && this._client.didRunSuites)
276         this._client.didRunSuites(this._measuredValues);
277
278     if (this._runNextIteration)
279         this._runNextIteration();
280 }