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