2016-02-19 17:03:16 -08:00
|
|
|
'use strict';
|
2017-09-13 22:48:53 -03:00
|
|
|
const common = require('../common.js');
|
|
|
|
const bench = common.createBenchmark(main, {
|
2013-02-19 15:03:41 -08:00
|
|
|
dur: [5],
|
2013-02-11 18:27:02 -08:00
|
|
|
type: ['buf', 'asc', 'utf'],
|
2023-01-29 20:13:35 +02:00
|
|
|
size: [100, 1024, 1024 * 1024, 4 * 1024 * 1024, 16 * 1024 * 1024],
|
2013-02-11 18:27:02 -08:00
|
|
|
});
|
|
|
|
|
2019-05-29 11:43:44 -07:00
|
|
|
const fixtures = require('../../test/common/fixtures');
|
2020-02-14 14:17:20 +01:00
|
|
|
let options;
|
2017-09-13 22:48:53 -03:00
|
|
|
const tls = require('tls');
|
2013-02-11 18:27:02 -08:00
|
|
|
|
2017-12-30 03:55:51 +01:00
|
|
|
function main({ dur, type, size }) {
|
2020-02-14 14:17:20 +01:00
|
|
|
let encoding;
|
|
|
|
let chunk;
|
2013-02-11 18:27:02 -08:00
|
|
|
switch (type) {
|
|
|
|
case 'buf':
|
2016-01-25 15:00:06 -08:00
|
|
|
chunk = Buffer.alloc(size, 'b');
|
2013-02-11 18:27:02 -08:00
|
|
|
break;
|
|
|
|
case 'asc':
|
2017-04-03 00:32:50 +03:00
|
|
|
chunk = 'a'.repeat(size);
|
2013-02-11 18:27:02 -08:00
|
|
|
encoding = 'ascii';
|
|
|
|
break;
|
|
|
|
case 'utf':
|
2017-04-03 00:32:50 +03:00
|
|
|
chunk = 'ü'.repeat(size / 2);
|
2013-02-11 18:27:02 -08:00
|
|
|
encoding = 'utf8';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('invalid type');
|
|
|
|
}
|
|
|
|
|
2016-11-24 11:43:35 -08:00
|
|
|
options = {
|
2019-05-29 11:43:44 -07:00
|
|
|
key: fixtures.readKey('rsa_private.pem'),
|
|
|
|
cert: fixtures.readKey('rsa_cert.crt'),
|
|
|
|
ca: fixtures.readKey('rsa_ca.crt'),
|
2022-06-27 10:47:13 +02:00
|
|
|
ciphers: 'AES256-GCM-SHA384',
|
|
|
|
maxVersion: 'TLSv1.2',
|
2016-11-24 11:43:35 -08:00
|
|
|
};
|
2013-02-11 18:27:02 -08:00
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const server = tls.createServer(options, onConnection);
|
2020-02-14 14:17:20 +01:00
|
|
|
let conn;
|
2019-02-04 22:06:08 -08:00
|
|
|
server.listen(common.PORT, () => {
|
2017-09-13 22:48:53 -03:00
|
|
|
const opt = { port: common.PORT, rejectUnauthorized: false };
|
2019-02-04 22:06:08 -08:00
|
|
|
conn = tls.connect(opt, () => {
|
2018-02-01 08:10:18 -05:00
|
|
|
setTimeout(done, dur * 1000);
|
2013-02-11 18:27:02 -08:00
|
|
|
bench.start();
|
|
|
|
conn.on('drain', write);
|
|
|
|
write();
|
|
|
|
});
|
|
|
|
|
|
|
|
function write() {
|
|
|
|
while (false !== conn.write(chunk, encoding));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-14 14:17:20 +01:00
|
|
|
let received = 0;
|
2013-02-11 18:27:02 -08:00
|
|
|
function onConnection(conn) {
|
2019-02-04 22:06:08 -08:00
|
|
|
conn.on('data', (chunk) => {
|
2013-02-11 18:27:02 -08:00
|
|
|
received += chunk.length;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function done() {
|
2017-09-13 22:48:53 -03:00
|
|
|
const mbits = (received * 8) / (1024 * 1024);
|
2013-02-11 18:27:02 -08:00
|
|
|
bench.end(mbits);
|
2016-03-02 13:38:23 -08:00
|
|
|
if (conn)
|
|
|
|
conn.destroy();
|
2013-02-11 18:27:02 -08:00
|
|
|
server.close();
|
|
|
|
}
|
|
|
|
}
|