forked from jmervine/phantomjs-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.js
More file actions
executable file
·170 lines (154 loc) · 5.14 KB
/
runner.js
File metadata and controls
executable file
·170 lines (154 loc) · 5.14 KB
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
#!/usr/bin/env node
/***********************************************************
* Author: @mervinej
* Licence: MIT
* Date: 12/07/2013
***********************************************************/
var fs = require('fs');
var Phapper = require('phapper');
var async = require('async');
var program = require('commander');
program
.option('-s , --script [string]' , 'script to be run')
.option('-r , --runs [number]' , 'number of runs [1]', parseInt, 1)
.option('-u , --scriptRuns [number]' , 'number of runs [1]', parseInt, 1)
.option('-j , --json' , 'output in JSON format')
.option('-m , --median' , 'return median value');
program.on('--help', function() {
console.log(' Examples:');
console.log('');
console.log(' $ ./runner.js -s timer -m -r 9 -j "http://google.com,http://github.com"');
console.log('');
console.log(' $ ./runner.js -s grep -r 9 ./urls.txt ./strings.txt');
console.log('');
console.log(' $ ./runner.js -s ./external/external.js -j ./urls.txt ./excluded.txt');
console.log('');
console.log(' Warning:');
console.log('');
console.log(' This is optimized for json output, otherwise, display for anything aside\n from simple timer output (e.g. ready and timer) needs a lot of work.');
console.log('');
});
program.parse(process.argv);
if (!program.script) {
program.script = program.args.slice(0);
}
try {
if (!fs.statSync(program.script).isFile()) {
var s = './'+program.script+'/'+program.script+'.js';
if (fs.statSync(s).isFile()) {
script = s;
}
}else{
script=program.script;
}
} catch(e) {
console.error("ERROR: '%s' isn't a valid script", program.script);
process.exit(1);
}
var args = program.args;
if (args.indexOf('--json') === -1) {
args.push('--json');
}
var runner = new Phapper(script, args);
var runs = [];
for (var i=0; i < program.runs; i++) {
runs.push( function(callback) {
runner.run(function(json, stdio) {
if (stdio.error) {
callback(stdio.error, null);
}
if (!json) {
callback(new Error(stdio.stdout), null);
}
callback(null, json);
});
});
}
async.parallel(runs,
function(err, results) {
if (err) {
console.trace(err);
process.exit(1);
}
var sets = {};
results.forEach(function(res) {
console.log('responces '+res);
res.forEach(function(r) {
var address = r.address;
delete r.address;
sets[address] = sets[address] || {};
Object.keys(r).forEach(function(key) {
sets[address][key] = sets[address][key] || [];
sets[address][key].push(r[key]);
});
});
});
if (program.json) {
Object.keys(sets).forEach(function(set) {
Object.keys(sets[set]).forEach(function(metric) {
var result = sets[set][metric];
if (Array.isArray(result) && typeof result[0] === 'number') {
result = median(result);
}
sets[set][metric] = result;
});
});
console.log(JSON.stringify(sets, null, 2));
} else {
Object.keys(sets).forEach(function(set) {
console.log('Regarding %s:', set);
Object.keys(sets[set]).forEach(function(metric) {
var result = median(sets[set][metric]);
if (Array.isArray(result)) {
console.log('\n* %s:', metric);
crawl(result);
} else {
console.log('* %s: %s', metric, result);
}
});
console.log('');
});
}
}
);
// functions
function median(values) {
if (!program.median || !Array.isArray(values) || typeof values[0] !== 'number') {
return values;
}
values.sort( function(a,b) {return a - b;} );
var half = Math.floor(values.length/2);
return (values.length % 2) ? values[half] : (values[half-1] + values[half]) / 2.0;
}
function crawl(item, depth) {
depth = depth || 1;
if (Array.isArray(item)) {
var i = 1;
item.forEach(function(sub) {
if (Array.isArray(sub)) {
crawl('Run ['+(i++)+']', depth);
}
crawl(sub, depth+1);
});
return;
}
if (typeof item === 'object') {
Object.keys(item).forEach(function(key) {
if (typeof item[key] === 'string') {
crawl(item[key], depth);
} else if (typeof item[key] === 'boolean' ||
typeof item[key] === 'number') {
crawl(key+': '+item[key], depth+1);
} else {
crawl(item[key], depth+1);
}
});
return;
}
var spacer = '';
for (var i = 0; i < (depth*2); i++) {
spacer += ' ';
}
console.log('%s- %s', spacer, item);
return;
}