• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Test UDP send/recv throughput with the "old" offset/length API
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: [1, 64, 256, 1024],
13  num: [100],
14  type: ['send', 'recv'],
15  dur: [5]
16});
17
18function main({ dur, len, num, type }) {
19  const chunk = Buffer.allocUnsafe(len);
20  let sent = 0;
21  let received = 0;
22  const socket = dgram.createSocket('udp4');
23
24  function onsend() {
25    if (sent++ % num === 0) {
26      // The setImmediate() is necessary to have event loop progress on OSes
27      // that only perform synchronous I/O on nonblocking UDP sockets.
28      setImmediate(() => {
29        for (let i = 0; i < num; i++) {
30          socket.send(chunk, 0, chunk.length, PORT, '127.0.0.1', onsend);
31        }
32      });
33    }
34  }
35
36  socket.on('listening', () => {
37    bench.start();
38    onsend();
39
40    setTimeout(() => {
41      const bytes = (type === 'send' ? sent : received) * chunk.length;
42      const gbits = (bytes * 8) / (1024 * 1024 * 1024);
43      bench.end(gbits);
44      process.exit(0);
45    }, dur * 1000);
46  });
47
48  socket.on('message', () => {
49    received++;
50  });
51
52  socket.bind(PORT);
53}
54