1'use strict'; 2const common = require('../common.js'); 3const bench = common.createBenchmark(main, { 4 dur: [5], 5 type: ['buf', 'asc', 'utf'], 6 sendchunklen: [256, 32 * 1024, 128 * 1024, 16 * 1024 * 1024], 7 recvbuflen: [0, 64 * 1024, 1024 * 1024], 8 recvbufgenfn: ['true', 'false'], 9}); 10 11const fixtures = require('../../test/common/fixtures'); 12let options; 13let recvbuf; 14let received = 0; 15const tls = require('tls'); 16 17function main({ dur, type, sendchunklen, recvbuflen, recvbufgenfn }) { 18 if (isFinite(recvbuflen) && recvbuflen > 0) 19 recvbuf = Buffer.alloc(recvbuflen); 20 21 let encoding; 22 let chunk; 23 switch (type) { 24 case 'buf': 25 chunk = Buffer.alloc(sendchunklen, 'b'); 26 break; 27 case 'asc': 28 chunk = 'a'.repeat(sendchunklen); 29 encoding = 'ascii'; 30 break; 31 case 'utf': 32 chunk = 'ü'.repeat(sendchunklen / 2); 33 encoding = 'utf8'; 34 break; 35 default: 36 throw new Error('invalid type'); 37 } 38 39 options = { 40 key: fixtures.readKey('rsa_private.pem'), 41 cert: fixtures.readKey('rsa_cert.crt'), 42 ca: fixtures.readKey('rsa_ca.crt'), 43 ciphers: 'AES256-GCM-SHA384', 44 maxVersion: 'TLSv1.2', 45 }; 46 47 let socketOpts; 48 if (recvbuf === undefined) { 49 socketOpts = { port: common.PORT, rejectUnauthorized: false }; 50 } else { 51 let buffer = recvbuf; 52 if (recvbufgenfn === 'true') { 53 let bufidx = -1; 54 const bufpool = [ 55 recvbuf, 56 Buffer.from(recvbuf), 57 Buffer.from(recvbuf), 58 ]; 59 buffer = () => { 60 bufidx = (bufidx + 1) % bufpool.length; 61 return bufpool[bufidx]; 62 }; 63 } 64 socketOpts = { 65 port: common.PORT, 66 rejectUnauthorized: false, 67 onread: { 68 buffer, 69 callback: function(nread, buf) { 70 received += nread; 71 }, 72 }, 73 }; 74 } 75 76 const server = tls.createServer(options, (socket) => { 77 socket.on('data', (buf) => { 78 socket.on('drain', write); 79 write(); 80 }); 81 82 function write() { 83 while (false !== socket.write(chunk, encoding)); 84 } 85 }); 86 87 let conn; 88 server.listen(common.PORT, () => { 89 conn = tls.connect(socketOpts, () => { 90 setTimeout(done, dur * 1000); 91 bench.start(); 92 conn.write('hello'); 93 }); 94 95 conn.on('data', (chunk) => { 96 received += chunk.length; 97 }); 98 }); 99 100 function done() { 101 const mbits = (received * 8) / (1024 * 1024); 102 bench.end(mbits); 103 process.exit(0); 104 } 105} 106