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 }; 26 27 const server = tls.createServer(options, onConnection); 28 server.listen(common.PORT, onListening); 29} 30 31function onListening() { 32 setTimeout(done, dur * 1000); 33 bench.start(); 34 for (let i = 0; i < concurrency; i++) 35 makeConnection(); 36} 37 38function onConnection(conn) { 39 serverConn++; 40} 41 42function makeConnection() { 43 const options = { 44 port: common.PORT, 45 rejectUnauthorized: false 46 }; 47 const conn = tls.connect(options, () => { 48 clientConn++; 49 conn.on('error', (er) => { 50 console.error('client error', er); 51 throw er; 52 }); 53 conn.end(); 54 if (running) makeConnection(); 55 }); 56} 57 58function done() { 59 running = false; 60 // It's only an established connection if they both saw it. 61 // because we destroy the server somewhat abruptly, these 62 // don't always match. Generally, serverConn will be 63 // the smaller number, but take the min just to be sure. 64 bench.end(Math.min(serverConn, clientConn)); 65 process.exit(0); 66} 67