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 = {};
16 var THING = 'sprites';
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){
28 var toProcess = files.filter( function(filename){
29 return filename.match(/\.json$/);
32 fileCount = toProcess.length;
34 toProcess.forEach( function(filename){
35 fs.readFile(path.join(INPUT_DIR, filename), 'utf8', function (err, json) {
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;
47 var columnLabels = [""];
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];
53 // testTypes are things like "scale", "rotation", and "translation"
54 Object.keys( spriteResults ).sort().forEach( function(testType){
55 var testResults = spriteResults[testType];
58 vals = [testResults.objectCount];
60 if(!basicDataStructure[type]){
61 basicDataStructure[type] = {};
63 if(!basicDataStructure[type][browser]){
64 basicDataStructure[type][browser] = {};
66 if(!basicDataStructure[type][browser][key]){
67 basicDataStructure[type][browser][key] = {};
69 if(!basicDataStructure[type][browser][key][name]){
70 basicDataStructure[type][browser][key][name] = {};
72 basicDataStructure[type][browser][key][name][testType] = testResults.objectCount;
75 columnLabels = columnLabels.concat( key );
76 row = row.concat( vals );
81 dataStructure[combo][2] = columnLabels;
82 dataStructure[combo].push( row );
85 if( fileCount === 0 ){
86 var spreadsheet = createSpreadsheet();
88 saveSpreadsheet( spreadsheet );
96 function createSpreadsheet(){
99 dataStructure = Object.keys(basicDataStructure).map( function(type){
100 // type is something like Phone / Tablet / Laptop
102 spreadsheet.push( [type] );
104 Object.keys( basicDataStructure[type] ).map( function(browser){
105 // browser is something like Safari or Firefox
107 spreadsheet.push( [,browser] );
109 var summaryOfTechniques = [];
110 var allEncounteredDevices = [];
112 Object.keys( basicDataStructure[type][browser] ).map( function(renderingTechnique){
113 // renderingTechnique is something like css2dImg
115 spreadsheet.push( [,,renderingTechnique] );
116 spreadsheet.push( [,,,"Device","translate","scale","rotate"] );
118 var summaryOfTechnique = {"technique":renderingTechnique, "deviceScores":{}};
120 Object.keys( basicDataStructure[type][browser][renderingTechnique] ).map( function(device){
121 // Device is something like the iPhone 4S
123 if( allEncounteredDevices.indexOf( device ) === -1 ){
124 allEncounteredDevices.push( device );
127 var dataRow = [,,,device];
128 var sumOfTestTypes = undefined;
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 );
136 sumOfTestTypes = sumOfTestTypes || 0;
137 sumOfTestTypes += dataPoint;
140 spreadsheet.push( dataRow );
142 summaryOfTechnique.deviceScores[ device ] = sumOfTestTypes;
145 spreadsheet.push( [] );
147 summaryOfTechniques.push( summaryOfTechnique );
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] )
159 spreadsheet.push( summaryRow );
161 spreadsheet.push( [] );
168 function saveSpreadsheet(spreadsheet){
169 fs.writeFile( path.join(OUTPUT_DIR, THING + "-" + PARAM + "-RAW.csv"), report.csvByTable(spreadsheet), 'utf8', function(err){
171 console.log("saved RAW!");
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++ ){
185 for( var j = 3; j < rows.length; j++ ){
186 var temp = rows[j][i];
192 var avg = sum / legitCount;
196 var bestTheWorstPhoneCanDo = Infinity;
197 var bestTheBestPhoneCanDo = -Infinity;
198 var averageOfEachBestTechnique = 0;
200 for( var j = 3; j < rows.length; j++ ){
202 var maxForPhone = -Infinity;
203 for( var i = 1; i < rows[2].length; i++ ){
204 var temp = rows[j][i];
206 maxForPhone = Math.max( maxForPhone, temp );
210 bestTheWorstPhoneCanDo = Math.min( bestTheWorstPhoneCanDo, maxForPhone );
211 bestTheBestPhoneCanDo = Math.max( bestTheBestPhoneCanDo, maxForPhone );
212 averageOfEachBestTechnique += maxForPhone;
214 averageOfEachBestTechnique = (averageOfEachBestTechnique / phoneCount);
216 toSave = toSave.concat( rows, [averages] );
217 roundUp.push( [combo, bestTheBestPhoneCanDo, averageOfEachBestTechnique, bestTheWorstPhoneCanDo] );
219 console.log( roundUp );
220 toSave = toSave.concat( roundUp );
222 fs.writeFile( path.join(OUTPUT_DIR, THING + "-" + PARAM + ".csv"), report.csvByTable(toSave), 'utf8', function(err){
224 console.log("saved!");
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] );
237 var uniqueXValues = [];
239 Object.keys( basicDataStructure[type][browser][techniqueName] ).sort().forEach( function(deviceName){
240 var data = basicDataStructure[type][browser][techniqueName][deviceName];
242 data.forEach( function(tuple){
243 var objectCount = tuple[0];
244 if( uniqueXValues.indexOf(objectCount) === -1 ){
245 uniqueXValues.push( objectCount );
250 uniqueXValues.sort( NUMERIC_SORT );
252 output.push( ["", "", "", ""].concat( uniqueXValues ) );
254 Object.keys( basicDataStructure[type][browser][techniqueName] ).sort().forEach( function(deviceName){
255 var data = basicDataStructure[type][browser][techniqueName][deviceName];
257 var yValues = uniqueXValues.map( function(numObjects){
258 var fps = fpsForObjectCount(data, numObjects);
266 output.push( ["", "", "", deviceName].concat( yValues ) );
273 fs.writeFile( path.join(OUTPUT_DIR, THING + "-" + PARAM + "-RAW.csv"), report.csvByTable(output), 'utf8', function(err){
275 console.log("saved RAW!");
280 function fpsForObjectCount( data, objectCount ){
281 for( var i = 0; i < data.length; i++ ){
282 if( data[i][0] === objectCount ){