| 12 | }); |
| 13 | |
| 14 | function main({ dur, len, type, method }) { |
| 15 | let encoding; |
| 16 | let chunk; |
| 17 | switch (type) { |
| 18 | case 'buf': |
| 19 | chunk = Buffer.alloc(len, 'x'); |
| 20 | break; |
| 21 | case 'utf': |
| 22 | encoding = 'utf8'; |
| 23 | chunk = 'ü'.repeat(len / 2); |
| 24 | break; |
| 25 | case 'asc': |
| 26 | chunk = 'a'.repeat(len); |
| 27 | break; |
| 28 | } |
| 29 | |
| 30 | let nreqs = 0; |
| 31 | const options = { |
| 32 | headers: { 'Connection': 'keep-alive', 'Transfer-Encoding': 'chunked' }, |
| 33 | agent: new http.Agent({ maxSockets: 1 }), |
| 34 | host: '127.0.0.1', |
| 35 | path: '/', |
| 36 | method: 'POST', |
| 37 | }; |
| 38 | |
| 39 | const server = http.createServer((req, res) => { |
| 40 | res.end(); |
| 41 | }); |
| 42 | server.listen(0, options.host, () => { |
| 43 | setTimeout(done, dur * 1000); |
| 44 | bench.start(); |
| 45 | pummel(server.address().port); |
| 46 | }); |
| 47 | |
| 48 | function pummel(port) { |
| 49 | options.port = port; |
| 50 | const req = http.request(options, (res) => { |
| 51 | nreqs++; |
| 52 | pummel(port); // Line up next request. |
| 53 | res.resume(); |
| 54 | }); |
| 55 | if (method === 'write') { |
| 56 | req.write(chunk, encoding); |
| 57 | req.end(); |
| 58 | } else { |
| 59 | req.end(chunk, encoding); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | function done() { |
| 64 | bench.end(nreqs); |
| 65 | process.exit(0); |
| 66 | } |
| 67 | } |