summaryrefslogtreecommitdiffstats
path: root/third_party/webkit/PerformanceTests/Speedometer/resources/benchmark-runner.js
blob: 751992b4f190bbfd69e54e5d5e749898365a7612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// FIXME: Use the real promise if available.
// FIXME: Make sure this interface is compatible with the real Promise.
function SimplePromise() {
    this._chainedPromise = null;
    this._callback = null;
}

SimplePromise.prototype.then = function (callback) {
    if (this._callback)
        throw "SimplePromise doesn't support multiple calls to then";
    this._callback = callback;
    this._chainedPromise = new SimplePromise;
    
    if (this._resolved)
        this.resolve(this._resolvedValue);

    return this._chainedPromise;
}

SimplePromise.prototype.resolve = function (value) {
    if (!this._callback) {
        this._resolved = true;
        this._resolvedValue = value;
        return;
    }

    var result = this._callback(value);
    if (result instanceof SimplePromise) {
        var chainedPromise = this._chainedPromise;
        result.then(function (result) { chainedPromise.resolve(result); });
    } else
        this._chainedPromise.resolve(result);
}

function BenchmarkTestStep(testName, testFunction) {
    this.name = testName;
    this.run = testFunction;
}

function BenchmarkRunner(suites, client) {
    this._suites = suites;
    this._prepareReturnValue = null;
    this._client = client;
}

BenchmarkRunner.prototype.waitForElement = function (selector) {
    var promise = new SimplePromise;
    var contentDocument = this._frame.contentDocument;

    function resolveIfReady() {
        var element = contentDocument.querySelector(selector);
        if (element) {
            window.requestAnimationFrame(function () {
                return promise.resolve(element);
            });
            return;
        }
        setTimeout(resolveIfReady, 50);
    }

    resolveIfReady();
    return promise;
}

BenchmarkRunner.prototype._removeFrame = function () {
    if (this._frame) {
        this._frame.parentNode.removeChild(this._frame);
        this._frame = null;
    }
}

BenchmarkRunner.prototype._appendFrame = function (src) {
    var frame = document.createElement('iframe');
    frame.style.width = '800px';
    frame.style.height = '600px';
    frame.style.border = '0px none';
    frame.style.position = 'absolute';
    frame.setAttribute('scrolling', 'no');

    var marginLeft = parseInt(getComputedStyle(document.body).marginLeft);
    var marginTop = parseInt(getComputedStyle(document.body).marginTop);
    if (window.innerWidth > 800 + marginLeft && window.innerHeight > 600 + marginTop) {
        frame.style.left = marginLeft + 'px';
        frame.style.top = marginTop + 'px';
    } else {
        frame.style.left = '0px';
        frame.style.top = '0px';
    }

    if (this._client && this._client.willAddTestFrame)
        this._client.willAddTestFrame(frame);

    document.body.insertBefore(frame, document.body.firstChild);
    this._frame = frame;
    return frame;
}

BenchmarkRunner.prototype._waitAndWarmUp = function () {
    var startTime = Date.now();

    function Fibonacci(n) {
        if (Date.now() - startTime > 100)
            return;
        if (n <= 0)
            return 0;
        else if (n == 1)
            return 1;
        return Fibonacci(n - 2) + Fibonacci(n - 1);
    }

    var promise = new SimplePromise;
    setTimeout(function () {
        Fibonacci(100);
        promise.resolve();
    }, 200);
    return promise;
}

BenchmarkRunner.prototype._writeMark = function(name) {
    if (window.performance && window.performance.mark)
        window.performance.mark(name);
}

// This function ought be as simple as possible. Don't even use SimplePromise.
BenchmarkRunner.prototype._runTest = function(suite, test, prepareReturnValue, callback)
{
    var self = this;
    var now = window.performance && window.performance.now ? function () { return window.performance.now(); } : Date.now;

    var contentWindow = self._frame.contentWindow;
    var contentDocument = self._frame.contentDocument;

    self._writeMark(suite.name + '.' + test.name + '-start');
    var startTime = now();
    test.run(prepareReturnValue, contentWindow, contentDocument);
    var endTime = now();
    self._writeMark(suite.name + '.' + test.name + '-sync-end');

    var syncTime = endTime - startTime;

    var startTime = now();
    setTimeout(function () {
        // Some browsers don't immediately update the layout for paint.
        // Force the layout here to ensure we're measuring the layout time.
        var height = self._frame.contentDocument.body.getBoundingClientRect().height;
        var endTime = now();
        self._frame.contentWindow._unusedHeightValue = height; // Prevent dead code elimination.
        self._writeMark(suite.name + '.' + test.name + '-async-end');
        window.requestAnimationFrame(function () {
            callback(syncTime, endTime - startTime, height);
        });
    }, 0);
}

