blob: 2664e25fe003a036ae7b85e318eae8bef4dfd8ab [file] [log] [blame]
isaacse82f9742013-02-08 03:13:261// In this benchmark, we connect a client to the server, and write
2// as many bytes as we can in the specified time (default = 10s)
3
4var common = require('../common.js');
5
6// if there are --dur=N and --len=N args, then
7// run the function with those settings.
8// if not, then queue up a bunch of child processes.
9var bench = common.createBenchmark(main, {
10 len: [102400, 1024 * 1024 * 16],
11 type: ['utf', 'asc', 'buf'],
12 dur: [1, 3],
13});
14
15var TCP = process.binding('tcp_wrap').TCP;
16var PORT = common.PORT;
17
18var dur;
19var len;
20var type;
21
22function main(conf) {
23 dur = +conf.dur;
24 len = +conf.len;
25 type = conf.type;
26 server();
27}
28
29
30function fail(syscall) {
31 var e = new Error(syscall + ' ' + errno);
32 e.errno = e.code = errno;
33 e.syscall = syscall;
34 throw e;
35}
36
37function server() {
38 var serverHandle = new TCP();
39 var r = serverHandle.bind('127.0.0.1', PORT);
40 if (r)
41 fail('bind');
42
43 var r = serverHandle.listen(511);
44 if (r)
45 fail('listen');
46
47 serverHandle.onconnection = function(clientHandle) {
48 if (!clientHandle)
49 fail('connect');
50
51 // the meat of the benchmark is right here:
52 bench.start();
53 var bytes = 0;
54
55 setTimeout(function() {
56 // report in Gb/sec
57 bench.end((bytes * 8) / (1024 * 1024 * 1024));
58 }, dur * 1000);
59
60 clientHandle.onread = function(buffer, offset, length) {
61 // we're not expecting to ever get an EOF from the client.
62 // just lots of data forever.
63 if (!buffer)
64 fail('read');
65
66 // don't slice the buffer. the point of this is to isolate, not
67 // simulate real traffic.
68 // var chunk = buffer.slice(offset, offset + length);
69 bytes += length;
70 };
71
72 clientHandle.readStart();
73 };
74
75 client();
76}
77
78function client() {
79 var chunk;
80 switch (type) {
81 case 'buf':
82 chunk = new Buffer(len);
83 chunk.fill('x');
84 break;
85 case 'utf':
86 chunk = new Array(len / 2 + 1).join('ΓΌ');
87 break;
88 case 'asc':
89 chunk = new Array(len + 1).join('x');
90 break;
91 default:
92 throw new Error('invalid type: ' + type);
93 break;
94 }
95
96 var clientHandle = new TCP();
97 var connectReq = clientHandle.connect('127.0.0.1', PORT);
98
99 if (!connectReq)
100 fail('connect');
101
102 clientHandle.readStart();
103
104 connectReq.oncomplete = function() {
105 while (clientHandle.writeQueueSize === 0)
106 write();
107 };
108
109 function write() {
110 var writeReq
111 switch (type) {
112 case 'buf':
113 writeReq = clientHandle.writeBuffer(chunk);
114 break;
115 case 'utf':
116 writeReq = clientHandle.writeUtf8String(chunk);
117 break;
118 case 'asc':
119 writeReq = clientHandle.writeAsciiString(chunk);
120 break;
121 }
122
123 if (!writeReq)
124 fail('write');
125
126 writeReq.oncomplete = afterWrite;
127 }
128
129 function afterWrite(status, handle, req) {
130 if (status)
131 fail('write');
132
133 while (clientHandle.writeQueueSize === 0)
134 write();
135 }
136}