0b467313eec867cf3b983e1ad7e05e51d463cb14
[WebKit-https.git] / PerformanceTests / DoYouEvenBench / 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     document.body.insertBefore(frame, document.body.firstChild);
87     this._frame = frame;
88     return frame;
89 }
90
91 BenchmarkRunner.prototype._waitAndWarmUp = function () {
92     var startTime = Date.now();
93
94     function Fibonacci(n) {
95         if (Date.now() - startTime > 100)
96             return;
97         if (n <= 0)
98             return 0;
99         else if (n == 1)
100             return 1;
101         return Fibonacci(n - 2) + Fibonacci(n - 1);
102     }
103
104     var promise = new SimplePromise;
105     setTimeout(function () {
106         Fibonacci(100);
107         promise.resolve();
108     }, 200);
109     return promise;
110 }
111
112 // This function ought be as simple as possible. Don't even use SimplePromise.
113 BenchmarkRunner.prototype._runTest = function(suite, testFunction, prepareReturnValue, callback)
114 {
115     var now = window.performance && window.performance.now ? function () { return window.performance.now(); } : Date.now;
116
117     var contentWindow = this._frame.contentWindow;
118     var contentDocument = this._frame.contentDocument;
119
120     var startTime = now();
121     testFunction(prepareReturnValue, contentWindow, contentDocument);
122     var endTime = now();
123     var syncTime = endTime - startTime;
124
125     var startTime = now();
126     setTimeout(function () {
127         var endTime = now();
128         callback(syncTime, endTime - startTime);
129     }, 0);
130 }
131
132 function BenchmarkState(suites) {
133     this._suites = suites;
134     this._suiteIndex = -1;
135     this._testIndex = 0;
136     this.next();
137 }
138
139 BenchmarkState.prototype.currentSuite = function() {
140     return this._suites[this._suiteIndex];
141 }
142
143 BenchmarkState.prototype.currentTest = function () {
144     var suite = this.currentSuite();
145     return suite ? suite.tests[this._testIndex] : null;
146 }
147
148 BenchmarkState.prototype.next = function () {
149     this._testIndex++;
150
151     var suite = this._suites[this._suiteIndex];
152     if (suite && this._testIndex < suite.tests.length)
153         return this;
154
155     this._testIndex = 0;
156     do {
157         this._suiteIndex++;
158     } while (this._suiteIndex < this._suites.length && this._suites[this._suiteIndex].disabled);
159
160     return this;
161 }
162
163 BenchmarkState.prototype.isFirstTest = function () {
164     return !this._testIndex;
165 }
166
167 BenchmarkState.prototype.prepareCurrentSuite = function (runner, frame) {
168     var suite = this.currentSuite();
169     var promise = new SimplePromise;
170     frame.onload = function () {
171         suite.prepare(runner, frame.contentWindow, frame.contentDocument).then(function (result) { promise.resolve(result); });
172     }
173     frame.src = 'resources/' + suite.url;
174     return promise;
175 }
176
177 BenchmarkRunner.prototype.step = function (state) {
178     if (!state) {
179         state = new BenchmarkState(this._suites);
180         this._measuredValues = {tests: {}, total: 0};
181     }
182
183     var suite = state.currentSuite();
184     if (!suite) {
185         this._finalize();
186         var promise = new SimplePromise;
187         promise.resolve();
188         return promise;
189     }
190
191     if (state.isFirstTest()) {
192         this._masuredValuesForCurrentSuite = {};
193         var self = this;
194         return state.prepareCurrentSuite(this, this._appendFrame()).then(function (prepareReturnValue) {
195             self._prepareReturnValue = prepareReturnValue;
196             return self._runTestAndRecordResults(state);
197         });
198     }
199
200     return this._runTestAndRecordResults(state);
201 }
202
203 BenchmarkRunner.prototype.runAllSteps = function (startingState) {
204     var nextCallee = this.runAllSteps.bind(this);
205     this.step(startingState).then(function (nextState) {
206         if (nextState)
207             nextCallee(nextState);
208     });
209 }
210
211 BenchmarkRunner.prototype.runMultipleIterations = function (iterationCount) {
212     var self = this;
213     var currentIteration = 0;
214
215     this._runNextIteration = function () {
216         currentIteration++;
217         if (currentIteration < iterationCount)
218             self.runAllSteps();
219         else if (this._client && this._client.didFinishLastIteration)
220             this._client.didFinishLastIteration();
221     }
222
223     if (this._client && this._client.willStartFirstIteration)
224         this._client.willStartFirstIteration(iterationCount);
225
226     self.runAllSteps();
227 }
228
229 BenchmarkRunner.prototype._runTestAndRecordResults = function (state) {
230     var promise = new SimplePromise;
231     var suite = state.currentSuite();
232     var test = state.currentTest();
233
234     if (this._client && this._client.willRunTest)
235         this._client.willRunTest(suite, test);
236
237     var self = this;
238     setTimeout(function () {
239         self._runTest(suite, test.run, self._prepareReturnValue, function (syncTime, asyncTime) {
240             var suiteResults = self._measuredValues.tests[suite.name] || {tests:{}, total: 0};
241             var total = syncTime + asyncTime;
242             self._measuredValues.tests[suite.name] = suiteResults;
243             suiteResults.tests[test.name] = {tests: {'Sync': syncTime, 'Async': asyncTime}, total: total};
244             suiteResults.total += total;
245             self._measuredValues.total += total;
246
247             if (self._client && self._client.didRunTest)
248                 self._client.didRunTest(suite, test);
249
250             state.next();
251             if (state.currentSuite() != suite)
252                 self._removeFrame();
253             promise.resolve(state);
254         });
255     }, 0);
256     return promise;
257 }
258
259 BenchmarkRunner.prototype._finalize = function () {
260     this._removeFrame();
261
262     if (this._client && this._client.didRunSuites)
263         this._client.didRunSuites(this._measuredValues);
264
265     if (this._runNextIteration)
266         this._runNextIteration();
267 }