forked from macbre/phantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-multiple.js
executable file
·207 lines (175 loc) · 4.99 KB
/
run-multiple.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env node
/*jshint -W083: false, -W099: false */
/**
* This is a helper NodeJS script allowing you to run phantomas multiple times and
* get a nice looking table with all the metrics + avg / median / min / max values
*
* Usage:
* ./run-multiple.js
* --url=<page to check>
* --runs=<number of runs, defaults to 3>
* --timeout=<in seconds (for each run), default to 15>
* [--modules=moduleOne,moduleTwo]
* [--skip-modules=moduleOne,moduleTwo]
* [--format=plain|json] (plain is default)
* note: json format, prints only errorrs or the
* results in json format, no other
* messaging
*
* @version 0.2
*/
var exec = require('child_process').exec,
phantomjs = require('phantomjs'),
VERSION = require('./package').version,
args = process.argv.slice(1),
params = require('./lib/args').parse(args),
pads = require('./core/pads'),
lpad = pads.lpad,
rpad = pads.rpad;
// handle --url and --runs CLI parameters
var url = params.url,
runs = parseInt(params.runs, 10) || 3,
format = params.format || 'plain',
remainingRuns = runs,
metrics = [];
function runPhantomas(params, callback) {
var timeMs = Date.now(),
cmd = [
__dirname + '/phantomas.js',
'--format json',
'--url "' + params.url + '"'
];
if (params.timeout > 0) {
cmd.push(' --timeout ' + params.timeout);
}
if (params.modules) {
cmd.push(' --modules ' + params.modules);
}
if (params['skip-modules']) {
cmd.push(' --skip-modules ' + params['skip-modules']);
}
// @see http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
exec(cmd.join(' '), function(error, stdout, stderr) {
var res = false;
if (stderr) {
console.log("Error from phantomas: " + stderr);
process.exit(2);
}
try {
res = JSON.parse(stdout) || false;
} catch(e) {
console.log("Unable to parse JSON from phantomas!");
}
if (res === false) {
console.log(stdout);
}
if (typeof callback === 'function') {
callback(res, Date.now() - timeMs);
}
});
}
function run() {
if (remainingRuns--) {
if (format === 'plain') {
console.log('Remaining runs: ' + (remainingRuns + 1));
}
runPhantomas(params, function(res, timeMs) {
if (res) {
metrics.push(res.metrics);
}
if (format === 'plain') {
console.log('Run completed in ' + (timeMs/1000).toFixed(2) + ' s');
}
run();
});
} else {
if (format === 'plain') {
console.log('Done');
}
formatResults(metrics);
}
}
function formatResults(metrics) {
var entries = {},
entry,
metric;
// prepare entries
for(metric in metrics[0]) {
entries[metric] = {
values: [],
sum: 0,
min: 0,
max: 0,
median: undefined,
average: undefined
};
}
// process all runs
metrics.forEach(function(data) {
var metric;
for (metric in data) {
entries[metric].values.push(data[metric]);
}
});
// calculate stats
for (metric in entries) {
entry = entries[metric];
if (typeof entry.values[0] === 'string') {
// don't sort metric with string value
}
else {
entry.values = entry.values.
filter(function(element) {
return element !== null;
}).
sort(function (a, b) {
return a - b;
});
}
if (entry.values.length === 0) {
continue;
}
entry.min = entry.values.slice(0, 1).pop();
entry.max = entry.values.slice(-1).pop();
if (typeof entry.values[0] === 'string') {
continue;
}
for (var i=0, len = entry.values.length++; i<len; i++) {
entry.sum += entry.values[i];
}
entry.average = len && (entry.sum / len).toFixed(2);
entry.median = ( (len % 2 === 0) ? ((entry.values[len >> 1] + entry.values[len >> 1 + 1])/2) : entry.values[len >> 1] ).toFixed(2);
}
// print out a nice table
if (format === 'plain') {
console.log("----------------------------------------------------------------------------------------------");
console.log("| " + rpad("Report from " + runs + " run(s) for <" + params.url + "> using phantomas v" + VERSION, 90) + " |");
console.log("----------------------------------------------------------------------------------------------");
console.log("| " + [rpad("Metric", 30), rpad("Min", 12), rpad("Max", 12), rpad("Average", 12), rpad("Median", 12)].join(" | ") + " |");
console.log("----------------------------------------------------------------------------------------------");
for (metric in entries) {
entry = entries[metric];
console.log("| "+
[
rpad(metric, 30),
lpad(entry.min, 12),
lpad(entry.max, 12),
lpad(entry.average, 12),
lpad(entry.median, 12)
].join(" | ") +
" |");
}
console.log("---------------------------------------------------------------------------------------------");
} else {
console.log(JSON.stringify(metrics));
}
}
if (typeof url === 'undefined') {
console.log('--url argument must be provided!');
process.exit(1);
}
if (format === 'plain') {
console.log('phantomas v' + VERSION + ' / PhantomJS v' + phantomjs.version + ' / nodejs ' + process.version);
console.log('Performing ' + runs + ' run(s) for <' + params.url + '>...');
}
run();