Skip to content

Commit 3dac421

Browse files
committed
bench: add dgram send/recv benchmark
1 parent 34e22b8 commit 3dac421

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

benchmark/net/dgram.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// test UDP send/recv throughput
2+
3+
var common = require('../common.js');
4+
var PORT = common.PORT;
5+
6+
// `num` is the number of send requests to queue up each time.
7+
// Keep it reasonably high (>10) otherwise you're benchmarking the speed of
8+
// event loop cycles more than anything else.
9+
var bench = common.createBenchmark(main, {
10+
len: [1, 64, 256, 1024],
11+
num: [100],
12+
type: ['send', 'recv'],
13+
dur: [5]
14+
});
15+
16+
var dur;
17+
var len;
18+
var num;
19+
var type;
20+
var chunk;
21+
var encoding;
22+
23+
function main(conf) {
24+
dur = +conf.dur;
25+
len = +conf.len;
26+
num = +conf.num;
27+
type = conf.type;
28+
chunk = new Buffer(len);
29+
server();
30+
}
31+
32+
var dgram = require('dgram');
33+
34+
function server() {
35+
var sent = 0;
36+
var received = 0;
37+
var socket = dgram.createSocket('udp4');
38+
39+
function onsend() {
40+
if (sent++ % num == 0)
41+
for (var i = 0; i < num; i++)
42+
socket.send(chunk, 0, chunk.length, PORT, '127.0.0.1', onsend);
43+
}
44+
45+
socket.on('listening', function() {
46+
bench.start();
47+
onsend();
48+
49+
setTimeout(function() {
50+
var bytes = (type === 'send' ? sent : received) * chunk.length;
51+
var gbits = (bytes * 8) / (1024 * 1024 * 1024);
52+
bench.end(gbits);
53+
}, dur * 1000);
54+
});
55+
56+
socket.on('message', function(buf, rinfo) {
57+
received++;
58+
});
59+
60+
socket.bind(PORT);
61+
}

0 commit comments

Comments
 (0)