({ dur, len, n, type, chunks })
| 17 | }); |
| 18 | |
| 19 | function main({ dur, len, n, type, chunks }) { |
| 20 | const chunk = []; |
| 21 | for (let i = 0; i < chunks; i++) { |
| 22 | chunk.push(Buffer.allocUnsafe(Math.round(len / chunks))); |
| 23 | } |
| 24 | |
| 25 | // Server |
| 26 | let sent = 0; |
| 27 | const socket = dgram.createSocket('udp4'); |
| 28 | const onsend = type === 'concat' ? onsendConcat : onsendMulti; |
| 29 | |
| 30 | function onsendConcat() { |
| 31 | if (sent++ % n === 0) { |
| 32 | // The setImmediate() is necessary to have event loop progress on OSes |
| 33 | // that only perform synchronous I/O on nonblocking UDP sockets. |
| 34 | setImmediate(() => { |
| 35 | for (let i = 0; i < n; i++) { |
| 36 | socket.send(Buffer.concat(chunk), PORT, '127.0.0.1', onsend); |
| 37 | } |
| 38 | }); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | function onsendMulti() { |
| 43 | if (sent++ % n === 0) { |
| 44 | // The setImmediate() is necessary to have event loop progress on OSes |
| 45 | // that only perform synchronous I/O on nonblocking UDP sockets. |
| 46 | setImmediate(() => { |
| 47 | for (let i = 0; i < n; i++) { |
| 48 | socket.send(chunk, PORT, '127.0.0.1', onsend); |
| 49 | } |
| 50 | }); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | socket.on('listening', () => { |
| 55 | bench.start(); |
| 56 | onsend(); |
| 57 | |
| 58 | setTimeout(() => { |
| 59 | const bytes = sent * len; |
| 60 | const gbits = (bytes * 8) / (1024 * 1024 * 1024); |
| 61 | bench.end(gbits); |
| 62 | process.exit(0); |
| 63 | }, dur * 1000); |
| 64 | }); |
| 65 | |
| 66 | socket.bind(PORT); |
| 67 | } |
nothing calls this directly
no test coverage detected