-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
129 lines (116 loc) · 3.17 KB
/
app.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
/* jshint node:true */
var argv = require('yargs').argv;
var args = argv._;
var fs = require('fs');
var async = require('async');
var request = require('request');
var clf = require('clf-parser');
var http = require('http');
var parallel = argv['max-sockets'] || 200;
var timeout = argv['timeout'] || 60000;
http.globalAgent.maxSockets = parallel;
if (args.length !== 2) {
console.error('Usage: holodeck http://example.com clf-log-file.log [--max-sockets=200] [--ignore-extensions=gif,jpg,png,js,css] [--timeout=60000] [--verbose] [--speed=1]');
process.exit(1);
}
if (argv['ignore-static']) {
argv['ignore-extensions'] = 'gif,jpg,png,js,xlx,pptx,docx,css,ico,pdf';
}
var input = fs.readFileSync(args[1], 'utf8');
var lines = input.split(/\n/);
var infos = [];
lines.forEach(function(line) {
var info = parse(line);
if (!info) {
return;
}
infos.push(info);
});
infos.sort(function(a, b) {
if (a.time_local < b.time_local) {
return -1;
}
if (a.time_local > b.time_local) {
return 1;
}
return 0;
});
var offset;
var statuses = {};
var totalTime = 0;
var ignore;
if (argv['ignore-extensions']) {
ignore = new RegExp('\\.(' + argv['ignore-extensions'].replace(/,/g, '|') + ')$');
}
var valid = 0;
var speed = 1;
if (argv.speed) {
speed = parseFloat(argv.speed);
}
return async.eachLimit(infos, parallel, function(info, callback) {
if (info.method !== 'GET') {
return setImmediate(callback);
}
if (ignore) {
if (info.path.match(ignore)) {
return setImmediate(callback);
}
}
var now = Date.now();
if (!offset) {
offset = now - info.time_local.getTime();
}
var wait = ((info.time_local.getTime() + offset) - now) / speed;
// if (wait < 0) {
// console.log('BEHIND: ' + (-wait) + 'ms');
// }
valid++;
return setTimeout(function() {
var now = Date.now();
if (argv.verbose) {
console.log('GETTING ' + info.path);
}
return request({
url: args[0] + info.path,
timeout: timeout
}, function(err, response, body) {
var after = Date.now();
var elapsed = after - now;
if (err) {
if (err.code === 'ETIMEDOUT') {
response = { statusCode: 'ETIMEDOUT' };
body = '';
} else {
return callback(err);
}
}
console.log(info.method + ' ' + info.path + ': ' + response.statusCode + ', ' + body.length + ' bytes (' + elapsed + 'ms)');
totalTime += elapsed;
if (!statuses[response.statusCode]) {
statuses[response.statusCode] = 0;
}
statuses[response.statusCode]++;
return callback(null);
});
}, (wait > 0) ? wait : 0);
}, function(err) {
if (err) {
throw err;
}
console.log('\n\nDone.');
console.log('Requests made: ' + valid);
console.log('Average response time: ' + twoPlaces(totalTime / valid) + 'ms');
var keys = Object.keys(statuses);
keys.sort();
console.log('Final status codes:');
keys.forEach(function(status) {
console.log(status + ': ' + statuses[status] + ' (' + twoPlaces(statuses[status] / valid * 100) + '%' + ')');
});
process.exit(0);
});
function parse(line) {
return clf(line);
}
function twoPlaces(n) {
return Math.round(Math.floor(n * 100)) / 100;
}