11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst fixtures = require('../../test/common/fixtures');
31cb0ef41Sopenharmony_ciconst tls = require('tls');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common.js');
61cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
71cb0ef41Sopenharmony_ci  concurrency: [1, 10],
81cb0ef41Sopenharmony_ci  dur: [5],
91cb0ef41Sopenharmony_ci});
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cilet clientConn = 0;
121cb0ef41Sopenharmony_cilet serverConn = 0;
131cb0ef41Sopenharmony_cilet dur;
141cb0ef41Sopenharmony_cilet concurrency;
151cb0ef41Sopenharmony_cilet running = true;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cifunction main(conf) {
181cb0ef41Sopenharmony_ci  dur = conf.dur;
191cb0ef41Sopenharmony_ci  concurrency = conf.concurrency;
201cb0ef41Sopenharmony_ci  const options = {
211cb0ef41Sopenharmony_ci    key: fixtures.readKey('rsa_private.pem'),
221cb0ef41Sopenharmony_ci    cert: fixtures.readKey('rsa_cert.crt'),
231cb0ef41Sopenharmony_ci    ca: fixtures.readKey('rsa_ca.crt'),
241cb0ef41Sopenharmony_ci    ciphers: 'AES256-GCM-SHA384',
251cb0ef41Sopenharmony_ci    maxVersion: 'TLSv1.2',
261cb0ef41Sopenharmony_ci  };
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const server = tls.createServer(options, onConnection);
291cb0ef41Sopenharmony_ci  server.listen(common.PORT, onListening);
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cifunction onListening() {
331cb0ef41Sopenharmony_ci  setTimeout(done, dur * 1000);
341cb0ef41Sopenharmony_ci  bench.start();
351cb0ef41Sopenharmony_ci  for (let i = 0; i < concurrency; i++)
361cb0ef41Sopenharmony_ci    makeConnection();
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cifunction onConnection(conn) {
401cb0ef41Sopenharmony_ci  serverConn++;
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_cifunction makeConnection() {
441cb0ef41Sopenharmony_ci  const options = {
451cb0ef41Sopenharmony_ci    port: common.PORT,
461cb0ef41Sopenharmony_ci    rejectUnauthorized: false,
471cb0ef41Sopenharmony_ci  };
481cb0ef41Sopenharmony_ci  const conn = tls.connect(options, () => {
491cb0ef41Sopenharmony_ci    clientConn++;
501cb0ef41Sopenharmony_ci    conn.on('error', (er) => {
511cb0ef41Sopenharmony_ci      console.error('client error', er);
521cb0ef41Sopenharmony_ci      throw er;
531cb0ef41Sopenharmony_ci    });
541cb0ef41Sopenharmony_ci    conn.end();
551cb0ef41Sopenharmony_ci    if (running) makeConnection();
561cb0ef41Sopenharmony_ci  });
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_cifunction done() {
601cb0ef41Sopenharmony_ci  running = false;
611cb0ef41Sopenharmony_ci  // It's only an established connection if they both saw it.
621cb0ef41Sopenharmony_ci  // because we destroy the server somewhat abruptly, these
631cb0ef41Sopenharmony_ci  // don't always match.  Generally, serverConn will be
641cb0ef41Sopenharmony_ci  // the smaller number, but take the min just to be sure.
651cb0ef41Sopenharmony_ci  bench.end(Math.min(serverConn, clientConn));
661cb0ef41Sopenharmony_ci  process.exit(0);
671cb0ef41Sopenharmony_ci}
68