(opts, callback)
| 48 | |
| 49 | |
| 50 | const runAb = (opts, callback) => { |
| 51 | const args = [ |
| 52 | '-c', opts.concurrent || 50, |
| 53 | '-t', opts.threads || 2, |
| 54 | '-d', opts.duration || '5s', |
| 55 | ]; |
| 56 | |
| 57 | if (!opts.keepalive) { |
| 58 | args.push('-H'); |
| 59 | args.push('Connection: close'); |
| 60 | } |
| 61 | |
| 62 | args.push(url.format({ hostname: '127.0.0.1', |
| 63 | port: opts.port, protocol: 'http' })); |
| 64 | |
| 65 | const child = child_process.spawn('wrk', args); |
| 66 | child.stderr.pipe(process.stderr); |
| 67 | child.stdout.setEncoding('utf8'); |
| 68 | |
| 69 | let stdout; |
| 70 | |
| 71 | child.stdout.on('data', (data) => stdout += data); |
| 72 | |
| 73 | child.on('close', (code, signal) => { |
| 74 | if (code) { |
| 75 | console.error(code, signal); |
| 76 | process.exit(code); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | let matches = /Requests\/sec:\s*(\d+)\./i.exec(stdout); |
| 81 | const reqSec = parseInt(matches[1]); |
| 82 | |
| 83 | matches = /Keep-Alive requests:\s*(\d+)/i.exec(stdout); |
| 84 | let keepAliveRequests; |
| 85 | if (matches) { |
| 86 | keepAliveRequests = parseInt(matches[1]); |
| 87 | } else { |
| 88 | keepAliveRequests = 0; |
| 89 | } |
| 90 | |
| 91 | callback(reqSec, keepAliveRequests); |
| 92 | }); |
| 93 | }; |
| 94 | |
| 95 | server.listen(0, () => { |
| 96 | const port = server.address().port; |
no test coverage detected