function BenchmarkState(suites) {
    this._suites = suites;
    this._suiteIndex = -1;
    this._testIndex = 0;
    this.next();
}

BenchmarkState.prototype.currentSuite = function() {
    return this._suites[this._suiteIndex];
}

BenchmarkState.prototype.currentTest = function () {
    var suite = this.currentSuite();
    return suite ? suite.tests[this._testIndex] : null;
}

BenchmarkState.prototype.next = function () {
    this._testIndex++;

    var suite = this._suites[this._suiteIndex];
    if (suite && this._testIndex < suite.tests.length)
        return this;

    this._testIndex = 0;
    do {
        this._suiteIndex++;
    } while (this._suiteIndex < this._suites.length && this._suites[this._suiteIndex].disabled);

    return this;
}

BenchmarkState.prototype.isFirstTest = function () {
    return !this._testIndex;
}

BenchmarkState.prototype.prepareCurrentSuite = function (runner, frame) {
    var suite = this.currentSuite();
    var promise = new SimplePromise;
    frame.onload = function () {
        suite.prepare(runner, frame.contentWindow, frame.contentDocument).then(function (result) { promise.resolve(result); });
    }
    frame.src = 'resources/' + suite.url;
    return promise;
}

BenchmarkRunner.prototype.step = function (state) {
    if (!state) {
        state = new BenchmarkState(this._suites);
        this._measuredValues = {tests: {}, total: 0, mean: NaN, geomean: NaN, score: NaN};
    }

    var suite = state.currentSuite();
    if (!suite) {
        this._finalize();
        var promise = new SimplePromise;
        promise.resolve();
        return promise;
    }

    if (state.isFirstTest()) {
        this._removeFrame();
        var self = this;
        return state.prepareCurrentSuite(this, this._appendFrame()).then(function (prepareReturnValue) {
            self._prepareReturnValue = prepareReturnValue;
            return self._runTestAndRecordResults(state);
        });
    }

    return this._runTestAndRecordResults(state);
}

BenchmarkRunner.prototype.runAllSteps = function (startingState) {
    var nextCallee = this.runAllSteps.bind(this);
    this.step(startingState).then(function (nextState) {
        if (nextState)
            nextCallee(nextState);
    });
}

BenchmarkRunner.prototype.runMultipleIterations = function (iterationCount) {
    var self = this;
    var currentIteration = 0;

    this._runNextIteration = function () {
        currentIteration++;
        if (currentIteration < iterationCount)
            self.runAllSteps();
        else if (this._client && this._client.didFinishLastIteration)
            this._client.didFinishLastIteration();
    }

    if (this._client && this._client.willStartFirstIteration)
        this._client.willStartFirstIteration(iterationCount);

    self.runAllSteps();
}

BenchmarkRunner.prototype._runTestAndRecordResults = function (state) {
    var promise = new SimplePromise;
    var suite = state.currentSuite();
    var test = state.currentTest();

    if (this._client && this._client.willRunTest)
        this._client.willRunTest(suite, test);

    var self = this;
    setTimeout(function () {
        self._runTest(suite, test, self._prepareReturnValue, function (syncTime, asyncTime) {
            var suiteResults = self._measuredValues.tests[suite.name] || {tests:{}, total: 0};
            var total = syncTime + asyncTime;
            self._measuredValues.tests[suite.name] = suiteResults;
            suiteResults.tests[test.name] = {tests: {'Sync': syncTime, 'Async': asyncTime}, total: total};
            suiteResults.total += total;

            if (self._client && self._client.didRunTest)
                self._client.didRunTest(suite, test);

            state.next();
            promise.resolve(state);
        });
    }, 0);
    return promise;
}

BenchmarkRunner.prototype._finalize = function () {
    this._removeFrame();

    if (this._client && this._client.didRunSuites) {
        var product = 1;
        var values = [];
        for (var suiteName in this._measuredValues.tests) {
            var suiteTotal = this._measuredValues.tests[suiteName].total;
            product *= suiteTotal;
            values.push(suiteTotal);
        }

        values.sort(function (a, b) { return a - b }); // Avoid the loss of significance for the sum.
        var total = values.reduce(function (a, b) { return a + b });
        var geomean = Math.pow(product, 1 / values.length);

        var correctionFactor = 3; // This factor makes the test score look reasonably fit within 0 to 140.
        this._measuredValues.total = total;
        this._measuredValues.mean = total / values.length;
        this._measuredValues.geomean = geomean;
        this._measuredValues.score = 60 * 1000 / geomean / correctionFactor;
        this._client.didRunSuites(this._measuredValues);
    }

    if (this._runNextIteration)
        this._runNextIteration();
}