Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / data / third_party / spaceport / bin / condense.js
blob50ca4a407888e66ab3e435d5c95e335c76899606
1 var NUMERIC_SORT = function(a,b){ return a-b; };
3 var fs = require('fs');
4 var path = require('path');
6 var jsBase = __dirname + '/../js';
7 var un = require(jsBase + '/unrequire');
8 un.require.config({ baseUrl: jsBase });
10 var dataStructure = {};
12 var basicDataStructure = {};
14 var fileCount = 0;
16 var THING = 'sprites';
17 var PARAM = 'image';
19 var RESULTS_DIR = path.join(__dirname, '..', 'results');
20 var INPUT_DIR = path.join(RESULTS_DIR, 'raw');
21 var OUTPUT_DIR = path.join(RESULTS_DIR);
23 un.require( ['util/report'], function(report){
24     fs.readdir( INPUT_DIR, function(err, files){
25         if( err ){
26             throw err;
27         }
28         var toProcess = files.filter( function(filename){
29             return filename.match(/\.json$/);
30         });
31     
32         fileCount = toProcess.length;
33     
34         toProcess.forEach( function(filename){
35             fs.readFile(path.join(INPUT_DIR, filename), 'utf8', function (err, json) {
36                 if( err ){
37                     throw err;
38                 }
39                 var data = JSON.parse(json);
40                 var name = data.userData.agentMetadata.name;
41                 var browser = data.userData.agentMetadata.browser;
42                 var userAgent = data.userData.agentMetadata.userAgent;
43                 var type = data.userData.agentMetadata.type;
44                 var combo = type + "-" + browser;
45                 
46                 var row = [name];
47                 var columnLabels = [""];
48                 
49                 // keys are techniques - like css2DImg
50                 Object.keys(data.userData.results[THING][PARAM]).sort().forEach( function(key){
51                     var spriteResults = data.userData.results[THING][PARAM][key];
52                     
53                     // testTypes are things like "scale", "rotation", and "translation"
54                     Object.keys( spriteResults ).sort().forEach( function(testType){
55                         var testResults = spriteResults[testType];
56                         var vals = [null];
57                         if( testResults ){
58                             vals = [testResults.objectCount];
59                         
60                             if(!basicDataStructure[type]){
61                                 basicDataStructure[type] = {};
62                             }
63                             if(!basicDataStructure[type][browser]){
64                                 basicDataStructure[type][browser] = {};
65                             }
66                             if(!basicDataStructure[type][browser][key]){
67                                 basicDataStructure[type][browser][key] = {};
68                             }
69                             if(!basicDataStructure[type][browser][key][name]){
70                                 basicDataStructure[type][browser][key][name] = {};
71                             }
72                             basicDataStructure[type][browser][key][name][testType] = testResults.objectCount;
73                         }
74                         /*
75                         columnLabels = columnLabels.concat( key );
76                         row = row.concat( vals );
77                         */
78                     });
79                 });
80                 /*
81                 dataStructure[combo][2] = columnLabels;
82                 dataStructure[combo].push( row );
83                 */
84                 fileCount -= 1;
85                 if( fileCount === 0 ){
86                     var spreadsheet = createSpreadsheet();
87                     
88                     saveSpreadsheet( spreadsheet );
89 //                    foo();
90 //                    bar();
91                 }
92             });
93         });
94     });
96     function createSpreadsheet(){
97         var spreadsheet = [];
98         
99         dataStructure = Object.keys(basicDataStructure).map( function(type){
100             // type is something like Phone / Tablet / Laptop
101             
102             spreadsheet.push( [type] );
103             
104             Object.keys( basicDataStructure[type] ).map( function(browser){
105                 // browser is something like Safari or Firefox
106                 
107                 spreadsheet.push( [,browser] );
108                 
109                 var summaryOfTechniques = [];
110                 var allEncounteredDevices = [];
111                 
112                 Object.keys( basicDataStructure[type][browser] ).map( function(renderingTechnique){
113                     // renderingTechnique is something like css2dImg
114                     
115                     spreadsheet.push( [,,renderingTechnique] );
116                     spreadsheet.push( [,,,"Device","translate","scale","rotate"] );
117                     
118                     var summaryOfTechnique = {"technique":renderingTechnique, "deviceScores":{}};
119                     
120                     Object.keys( basicDataStructure[type][browser][renderingTechnique] ).map( function(device){
121                         // Device is something like the iPhone 4S
122                         
123                         if( allEncounteredDevices.indexOf( device ) === -1 ){
124                             allEncounteredDevices.push( device );
125                         }
126                         
127                         var dataRow = [,,,device];
128                         var sumOfTestTypes = undefined;
129                         
130                         // Object.keys( basicDataStructure[type][browser][renderingTechnique][device] )
131                         ['translate', 'scale', 'rotate'].map( function(testType){
132                             // testType is something like 'translate', 'scale', or 'rotate;
133                             var dataPoint = basicDataStructure[type][browser][renderingTechnique][device][testType];
134                             dataRow.push( dataPoint );
135                             
136                             sumOfTestTypes = sumOfTestTypes || 0;
137                             sumOfTestTypes += dataPoint;
138                         });
139                         
140                         spreadsheet.push( dataRow );
141                         
142                         summaryOfTechnique.deviceScores[ device ] = sumOfTestTypes;
143                     });
144                     
145                     spreadsheet.push( [] );
146                     
147                     summaryOfTechniques.push( summaryOfTechnique );
148                 });
149                 
150                 spreadsheet.push( [,"Summary"] );
151                 spreadsheet.push( [,].concat(allEncounteredDevices) );
152                 for( var i = 0; i < summaryOfTechniques.length; i++ ){
153                     var summary = summaryOfTechniques[i];
154                     var summaryRow = [summary.technique];
155                     for( var j = 0; j < allEncounteredDevices.length; j++ ){
156                         var encounteredDevice = allEncounteredDevices[j];
157                         summaryRow.push( summary.deviceScores[encounteredDevice] )
158                     }
159                     spreadsheet.push( summaryRow );
160                 }
161                 spreadsheet.push( [] );
162             })
163         });
164         
165         return spreadsheet;
166     }
167     
168     function saveSpreadsheet(spreadsheet){
169         fs.writeFile( path.join(OUTPUT_DIR, THING + "-" + PARAM + "-RAW.csv"), report.csvByTable(spreadsheet), 'utf8', function(err){
170             if(err) throw err;
171             console.log("saved RAW!");
172         } );
173     }
174     
176     function foo(){
177         var toSave = [];
178         var roundUp = [[], ["","best of best device","average of each device's best","best of worst device"]];
179         Object.keys(dataStructure).forEach( function(combo){
180             var rows = dataStructure[combo];
181             var averages = ['averages'];
182             for( var i = 1; i < rows[2].length; i++ ){
183                 var sum = 0;
184                 var legitCount = 0;
185                 for( var j = 3; j < rows.length; j++ ){
186                     var temp = rows[j][i];
187                     if( temp !== null ){
188                         legitCount += 1;
189                         sum += temp;
190                     }
191                 }
192                 var avg = sum / legitCount;
193                 averages[i] = avg;
194             }
195             
196             var bestTheWorstPhoneCanDo = Infinity;
197             var bestTheBestPhoneCanDo = -Infinity;
198             var averageOfEachBestTechnique = 0;
199             var phoneCount = 0;
200             for( var j = 3; j < rows.length; j++ ){
201                 phoneCount += 1;
202                 var maxForPhone = -Infinity;
203                 for( var i = 1; i < rows[2].length; i++ ){
204                     var temp = rows[j][i];
205                     if( temp !== null ){
206                         maxForPhone = Math.max( maxForPhone, temp );
207                     }
208                 }
209                 
210                 bestTheWorstPhoneCanDo = Math.min( bestTheWorstPhoneCanDo, maxForPhone );
211                 bestTheBestPhoneCanDo = Math.max( bestTheBestPhoneCanDo, maxForPhone );
212                 averageOfEachBestTechnique += maxForPhone;
213             }
214             averageOfEachBestTechnique = (averageOfEachBestTechnique / phoneCount);
215             
216             toSave = toSave.concat( rows, [averages] );
217             roundUp.push( [combo, bestTheBestPhoneCanDo, averageOfEachBestTechnique, bestTheWorstPhoneCanDo] );
218         });
219         console.log( roundUp );
220         toSave = toSave.concat( roundUp );
221         
222         fs.writeFile( path.join(OUTPUT_DIR, THING + "-" + PARAM + ".csv"), report.csvByTable(toSave), 'utf8', function(err){
223             if(err) throw err;
224             console.log("saved!");
225         } );
226     }
227     
228     function bar(){
229         var output = [];
230         Object.keys( basicDataStructure ).sort().forEach( function(type){
231             output.push( [type] );
232             Object.keys( basicDataStructure[type] ).sort().forEach( function(browser){
233                 output.push( ["", browser] );
234                 Object.keys( basicDataStructure[type][browser] ).sort().forEach( function(techniqueName){
235                     output.push( ["", "", techniqueName] );
236                     
237                     var uniqueXValues = [];
238                     
239                     Object.keys( basicDataStructure[type][browser][techniqueName] ).sort().forEach( function(deviceName){
240                         var data = basicDataStructure[type][browser][techniqueName][deviceName];
241                         
242                         data.forEach( function(tuple){
243                             var objectCount = tuple[0];
244                             if( uniqueXValues.indexOf(objectCount) === -1 ){
245                                 uniqueXValues.push( objectCount );
246                             }
247                         });
248                     });
249                     
250                     uniqueXValues.sort( NUMERIC_SORT );
251                     
252                     output.push( ["", "", "", ""].concat( uniqueXValues ) );
253                     
254                     Object.keys( basicDataStructure[type][browser][techniqueName] ).sort().forEach( function(deviceName){
255                         var data = basicDataStructure[type][browser][techniqueName][deviceName];
256                         
257                         var yValues = uniqueXValues.map( function(numObjects){
258                             var fps = fpsForObjectCount(data, numObjects);
259                             if( fps === null ){
260                                 return '';
261                             }else{
262                                 return fps;
263                             }
264                         });
265                         
266                         output.push( ["", "", "", deviceName].concat( yValues ) );
267                     });
268                 });
269             });
270         });
271         
272         
273         fs.writeFile( path.join(OUTPUT_DIR, THING + "-" + PARAM + "-RAW.csv"), report.csvByTable(output), 'utf8', function(err){
274             if(err) throw err;
275             console.log("saved RAW!");
276         } );    
277     }
280 function fpsForObjectCount( data, objectCount ){
281     for( var i = 0; i < data.length; i++ ){
282         if( data[i][0] === objectCount ){
283             return data[i][2];
284         }
285     }
286     return null;