| 19 | }); |
| 20 | |
| 21 | function main({ len, type, method, c, duration }) { |
| 22 | const http = require('http'); |
| 23 | let chunk; |
| 24 | switch (type) { |
| 25 | case 'buf': |
| 26 | chunk = Buffer.alloc(len, 'x'); |
| 27 | break; |
| 28 | case 'utf': |
| 29 | chunk = 'ü'.repeat(len / 2); |
| 30 | break; |
| 31 | case 'asc': |
| 32 | chunk = 'a'.repeat(len); |
| 33 | break; |
| 34 | } |
| 35 | |
| 36 | function write(res) { |
| 37 | res.write(chunk); |
| 38 | res.end(); |
| 39 | } |
| 40 | |
| 41 | function end(res) { |
| 42 | res.end(chunk); |
| 43 | } |
| 44 | |
| 45 | const fn = method === 'write' ? write : end; |
| 46 | |
| 47 | const server = http.createServer((req, res) => { |
| 48 | fn(res); |
| 49 | }); |
| 50 | |
| 51 | server.listen(0, () => { |
| 52 | bench.http({ |
| 53 | connections: c, |
| 54 | duration, |
| 55 | port: server.address().port, |
| 56 | }, () => { |
| 57 | server.close(); |
| 58 | }); |
| 59 | }); |
| 60 | } |