|
| 1 | +// Throughput benchmark: fan-out data to N consumers simultaneously. |
| 2 | +// Classic streams use PassThrough + pipe, Web Streams use tee() chains, |
| 3 | +// stream/iter uses broadcast() with push() consumers. |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +const common = require('../../common.js'); |
| 7 | +const { PassThrough, Writable } = require('stream'); |
| 8 | + |
| 9 | +const bench = common.createBenchmark(main, { |
| 10 | + api: ['classic', 'webstream', 'iter'], |
| 11 | + consumers: [1, 2, 4], |
| 12 | + datasize: [1024 * 1024, 16 * 1024 * 1024], |
| 13 | + n: [5], |
| 14 | +}, { |
| 15 | + flags: ['--experimental-stream-iter'], |
| 16 | +}); |
| 17 | + |
| 18 | +const CHUNK_SIZE = 64 * 1024; |
| 19 | + |
| 20 | +function main({ api, consumers, datasize, n }) { |
| 21 | + const chunk = Buffer.alloc(CHUNK_SIZE, 'abcdefghij'); |
| 22 | + const totalOps = (datasize * n) / (1024 * 1024); |
| 23 | + |
| 24 | + switch (api) { |
| 25 | + case 'classic': |
| 26 | + return benchClassic(chunk, consumers, datasize, n, totalOps); |
| 27 | + case 'webstream': |
| 28 | + return benchWebStream(chunk, consumers, datasize, n, totalOps); |
| 29 | + case 'iter': |
| 30 | + return benchIter(chunk, consumers, datasize, n, totalOps); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function benchClassic(chunk, numConsumers, datasize, n, totalOps) { |
| 35 | + function run(cb) { |
| 36 | + const source = new PassThrough(); |
| 37 | + const sinks = []; |
| 38 | + let finished = 0; |
| 39 | + |
| 40 | + for (let c = 0; c < numConsumers; c++) { |
| 41 | + const w = new Writable({ write(data, enc, cb) { cb(); } }); |
| 42 | + source.pipe(w); |
| 43 | + w.on('finish', () => { if (++finished === numConsumers) cb(); }); |
| 44 | + sinks.push(w); |
| 45 | + } |
| 46 | + |
| 47 | + let remaining = datasize; |
| 48 | + function write() { |
| 49 | + let ok = true; |
| 50 | + while (ok && remaining > 0) { |
| 51 | + const size = Math.min(remaining, chunk.length); |
| 52 | + remaining -= size; |
| 53 | + const buf = size === chunk.length ? chunk : chunk.subarray(0, size); |
| 54 | + ok = source.write(buf); |
| 55 | + } |
| 56 | + if (remaining > 0) { |
| 57 | + source.once('drain', write); |
| 58 | + } else { |
| 59 | + source.end(); |
| 60 | + } |
| 61 | + } |
| 62 | + write(); |
| 63 | + } |
| 64 | + |
| 65 | + let i = 0; |
| 66 | + bench.start(); |
| 67 | + (function next() { |
| 68 | + if (i++ >= n) return bench.end(totalOps); |
| 69 | + run(next); |
| 70 | + })(); |
| 71 | +} |
| 72 | + |
| 73 | +function benchWebStream(chunk, numConsumers, datasize, n, totalOps) { |
| 74 | + async function run() { |
| 75 | + let remaining = datasize; |
| 76 | + const rs = new ReadableStream({ |
| 77 | + pull(controller) { |
| 78 | + if (remaining <= 0) { controller.close(); return; } |
| 79 | + const size = Math.min(remaining, chunk.length); |
| 80 | + remaining -= size; |
| 81 | + controller.enqueue( |
| 82 | + size === chunk.length ? chunk : chunk.subarray(0, size)); |
| 83 | + }, |
| 84 | + }); |
| 85 | + |
| 86 | + // Chain tee() calls to get numConsumers branches. |
| 87 | + // tee() gives 2; for 4 we tee twice at each level. |
| 88 | + const branches = []; |
| 89 | + if (numConsumers === 1) { |
| 90 | + branches.push(rs); |
| 91 | + } else { |
| 92 | + const pending = [rs]; |
| 93 | + while (branches.length + pending.length < numConsumers) { |
| 94 | + const stream = pending.shift(); |
| 95 | + const [a, b] = stream.tee(); |
| 96 | + pending.push(a, b); |
| 97 | + } |
| 98 | + branches.push(...pending); |
| 99 | + } |
| 100 | + |
| 101 | + const ws = () => new WritableStream({ write() {} }); |
| 102 | + await Promise.all(branches.map((b) => b.pipeTo(ws()))); |
| 103 | + } |
| 104 | + |
| 105 | + (async () => { |
| 106 | + bench.start(); |
| 107 | + for (let i = 0; i < n; i++) await run(); |
| 108 | + bench.end(totalOps); |
| 109 | + })(); |
| 110 | +} |
| 111 | + |
| 112 | +function benchIter(chunk, numConsumers, datasize, n, totalOps) { |
| 113 | + const { broadcast } = require('stream/iter'); |
| 114 | + |
| 115 | + // No-op consumer: drain all batches without collecting |
| 116 | + async function drain(source) { |
| 117 | + // eslint-disable-next-line no-unused-vars |
| 118 | + for await (const _ of source) { /* drain */ } |
| 119 | + } |
| 120 | + |
| 121 | + async function run() { |
| 122 | + const { writer, broadcast: bc } = broadcast(); |
| 123 | + const consumers = []; |
| 124 | + for (let c = 0; c < numConsumers; c++) { |
| 125 | + consumers.push(drain(bc.push())); |
| 126 | + } |
| 127 | + |
| 128 | + let remaining = datasize; |
| 129 | + while (remaining > 0) { |
| 130 | + const size = Math.min(remaining, chunk.length); |
| 131 | + remaining -= size; |
| 132 | + const buf = size === chunk.length ? chunk : chunk.subarray(0, size); |
| 133 | + writer.writeSync(buf); |
| 134 | + } |
| 135 | + writer.endSync(); |
| 136 | + |
| 137 | + await Promise.all(consumers); |
| 138 | + } |
| 139 | + |
| 140 | + (async () => { |
| 141 | + bench.start(); |
| 142 | + for (let i = 0; i < n; i++) await run(); |
| 143 | + bench.end(totalOps); |
| 144 | + })(); |
| 145 | +} |
0 commit comments