0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 15:30:56 +01:00
nodejs/benchmark/tls/throughput.js
Rusty Conover 8b1efe0306 tls: reduce memory copying and number of BIO buffer allocations
Avoid copying buffers before passing to SSL_write if there
are zero length buffers involved.  Only copy the data when
the buffer has a non zero length.

Send a memory allocation hint to the crypto BIO about how much
memory will likely be needed to be allocated by the next call
to SSL_write.  This makes a single allocation rather than the BIO
allocating a buffer for each 16k TLS segment written.  This
solves a problem with large buffers written over TLS triggering
V8's GC.

PR-URL: https://github.com/nodejs/node/pull/31499
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2020-02-28 22:02:21 -08:00

70 lines
1.5 KiB
JavaScript

'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
dur: [5],
type: ['buf', 'asc', 'utf'],
size: [2, 1024, 1024 * 1024, 4 * 1024 * 1024, 16 * 1024 * 1024]
});
const fixtures = require('../../test/common/fixtures');
var options;
const tls = require('tls');
function main({ dur, type, size }) {
var encoding;
var chunk;
switch (type) {
case 'buf':
chunk = Buffer.alloc(size, 'b');
break;
case 'asc':
chunk = 'a'.repeat(size);
encoding = 'ascii';
break;
case 'utf':
chunk = 'ü'.repeat(size / 2);
encoding = 'utf8';
break;
default:
throw new Error('invalid type');
}
options = {
key: fixtures.readKey('rsa_private.pem'),
cert: fixtures.readKey('rsa_cert.crt'),
ca: fixtures.readKey('rsa_ca.crt'),
ciphers: 'AES256-GCM-SHA384'
};
const server = tls.createServer(options, onConnection);
var conn;
server.listen(common.PORT, () => {
const opt = { port: common.PORT, rejectUnauthorized: false };
conn = tls.connect(opt, () => {
setTimeout(done, dur * 1000);
bench.start();
conn.on('drain', write);
write();
});
function write() {
while (false !== conn.write(chunk, encoding));
}
});
var received = 0;
function onConnection(conn) {
conn.on('data', (chunk) => {
received += chunk.length;
});
}
function done() {
const mbits = (received * 8) / (1024 * 1024);
bench.end(mbits);
if (conn)
conn.destroy();
server.close();
}
}