Add an option to output the results of the graphics benchmark in JSON format
[WebKit-https.git] / PerformanceTests / Animometer / runner / resources / animometer.js
1 window.benchmarkRunnerClient = {
2     iterationCount: 1,
3     testsCount: null,
4     progressBar: null,
5     recordTable: null,
6     options: { testInterval: 30000, frameRate: 50, estimatedFrameRate: true, fixTestComplexity : false },
7     score: 0,
8     _resultsDashboard: null,
9     _resultsTable: null,
10     
11     willAddTestFrame: function (frame)
12     {
13         var main = document.querySelector("main");
14         var style = getComputedStyle(main);
15         frame.style.left = main.offsetLeft + parseInt(style.borderLeftWidth) + parseInt(style.paddingLeft) + "px";
16         frame.style.top = main.offsetTop + parseInt(style.borderTopWidth) + parseInt(style.paddingTop) + "px";
17     },
18     
19     didRunTest: function ()
20     {
21         this.progressBar.incRange();
22     },
23     
24     willStartFirstIteration: function ()
25     {
26         this._resultsDashboard = new ResultsDashboard();
27         this._resultsTable = new ResultsTable(document.querySelector(".results-table"), Headers);
28         
29         this.progressBar = new ProgressBar(document.getElementById("progress-completed"), this.testsCount);
30         this.recordTable = new ResultsTable(document.querySelector(".record-table"), Headers);
31     },
32     
33     didRunSuites: function (suitesSamplers)
34     {
35         this._resultsDashboard.push(suitesSamplers);
36     },
37     
38     didFinishLastIteration: function ()
39     {
40         var json = this._resultsDashboard.toJSON(true, true);
41         this._resultsTable.showIterations(json[Strings["JSON_RESULTS"][0]]);
42         
43         var element = document.querySelector("#json > textarea");
44         element.innerHTML = JSON.stringify(json[Strings["JSON_RESULTS"][0]][0], function(key, value) { 
45             if (typeof value == "number")
46                 return value.toFixed(2);
47             return value;
48         }, 4);
49         
50         this.score = json[Strings["JSON_SCORE"]];
51         showResults();
52     }
53 }
54
55 function showSection(sectionIdentifier, pushState)
56 {
57     var currentSectionElement = document.querySelector("section.selected");
58     console.assert(currentSectionElement);
59
60     var newSectionElement = document.getElementById(sectionIdentifier);
61     console.assert(newSectionElement);
62
63     currentSectionElement.classList.remove("selected");
64     newSectionElement.classList.add("selected");
65
66     if (pushState)
67         history.pushState({section: sectionIdentifier}, document.title);
68 }
69
70 function startBenchmark()
71 {
72     var enabledSuites = [];
73     var checkboxes = document.querySelectorAll("#suites input");
74     for (var i = 0; i < checkboxes.length; ++i) {
75         var checkbox = checkboxes[i];
76         if (checkbox.checked) {
77             enabledSuites.push(checkbox.suite);
78         }
79         localStorage.setItem(checkbox.suite.name, +checkbox.checked);
80         localStorage.setItem("test-interval", document.getElementById("test-interval").value);
81     }
82
83     var enabledSuites = Suites.filter(function (suite, index) { return !suite.disabled && checkboxes[index].checked; });
84     var testsCount = enabledSuites.reduce(function (testsCount, suite) { return testsCount + suite.tests.length; }, 0);
85     benchmarkRunnerClient.testsCount = benchmarkRunnerClient.iterationCount * testsCount;
86     benchmarkRunnerClient.options["testInterval"] = parseInt(document.getElementById("test-interval").value) * 1000;
87     benchmarkRunnerClient.options["frameRate"] = parseInt(document.getElementById("frame-rate").value);
88     benchmarkRunnerClient.options["estimatedFrameRate"] = document.getElementById("estimated-frame-rate").checked;    
89     benchmarkRunnerClient.options["fixTestComplexity"] = document.getElementById("fix-test-complexity").checked;
90     benchmarkRunnerClient.options["showRunningResults"] = document.getElementById("show-running-results").checked;
91     
92     if (!benchmarkRunnerClient.options["showRunningResults"])
93         document.getElementById("record").style.display = "none";
94
95     var runner = new BenchmarkRunner(enabledSuites, benchmarkRunnerClient);
96     runner.runMultipleIterations(benchmarkRunnerClient.iterationCount);
97 }
98
99 function startTest()
100 {
101     showSection("running");
102     startBenchmark();
103 }
104
105 function showResults()
106 {
107     var element = document.querySelector("#results > h1");
108     element.textContent = Strings["TEXT_RESULTS"][0] + ":";
109     
110     var score = benchmarkRunnerClient.score.toFixed(2);
111     element.textContent += " [Score = " + score + "]";
112         
113     showSection("results", true);
114 }
115
116 function showJson()
117 {
118     var element = document.querySelector("#json > h1");
119     element.textContent = Strings["TEXT_RESULTS"][2] + ":";
120     
121     var score = benchmarkRunnerClient.score.toFixed(2);
122     element.textContent += " [Score = " + score + "]";
123
124     showSection("json", true);
125 }
126
127 function showTestGraph(testName, axes, samples, samplingTimeOffset)
128 {
129     var element = document.querySelector("#test-graph > h1");
130     element.textContent = Strings["TEXT_RESULTS"][1] + ":";
131
132     if (testName.length)
133         element.textContent += " [test = " + testName + "]";
134             
135     graph("#graphContainer", new Point(700, 400), new Insets(20, 50, 20, 50), axes, samples, samplingTimeOffset);
136     showSection("test-graph", true);    
137 }
138
139 function showTestJSON(testName, testResults)
140 {
141     var element = document.querySelector("#test-json > h1");
142     element.textContent = Strings["TEXT_RESULTS"][2] + ":";
143
144     if (testName.length)
145         element.textContent += " [test = " + testName + "]";
146             
147     var element = document.querySelector("#test-json > textarea");
148     element.innerHTML = JSON.stringify(testResults, function(key, value) { 
149         if (typeof value == "number")
150             return value.toFixed(2);
151         return value;
152     }, 4);
153
154     showSection("test-json", true);    
155 }
156
157 function populateSettings() {
158     var suitesDiv = document.getElementById("suites");
159     Suites.forEach(function(suite) {
160         var suiteDiv = document.createDocumentFragment();
161
162         var label = document.createElement("label");
163         var checkbox = document.createElement("input");
164         checkbox.setAttribute("type", "checkbox");
165         checkbox.suite = suite;
166         if (+localStorage.getItem(suite.name)) {
167             checkbox.checked = true;
168         }
169         label.appendChild(checkbox);
170         label.appendChild(document.createTextNode(" " + suite.name));
171
172         suiteDiv.appendChild(label);
173         suiteDiv.appendChild(document.createElement("br"));
174         suitesDiv.appendChild(suiteDiv);
175     });
176
177     var interval = localStorage.getItem("test-interval");
178     if (interval) {
179         document.getElementById("test-interval").value = interval;
180     }
181 }
182 document.addEventListener("DOMContentLoaded", populateSettings);