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 }; 45 46 let socketOpts; 47 if (recvbuf === undefined) { 48 socketOpts = { port: common.PORT, rejectUnauthorized: false }; 49 } else { 50 let buffer = recvbuf; 51 if (recvbufgenfn === 'true') { 52 let bufidx = -1; 53 const bufpool = [ 54 recvbuf, 55 Buffer.from(recvbuf), 56 Buffer.from(recvbuf), 57 ]; 58 buffer = () => { 59 bufidx = (bufidx + 1) % bufpool.length; 60 return bufpool[bufidx]; 61 }; 62 } 63 socketOpts = { 64 port: common.PORT, 65 rejectUnauthorized: false, 66 onread: { 67 buffer, 68 callback: function(nread, buf) { 69 received += nread; 70 } 71 } 72 }; 73 } 74 75 const server = tls.createServer(options, (socket) => { 76 socket.on('data', (buf) => { 77 socket.on('drain', write); 78 write(); 79 }); 80 81 function write() { 82 while (false !== socket.write(chunk, encoding)); 83 } 84 }); 85 86 let conn; 87 server.listen(common.PORT, () => { 88 conn = tls.connect(socketOpts, () => { 89 setTimeout(done, dur * 1000); 90 bench.start(); 91 conn.write('hello'); 92 }); 93 94 conn.on('data', (chunk) => { 95 received += chunk.length; 96 }); 97 }); 98 99 function done() { 100 const mbits = (received * 8) / (1024 * 1024); 101 bench.end(mbits); 102 process.exit(0); 103 } 104} 105