1'use strict';
2const fixtures = require('../../test/common/fixtures');
3const tls = require('tls');
4
5const common = require('../common.js');
6const bench = common.createBenchmark(main, {
7  concurrency: [1, 10],
8  dur: [5],
9});
10
11let clientConn = 0;
12let serverConn = 0;
13let dur;
14let concurrency;
15let running = true;
16
17function main(conf) {
18  dur = conf.dur;
19  concurrency = conf.concurrency;
20  const options = {
21    key: fixtures.readKey('rsa_private.pem'),
22    cert: fixtures.readKey('rsa_cert.crt'),
23    ca: fixtures.readKey('rsa_ca.crt'),
24    ciphers: 'AES256-GCM-SHA384',
25    maxVersion: 'TLSv1.2',
26  };
27
28  const server = tls.createServer(options, onConnection);
29  server.listen(common.PORT, onListening);
30}
31
32function onListening() {
33  setTimeout(done, dur * 1000);
34  bench.start();
35  for (let i = 0; i < concurrency; i++)
36    makeConnection();
37}
38
39function onConnection(conn) {
40  serverConn++;
41}
42
43function makeConnection() {
44  const options = {
45    port: common.PORT,
46    rejectUnauthorized: false,
47  };
48  const conn = tls.connect(options, () => {
49    clientConn++;
50    conn.on('error', (er) => {
51      console.error('client error', er);
52      throw er;
53    });
54    conn.end();
55    if (running) makeConnection();
56  });
57}
58
59function done() {
60  running = false;
61  // It's only an established connection if they both saw it.
62  // because we destroy the server somewhat abruptly, these
63  // don't always match.  Generally, serverConn will be
64  // the smaller number, but take the min just to be sure.
65  bench.end(Math.min(serverConn, clientConn));
66  process.exit(0);
67}
68