|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common.js'); |
| 4 | +const net = require('node:net'); |
| 5 | +const { Duplex } = require('node:stream'); |
| 6 | + |
| 7 | +const WINDOW_SIZE = 32 * 1024 * 1024; |
| 8 | + |
| 9 | +const bench = common.createBenchmark(main, { |
| 10 | + n: [500], |
| 11 | + parallel: [25], |
| 12 | + size: [384 * 1024], |
| 13 | + delay: [0, 5], |
| 14 | +}, { flags: ['--no-warnings'] }); |
| 15 | + |
| 16 | +class DelayedWriteSocket extends Duplex { |
| 17 | + constructor(port, delay, host = '127.0.0.1') { |
| 18 | + super(); |
| 19 | + this.delay = delay; |
| 20 | + this.inner = net.connect(port, host); |
| 21 | + this.inner.on('data', (chunk) => { |
| 22 | + if (!this.push(chunk)) this.inner.pause(); |
| 23 | + }); |
| 24 | + this.inner.on('end', () => this.push(null)); |
| 25 | + this.inner.on('error', (err) => this.destroy(err)); |
| 26 | + this.inner.on('close', () => this.destroy()); |
| 27 | + } |
| 28 | + |
| 29 | + _read() { |
| 30 | + this.inner.resume(); |
| 31 | + } |
| 32 | + |
| 33 | + _write(chunk, encoding, callback) { |
| 34 | + if (!this.inner.write(chunk, encoding)) { |
| 35 | + this.inner.once('drain', () => setTimeout(callback, this.delay)); |
| 36 | + return; |
| 37 | + } |
| 38 | + setTimeout(callback, this.delay); |
| 39 | + } |
| 40 | + |
| 41 | + _final(callback) { |
| 42 | + this.inner.end(); |
| 43 | + setTimeout(callback, this.delay); |
| 44 | + } |
| 45 | + |
| 46 | + _destroy(err, callback) { |
| 47 | + this.inner.destroy(); |
| 48 | + callback(err); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function once(emitter, event) { |
| 53 | + return new Promise((resolve, reject) => { |
| 54 | + emitter.once(event, resolve); |
| 55 | + emitter.once('error', reject); |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +function fetchHttp2(client) { |
| 60 | + return new Promise((resolve, reject) => { |
| 61 | + const req = client.request({ ':path': '/' }); |
| 62 | + let total = 0; |
| 63 | + req.on('data', (chunk) => { |
| 64 | + total += chunk.length; |
| 65 | + }); |
| 66 | + req.on('end', () => resolve(total)); |
| 67 | + req.on('error', reject); |
| 68 | + req.end(); |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +async function main({ n, parallel, size, delay }) { |
| 73 | + const http2 = require('node:http2'); |
| 74 | + const payload = Buffer.alloc(size, 'x'); |
| 75 | + const server = http2.createServer(); |
| 76 | + |
| 77 | + server.on('stream', (stream) => { |
| 78 | + stream.respond({ |
| 79 | + ':status': 200, |
| 80 | + 'content-length': payload.length, |
| 81 | + 'content-type': 'application/octet-stream', |
| 82 | + }); |
| 83 | + stream.end(payload); |
| 84 | + }); |
| 85 | + |
| 86 | + server.listen(0, '127.0.0.1'); |
| 87 | + await once(server, 'listening'); |
| 88 | + |
| 89 | + const port = server.address().port; |
| 90 | + const client = http2.connect(`http://127.0.0.1:${port}`, { |
| 91 | + settings: { initialWindowSize: WINDOW_SIZE }, |
| 92 | + createConnection: delay === 0 ? undefined : () => { |
| 93 | + return new DelayedWriteSocket(port, delay); |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + try { |
| 98 | + await once(client, 'connect'); |
| 99 | + client.setLocalWindowSize(WINDOW_SIZE); |
| 100 | + |
| 101 | + // Warm up the session so connection establishment does not dominate. |
| 102 | + await fetchHttp2(client); |
| 103 | + |
| 104 | + bench.start(); |
| 105 | + for (let completed = 0; completed < n; completed += parallel) { |
| 106 | + const batch = Math.min(parallel, n - completed); |
| 107 | + await Promise.all( |
| 108 | + Array.from({ length: batch }, () => fetchHttp2(client)), |
| 109 | + ); |
| 110 | + } |
| 111 | + bench.end(n); |
| 112 | + } finally { |
| 113 | + client.close(); |
| 114 | + server.close(); |
| 115 | + } |
| 116 | +} |
0 commit comments