a2ac7b2b0bd6e725e69ff77cb48ae4fe5f6d47c8
[WebKit-https.git] / PerformanceTests / Animometer / resources / runner / benchmark-runner.js
1 BenchmarkRunnerState = Utilities.createClass(
2     function(suites)
3     {
4         this._suites = suites;
5         this._suiteIndex = -1;
6         this._testIndex = 0;
7         this.next();
8     }, {
9
10     currentSuite: function()
11     {
12         return this._suites[this._suiteIndex];
13     },
14
15     currentTest: function()
16     {
17         var suite = this.currentSuite();
18         return suite ? suite.tests[this._testIndex] : null;
19     },
20
21     isFirstTest: function()
22     {
23         return !this._testIndex;
24     },
25
26     next: function()
27     {
28         this._testIndex++;
29
30         var suite = this._suites[this._suiteIndex];
31         if (suite && this._testIndex < suite.tests.length)
32             return;
33
34         this._testIndex = 0;
35         do {
36             this._suiteIndex++;
37         } while (this._suiteIndex < this._suites.length && this._suites[this._suiteIndex].disabled);
38     },
39
40     prepareCurrentTest: function(runner, frame)
41     {
42         var test = this.currentTest();
43         var promise = new SimplePromise;
44
45         frame.onload = function() {
46             promise.resolve();
47         };
48
49         frame.src = "tests/" + test.url;
50         return promise;
51     }
52 });
53
54 BenchmarkRunner = Utilities.createClass(
55     function(suites, frameContainer, client)
56     {
57         this._suites = suites;
58         this._client = client;
59         this._frameContainer = frameContainer;
60     }, {
61
62     _appendFrame: function()
63     {
64         var frame = document.createElement("iframe");
65         frame.setAttribute("scrolling", "no");
66
67         this._frameContainer.insertBefore(frame, this._frameContainer.firstChild);
68         this._frame = frame;
69         return frame;
70     },
71
72     _removeFrame: function()
73     {
74         if (this._frame) {
75             this._frame.parentNode.removeChild(this._frame);
76             this._frame = null;
77         }
78     },
79
80     _runBenchmarkAndRecordResults: function(state)
81     {
82         var promise = new SimplePromise;
83         var suite = state.currentSuite();
84         var test = state.currentTest();
85
86         if (this._client && this._client.willRunTest)
87             this._client.willRunTest(suite, test);
88
89         var contentWindow = this._frame.contentWindow;
90         var self = this;
91
92         var options = { complexity: test.complexity };
93         Utilities.extendObject(options, this._client.options);
94         Utilities.extendObject(options, contentWindow.Utilities.parseParameters());
95
96         var benchmark = new contentWindow.benchmarkClass(options);
97         document.body.style.backgroundColor = benchmark.backgroundColor();
98         benchmark.run().then(function(results) {
99             var suiteResults = self._suitesResults[suite.name] || {};
100             suiteResults[test.name] = results;
101             self._suitesResults[suite.name] = suiteResults;
102
103             if (self._client && self._client.didRunTest)
104                 self._client.didRunTest(suite, test);
105
106             state.next();
107             if (state.currentSuite() != suite)
108                 self._removeFrame();
109             promise.resolve(state);
110         });
111
112         return promise;
113     },
114
115     step: function(state)
116     {
117         if (!state) {
118             state = new BenchmarkRunnerState(this._suites);
119             this._suitesResults = {};
120         }
121
122         var suite = state.currentSuite();
123         if (!suite) {
124             this._finalize();
125             var promise = new SimplePromise;
126             promise.resolve();
127             return promise;
128         }
129
130         if (state.isFirstTest()) {
131             this._appendFrame();
132         }
133
134         return state.prepareCurrentTest(this, this._frame).then(function(prepareReturnValue) {
135             return this._runBenchmarkAndRecordResults(state);
136         }.bind(this));
137     },
138
139     runAllSteps: function(startingState)
140     {
141         var nextCallee = this.runAllSteps.bind(this);
142         this.step(startingState).then(function(nextState) {
143             if (nextState)
144                 nextCallee(nextState);
145         });
146     },
147
148     runMultipleIterations: function()
149     {
150         var self = this;
151         var currentIteration = 0;
152
153         this._runNextIteration = function() {
154             currentIteration++;
155             if (currentIteration < self._client.iterationCount)
156                 self.runAllSteps();
157             else if (this._client && this._client.didFinishLastIteration) {
158                 document.body.style.backgroundColor = "";
159                 self._client.didFinishLastIteration();
160             }
161         }
162
163         if (this._client && this._client.willStartFirstIteration)
164             this._client.willStartFirstIteration();
165
166         this.runAllSteps();
167     },
168
169     _finalize: function()
170     {
171         this._removeFrame();
172
173         if (this._client && this._client.didRunSuites)
174             this._client.didRunSuites(this._suitesResults);
175
176         if (this._runNextIteration)
177             this._runNextIteration();
178     }
179 });