summaryrefslogtreecommitdiff
path: root/benchmark/tls/throughput.js
blob: cbc5ffc0fba188ae35adf14abe949b33feffb729 (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
'use strict';
var common = require('../common.js');
var bench = common.createBenchmark(main, {
  dur: [5],
  type: ['buf', 'asc', 'utf'],
  size: [2, 1024, 1024 * 1024]
});

var dur, type, encoding, size;
var server;

var path = require('path');
var fs = require('fs');
var cert_dir = path.resolve(__dirname, '../../test/fixtures');
var options;
var tls = require('tls');

function main(conf) {
  dur = +conf.dur;
  type = conf.type;
  size = +conf.size;

  var chunk;
  switch (type) {
    case 'buf':
      chunk = new Buffer(size);
      chunk.fill('b');
      break;
    case 'asc':
      chunk = new Array(size + 1).join('a');
      encoding = 'ascii';
      break;
    case 'utf':
      chunk = new Array(size / 2 + 1).join('ü');
      encoding = 'utf8';
      break;
    default:
      throw new Error('invalid type');
  }

  options = { key: fs.readFileSync(cert_dir + '/test_key.pem'),
              cert: fs.readFileSync(cert_dir + '/test_cert.pem'),
              ca: [ fs.readFileSync(cert_dir + '/test_ca.pem') ],
              ciphers: 'AES256-GCM-SHA384' };

  server = tls.createServer(options, onConnection);
  setTimeout(done, dur * 1000);
  server.listen(common.PORT, function() {
    var opt = { port: common.PORT, rejectUnauthorized: false };
    var conn = tls.connect(opt, function() {
      bench.start();
      conn.on('drain', write);
      write();
    });

    function write() {
      while (false !== conn.write(chunk, encoding));
    }
  });

  var received = 0;
  function onConnection(conn) {
    conn.on('data', function(chunk) {
      received += chunk.length;
    });
  }

  function done() {
    var mbits = (received * 8) / (1024 * 1024);
    bench.end(mbits);
    conn.destroy();
    server.close();
  }
}