summaryrefslogtreecommitdiff
path: root/benchmark/net/tcp-raw-c2s.js
blob: e3c726e45294544e6c99675577130a40aa5dde92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// In this benchmark, we connect a client to the server, and write
// as many bytes as we can in the specified time (default = 10s)

var common = require('../common.js');

// if there are --dur=N and --len=N args, then
// run the function with those settings.
// if not, then queue up a bunch of child processes.
var bench = common.createBenchmark(main, {
  len: [102400, 1024 * 1024 * 16],
  type: ['utf', 'asc', 'buf'],
  dur: [5]
});

var TCP = process.binding('tcp_wrap').TCP;
var PORT = common.PORT;

var dur;
var len;
var type;

function main(conf) {
  dur = +conf.dur;
  len = +conf.len;
  type = conf.type;
  server();
}


function fail(syscall) {
  var e = new Error(syscall + ' ' + errno);
  e.errno = e.code = errno;
  e.syscall = syscall;
  throw e;
}

function server() {
  var serverHandle = new TCP();
  var r = serverHandle.bind('127.0.0.1', PORT);
  if (r)
    fail('bind');

  var r = serverHandle.listen(511);
  if (r)
    fail('listen');

  serverHandle.onconnection = function(clientHandle) {
    if (!clientHandle)
      fail('connect');

    // the meat of the benchmark is right here:
    bench.start();
    var bytes = 0;

    setTimeout(function() {
      // report in Gb/sec
      bench.end((bytes * 8) / (1024 * 1024 * 1024));
    }, dur * 1000);

    clientHandle.onread = function(buffer) {
      // we're not expecting to ever get an EOF from the client.
      // just lots of data forever.
      if (!buffer)
        fail('read');

      // don't slice the buffer.  the point of this is to isolate, not
      // simulate real traffic.
      bytes += buffer.length;
    };

    clientHandle.readStart();
  };

  client();
}

function client() {
  var chunk;
  switch (type) {
    case 'buf':
      chunk = new Buffer(len);
      chunk.fill('x');
      break;
    case 'utf':
      chunk = new Array(len / 2 + 1).join('ü');
      break;
    case 'asc':
      chunk = new Array(len + 1).join('x');
      break;
    default:
      throw new Error('invalid type: ' + type);
      break;
  }

  var clientHandle = new TCP();
  var connectReq = clientHandle.connect('127.0.0.1', PORT);

  if (!connectReq)
    fail('connect');

  clientHandle.readStart();

  connectReq.oncomplete = function() {
    while (clientHandle.writeQueueSize === 0)
      write();
  };

  function write() {
    var writeReq
    switch (type) {
      case 'buf':
        writeReq = clientHandle.writeBuffer(chunk);
        break;
      case 'utf':
        writeReq = clientHandle.writeUtf8String(chunk);
        break;
      case 'asc':
        writeReq = clientHandle.writeAsciiString(chunk);
        break;
    }

    if (!writeReq)
      fail('write');

    writeReq.oncomplete = afterWrite;
  }

  function afterWrite(status, handle, req) {
    if (status)
      fail('write');

    while (clientHandle.writeQueueSize === 0)
      write();
  }
}