1// Test UDP send throughput with the multi buffer API against Buffer.concat 2'use strict'; 3 4const common = require('../common.js'); 5const dgram = require('dgram'); 6const PORT = common.PORT; 7 8// `num` is the number of send requests to queue up each time. 9// Keep it reasonably high (>10) otherwise you're benchmarking the speed of 10// event loop cycles more than anything else. 11const bench = common.createBenchmark(main, { 12 len: [64, 256, 512, 1024], 13 num: [100], 14 chunks: [1, 2, 4, 8], 15 type: ['concat', 'multi'], 16 dur: [5] 17}); 18 19function main({ dur, len, num, 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++ % num === 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 < num; i++) { 36 socket.send(Buffer.concat(chunk), PORT, '127.0.0.1', onsend); 37 } 38 }); 39 } 40 } 41 42 function onsendMulti() { 43 if (sent++ % num === 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 < num; 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} 68