forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtls-connect.js
67 lines (59 loc) · 1.53 KB
/
tls-connect.js
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
'use strict';
const fixtures = require('../../test/common/fixtures');
const tls = require('tls');
const common = require('../common.js');
const bench = common.createBenchmark(main, {
concurrency: [1, 10],
dur: [5],
});
let clientConn = 0;
let serverConn = 0;
let dur;
let concurrency;
let running = true;
function main(conf) {
dur = conf.dur;
concurrency = conf.concurrency;
const options = {
key: fixtures.readKey('rsa_private.pem'),
cert: fixtures.readKey('rsa_cert.crt'),
ca: fixtures.readKey('rsa_ca.crt'),
ciphers: 'AES256-GCM-SHA384',
maxVersion: 'TLSv1.2',
};
const server = tls.createServer(options, onConnection);
server.listen(common.PORT, onListening);
}
function onListening() {
setTimeout(done, dur * 1000);
bench.start();
for (let i = 0; i < concurrency; i++)
makeConnection();
}
function onConnection(conn) {
serverConn++;
}
function makeConnection() {
const options = {
port: common.PORT,
rejectUnauthorized: false,
};
const conn = tls.connect(options, () => {
clientConn++;
conn.on('error', (er) => {
console.error('client error', er);
throw er;
});
conn.end();
if (running) makeConnection();
});
}
function done() {
running = false;
// It's only an established connection if they both saw it.
// because we destroy the server somewhat abruptly, these
// don't always match. Generally, serverConn will be
// the smaller number, but take the min just to be sure.
bench.end(Math.min(serverConn, clientConn));
process.exit(0);
}