2018-12-03 17:15:45 +01:00
|
|
|
// Test UDP send/recv throughput with the "old" offset/length API
|
2016-01-29 14:18:27 +01:00
|
|
|
'use strict';
|
2013-03-20 16:53:23 +01:00
|
|
|
|
2016-01-29 14:18:27 +01:00
|
|
|
const common = require('../common.js');
|
2017-12-30 04:00:35 +01:00
|
|
|
const dgram = require('dgram');
|
2016-01-29 14:18:27 +01:00
|
|
|
const PORT = common.PORT;
|
2013-03-20 16:53:23 +01:00
|
|
|
|
|
|
|
// `num` is the number of send requests to queue up each time.
|
|
|
|
// Keep it reasonably high (>10) otherwise you're benchmarking the speed of
|
|
|
|
// event loop cycles more than anything else.
|
2017-09-14 03:48:53 +02:00
|
|
|
const bench = common.createBenchmark(main, {
|
2013-03-20 16:53:23 +01:00
|
|
|
len: [1, 64, 256, 1024],
|
|
|
|
num: [100],
|
|
|
|
type: ['send', 'recv'],
|
|
|
|
dur: [5]
|
|
|
|
});
|
|
|
|
|
2017-12-30 04:00:35 +01:00
|
|
|
function main({ dur, len, num, type }) {
|
|
|
|
const chunk = Buffer.allocUnsafe(len);
|
2020-01-03 14:37:27 +01:00
|
|
|
let sent = 0;
|
|
|
|
let received = 0;
|
2017-09-14 03:48:53 +02:00
|
|
|
const socket = dgram.createSocket('udp4');
|
2013-03-20 16:53:23 +01:00
|
|
|
|
|
|
|
function onsend() {
|
2017-06-20 23:37:00 +02:00
|
|
|
if (sent++ % num === 0) {
|
2019-10-02 23:26:51 +02:00
|
|
|
// The setImmediate() is necessary to have event loop progress on OSes
|
|
|
|
// that only perform synchronous I/O on nonblocking UDP sockets.
|
|
|
|
setImmediate(() => {
|
2020-01-03 14:37:27 +01:00
|
|
|
for (let i = 0; i < num; i++) {
|
2019-10-02 23:26:51 +02:00
|
|
|
socket.send(chunk, 0, chunk.length, PORT, '127.0.0.1', onsend);
|
|
|
|
}
|
|
|
|
});
|
2017-06-20 23:37:00 +02:00
|
|
|
}
|
2013-03-20 16:53:23 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 07:06:08 +01:00
|
|
|
socket.on('listening', () => {
|
2013-03-20 16:53:23 +01:00
|
|
|
bench.start();
|
|
|
|
onsend();
|
|
|
|
|
2019-02-05 07:06:08 +01:00
|
|
|
setTimeout(() => {
|
2017-09-14 03:48:53 +02:00
|
|
|
const bytes = (type === 'send' ? sent : received) * chunk.length;
|
|
|
|
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
|
2013-03-20 16:53:23 +01:00
|
|
|
bench.end(gbits);
|
2016-05-31 21:49:16 +02:00
|
|
|
process.exit(0);
|
2013-03-20 16:53:23 +01:00
|
|
|
}, dur * 1000);
|
|
|
|
});
|
|
|
|
|
2019-02-05 07:06:08 +01:00
|
|
|
socket.on('message', () => {
|
2013-03-20 16:53:23 +01:00
|
|
|
received++;
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.bind(PORT);
|
|
|
|
}
|