-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathreport.js
137 lines (120 loc) · 5.99 KB
/
report.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* This is the `report` component. Given a set of `results` data as collected by
* the `server` component it gets the mean values, then generates an HTML
* document with bars 'n things.
*
* This is done via straight up string iterpolation.
* TODO: Probably use a templating language instead.
*
* @author Daniel Espeset <[email protected]>
* @copyright (c) 2014 Etsy, Inc.
**/
var fs = require('fs'),
path = require('path');
function uniqueSort(arr){
return arr.reverse()
.filter(function(e, i, arr){
return arr.indexOf(e, i+1) === -1;
})
.reverse()
.sort(function(a,b){
return a-b;
});
}
// Summerizes the results data into mean values,
// returns the data structure used to build the HTML
function buildReportData(results) {
var report = {};
for ( var UA in results ) {
report[UA] = { times: {} };
for ( var js in results[UA] ) {
results[UA][js].parse = uniqueSort(results[UA][js].parse);
results[UA][js].exec = uniqueSort(results[UA][js].exec);
report[UA].times[js] = {
parse: results[UA][js].parse[Math.floor(results[UA][js].parse.length/2)],
exec: results[UA][js].exec[Math.floor(results[UA][js].exec.length/2)]
}
}
}
return report;
}
// Given the data structure returned by `buildReportData`, generates HTML
function generateHTML(report) {
var html =
[ "<!DOCTYPE html>",
"<html>",
"<head>",
"<style>",
"body { box-sizing: border-box; -moz-box-sizing: border-box; }",
"table { border-spacing: 0px }",
"tr { height: 20px; }",
"td { border-bottom: 1px solid #333; border-left: 1px solid #333; padding: 2px 8px; }",
"tr:nth-child(odd) { background: #dadad1 }",
"</style>",
"</head>",
"<body style='background:#fafaf1;color:#333;font-family:Arial;'>",
"<h1>Initial parse & execution times for some JS files on mobile devices.</h1>",
"<p>Median of several runs on each device, <strong>excludes network time</strong>.</p>" ];
// Convert ms `parse` or `execute` int to pixel value
// by dividing by constand 1.8
function f(n) { return Math.round(n/1.8); }
// Generate a color for a given parse time as n
function cp(n) { var delta = Math.round(255 * ((n / 1000) > 1 ? 1 : n / 1000)); return 'rgb('+delta+','+(255-delta)+',80)'; }
// Generate a color for a given exec time as n
function ce(n) { var delta = Math.round(255 * ((n / 1000) > 1 ? 1 : n / 1000)); return 'rgb('+delta+','+100+','+(255-delta)+')'; }
// Styled "ms"
var ms = "<span style='color:#aaa;font-size:0.8em'>ms</span>";
// Loop through the report data and generate content for each User Agent
for ( var UA in report ) {
var sum = 0;
html.push("<div style='clear:both;display:table;padding-top:20px;font-family:monospace;font-size:1.1em'>");
if ( report[UA].device ) {
html.push("<h1 style='margin-bottom:5px'>" + report[UA].device.vendor + " " + report[UA].device.marketing_name + " (" + report[UA].device.model + ")</h1>");
html.push("<h3 style='font-weight:normal;margin:5px 0 0;'>" + report[UA].device.browser + " " + report[UA].device.version + " on " + (report[UA].device.os_ios === 't' ? "iOS" : report[UA].device.os_android === 't' ? "Android" : "Unknown OS") + " " + report[UA].device.os_version + "</h3>");
html.push("<h3 style='font-weight:normal;margin-top:0px;'>RAM: " + report[UA].device.RAM + "mb CPU: " + report[UA].device.CPU + "mhz " + (report[UA].device.GPU.length > 1 ? "GPU: " + report[UA].device.GPU + 'mhz ' : 'GPU: N/A' )+"</h3>");
} else {
html.push("<h1>" + UA + "</h1>");
}
html.push(["<div style='clear:left; float:left;'>",
"<table>",
"<tr>",
"<th>file</th><th>parse</th><th>exec</th><th>total</th>",
"</tr>"].join("\n"));
for ( var js in report[UA].times ) {
var data = report[UA].times[js]
html.push(["<tr>",
"<td width='250'>" + js + "</td>",
"<td width='40'>" + data.parse + ms + "</td>",
"<td width='40'>" + data.exec + ms + "</td>",
"<td width='40'>" + (data.parse + data.exec) + ms + "</td>",
"</tr>"].join("\n"));
}
html.push("</table>");
html.push("</div>");
html.push("<div style='float:left;margin-top:20px;'>");
for ( var js in report[UA].times ) {
html.push("<div style='clear:left;float:left;margin-bottom:1px;height:19px;background:"+cp(report[UA].times[js].parse)+";width:"+f(report[UA].times[js].parse)+"px;'></div>");
html.push("<div style='float:left;height:19px;margin-bottom:1px;background:"+ce(report[UA].times[js].exec)+";width:"+f(report[UA].times[js].exec)+"px;'></div>");
sum += parseInt(report[UA].times[js].parse) + parseInt(report[UA].times[js].exec);
}
html.push("</div>");
html.push("</div>");
html.push("<div style='clear:both;width:435px;padding-top:5px;text-align:right;font-size:1em;'>sum: <span style='color:"+ce(sum)+"'>"+sum+ms+"</span></div>");
}
html.push("</body></html>");
return html.join("\n");
}
// Given results and an outputPath, writes report.json and report.html
function outputReport(results, outputPath) {
var jsonPath = path.resolve(outputPath, 'report.json'),
htmlPath = path.resolve(outputPath, 'report.html'),
report = buildReportData(results),
html = generateHTML(report);
fs.writeFileSync(jsonPath, JSON.stringify(report, null, ' '), 'utf8');
fs.writeFileSync(htmlPath, html, 'utf8');
}
module.exports = {
buildReportData: buildReportData,
generateHTML: generateHTML,
outputReport: outputReport
};