|
| 1 | +// Test the speed of .pipe() with QUIC sockets |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common.js'); |
| 5 | +const quic = require('quic'); |
| 6 | +const fixtures = require('../../test/common/fixtures'); |
| 7 | + |
| 8 | +const key = fixtures.readKey('agent1-key.pem', 'binary'); |
| 9 | +const cert = fixtures.readKey('agent1-cert.pem', 'binary'); |
| 10 | +const ca = fixtures.readKey('ca1-cert.pem', 'binary'); |
| 11 | + |
| 12 | +const bench = common.createBenchmark(main, { |
| 13 | + dur: [5], |
| 14 | +}); |
| 15 | + |
| 16 | +function main({ dur, len, type }) { |
| 17 | + const server = quic.createSocket({ port: 0, validateAddress: true }); |
| 18 | + |
| 19 | + server.listen({ |
| 20 | + key, |
| 21 | + cert, |
| 22 | + ca, |
| 23 | + rejectUnauthorized: false, |
| 24 | + alpn: 'meow' |
| 25 | + }); |
| 26 | + |
| 27 | + server.on('session', (session) => { |
| 28 | + session.on('stream', (stream) => { |
| 29 | + stream.pipe(stream); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + const buffer = Buffer.alloc(102400); |
| 34 | + let received = 0; |
| 35 | + |
| 36 | + server.on('ready', () => { |
| 37 | + const client = quic.createSocket({ |
| 38 | + port: 0, |
| 39 | + client: { |
| 40 | + key, |
| 41 | + cert, |
| 42 | + ca, |
| 43 | + alpn: 'meow' |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + const req = client.connect({ |
| 48 | + address: 'localhost', |
| 49 | + port: server.address.port |
| 50 | + }); |
| 51 | + |
| 52 | + req.on('secure', () => { |
| 53 | + const stream = req.openStream({ halfOpen: false }); |
| 54 | + stream.on('data', (chunk) => received += chunk.length); |
| 55 | + |
| 56 | + function write() { |
| 57 | + stream.write(buffer, write); |
| 58 | + } |
| 59 | + |
| 60 | + bench.start(); |
| 61 | + write(); |
| 62 | + |
| 63 | + setTimeout(() => { |
| 64 | + // Multiply by 2 since we're sending it first one way |
| 65 | + // then then back again. |
| 66 | + const bytes = received * 2; |
| 67 | + const gbits = (bytes * 8) / (1024 * 1024 * 1024); |
| 68 | + bench.end(gbits); |
| 69 | + process.exit(0); |
| 70 | + }, dur * 1000); |
| 71 | + }); |
| 72 | + }); |
| 73 | +} |
0 commit comments