• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3const bench = common.createBenchmark(main, {
4  dur: [5],
5  type: ['buf', 'asc', 'utf'],
6  size: [100, 1024, 1024 * 1024, 4 * 1024 * 1024, 16 * 1024 * 1024]
7});
8
9const fixtures = require('../../test/common/fixtures');
10let options;
11const tls = require('tls');
12
13function main({ dur, type, size }) {
14  let encoding;
15  let chunk;
16  switch (type) {
17    case 'buf':
18      chunk = Buffer.alloc(size, 'b');
19      break;
20    case 'asc':
21      chunk = 'a'.repeat(size);
22      encoding = 'ascii';
23      break;
24    case 'utf':
25      chunk = 'ü'.repeat(size / 2);
26      encoding = 'utf8';
27      break;
28    default:
29      throw new Error('invalid type');
30  }
31
32  options = {
33    key: fixtures.readKey('rsa_private.pem'),
34    cert: fixtures.readKey('rsa_cert.crt'),
35    ca: fixtures.readKey('rsa_ca.crt'),
36    ciphers: 'AES256-GCM-SHA384'
37  };
38
39  const server = tls.createServer(options, onConnection);
40  let conn;
41  server.listen(common.PORT, () => {
42    const opt = { port: common.PORT, rejectUnauthorized: false };
43    conn = tls.connect(opt, () => {
44      setTimeout(done, dur * 1000);
45      bench.start();
46      conn.on('drain', write);
47      write();
48    });
49
50    function write() {
51      while (false !== conn.write(chunk, encoding));
52    }
53  });
54
55  let received = 0;
56  function onConnection(conn) {
57    conn.on('data', (chunk) => {
58      received += chunk.length;
59    });
60  }
61
62  function done() {
63    const mbits = (received * 8) / (1024 * 1024);
64    bench.end(mbits);
65    if (conn)
66      conn.destroy();
67    server.close();
68  }
69}
70