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