-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
52 lines (46 loc) · 1.29 KB
/
index.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
var execFile = require('child_process').execFile;
var parseWrk = require('./lib/parseWrk');
var util = require("util");
function wrk(opts, callback) {
var cmd = opts.path || 'wrk';
if (opts.threads)
cmd += ' -t' + opts.threads;
if (opts.connections)
cmd += ' -c' + opts.connections;
if (opts.duration)
cmd += ' -d' + opts.duration;
if (opts.script)
cmd += ' -s' + opts.script;
if (opts.timeout)
cmd += ' --timeout ' + opts.timeout;
if (opts.rate)
cmd += ' -R' + opts.rate;
if (opts.printLatency)
cmd += ' --latency ';
if (opts.headers) {
Object.keys(opts.headers).forEach(function(key) {
cmd += util.format(' -H \'%s: %s\'', key, opts.headers[key]);
})
}
// the caller may have requested that we pass options to the exec
const { execOptions={} } = opts
cmd += opts.url;
opts.debug && console.log(cmd);
cmd = cmd.split(' ')
var child = execFile(cmd[0], cmd.splice(1), execOptions, function(error, stdout, stderr) {
if (opts.debug) {
stdout && console.log(stdout);
stderr && console.error(stderr);
}
if (error) {
return callback(error);
}
callback(null, parseWrk(stdout));
});
}
module.exports = wrk;
module.exports.co = function(opts) {
return function(callback) {
wrk(opts, callback);
}
};