|
| 1 | +// Compare FileHandle.createReadStream() vs readableWebStream() vs pull() |
| 2 | +// reading a large file through two transforms: uppercase then gzip compress. |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const common = require('../common.js'); |
| 6 | +const fs = require('fs'); |
| 7 | +const zlib = require('zlib'); |
| 8 | +const { Transform, Writable, pipeline } = require('stream'); |
| 9 | + |
| 10 | +const tmpdir = require('../../test/common/tmpdir'); |
| 11 | +tmpdir.refresh(); |
| 12 | +const filename = tmpdir.resolve(`.removeme-benchmark-garbage-${process.pid}`); |
| 13 | + |
| 14 | +const bench = common.createBenchmark(main, { |
| 15 | + api: ['classic', 'webstream', 'pull'], |
| 16 | + filesize: [1024 * 1024, 16 * 1024 * 1024, 64 * 1024 * 1024], |
| 17 | + n: [5], |
| 18 | +}); |
| 19 | + |
| 20 | +function main({ api, filesize, n }) { |
| 21 | + // Create the fixture file with repeating lowercase ASCII |
| 22 | + const chunk = Buffer.alloc(Math.min(filesize, 64 * 1024), 'abcdefghij'); |
| 23 | + const fd = fs.openSync(filename, 'w'); |
| 24 | + let remaining = filesize; |
| 25 | + while (remaining > 0) { |
| 26 | + const toWrite = Math.min(remaining, chunk.length); |
| 27 | + fs.writeSync(fd, chunk, 0, toWrite); |
| 28 | + remaining -= toWrite; |
| 29 | + } |
| 30 | + fs.closeSync(fd); |
| 31 | + |
| 32 | + if (api === 'classic') { |
| 33 | + benchClassic(n, filesize).then(() => cleanup()); |
| 34 | + } else if (api === 'webstream') { |
| 35 | + benchWebStream(n, filesize).then(() => cleanup()); |
| 36 | + } else { |
| 37 | + benchPull(n, filesize).then(() => cleanup()); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function cleanup() { |
| 42 | + try { fs.unlinkSync(filename); } catch { /* ignore */ } |
| 43 | +} |
| 44 | + |
| 45 | +// --------------------------------------------------------------------------- |
| 46 | +// Classic streams path: createReadStream -> Transform (upper) -> createGzip |
| 47 | +// --------------------------------------------------------------------------- |
| 48 | +async function benchClassic(n, filesize) { |
| 49 | + // Warm up |
| 50 | + await runClassic(); |
| 51 | + |
| 52 | + bench.start(); |
| 53 | + let totalBytes = 0; |
| 54 | + for (let i = 0; i < n; i++) { |
| 55 | + totalBytes += await runClassic(); |
| 56 | + } |
| 57 | + bench.end(totalBytes / (1024 * 1024)); |
| 58 | +} |
| 59 | + |
| 60 | +function runClassic() { |
| 61 | + return new Promise((resolve, reject) => { |
| 62 | + const rs = fs.createReadStream(filename); |
| 63 | + |
| 64 | + // Transform 1: uppercase |
| 65 | + const upper = new Transform({ |
| 66 | + transform(chunk, encoding, callback) { |
| 67 | + const buf = Buffer.allocUnsafe(chunk.length); |
| 68 | + for (let i = 0; i < chunk.length; i++) { |
| 69 | + const b = chunk[i]; |
| 70 | + buf[i] = (b >= 0x61 && b <= 0x7a) ? b - 0x20 : b; |
| 71 | + } |
| 72 | + callback(null, buf); |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + // Transform 2: gzip |
| 77 | + const gz = zlib.createGzip(); |
| 78 | + |
| 79 | + // Sink: count compressed bytes |
| 80 | + let totalBytes = 0; |
| 81 | + const sink = new Writable({ |
| 82 | + write(chunk, encoding, callback) { |
| 83 | + totalBytes += chunk.length; |
| 84 | + callback(); |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + pipeline(rs, upper, gz, sink, (err) => { |
| 89 | + if (err) reject(err); |
| 90 | + else resolve(totalBytes); |
| 91 | + }); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +// --------------------------------------------------------------------------- |
| 96 | +// WebStream path: readableWebStream -> TransformStream (upper) -> CompressionStream |
| 97 | +// --------------------------------------------------------------------------- |
| 98 | +async function benchWebStream(n, filesize) { |
| 99 | + // Warm up |
| 100 | + await runWebStream(); |
| 101 | + |
| 102 | + bench.start(); |
| 103 | + let totalBytes = 0; |
| 104 | + for (let i = 0; i < n; i++) { |
| 105 | + totalBytes += await runWebStream(); |
| 106 | + } |
| 107 | + bench.end(totalBytes / (1024 * 1024)); |
| 108 | +} |
| 109 | + |
| 110 | +async function runWebStream() { |
| 111 | + const fh = await fs.promises.open(filename, 'r'); |
| 112 | + try { |
| 113 | + const rs = fh.readableWebStream(); |
| 114 | + |
| 115 | + // Transform 1: uppercase |
| 116 | + const upper = new TransformStream({ |
| 117 | + transform(chunk, controller) { |
| 118 | + const buf = new Uint8Array(chunk.length); |
| 119 | + for (let i = 0; i < chunk.length; i++) { |
| 120 | + const b = chunk[i]; |
| 121 | + // a-z (0x61-0x7a) -> A-Z (0x41-0x5a) |
| 122 | + buf[i] = (b >= 0x61 && b <= 0x7a) ? b - 0x20 : b; |
| 123 | + } |
| 124 | + controller.enqueue(buf); |
| 125 | + }, |
| 126 | + }); |
| 127 | + |
| 128 | + // Transform 2: gzip via CompressionStream |
| 129 | + const compress = new CompressionStream('gzip'); |
| 130 | + |
| 131 | + const output = rs.pipeThrough(upper).pipeThrough(compress); |
| 132 | + const reader = output.getReader(); |
| 133 | + |
| 134 | + let totalBytes = 0; |
| 135 | + while (true) { |
| 136 | + const { done, value } = await reader.read(); |
| 137 | + if (done) break; |
| 138 | + totalBytes += value.byteLength; |
| 139 | + } |
| 140 | + return totalBytes; |
| 141 | + } finally { |
| 142 | + await fh.close(); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// --------------------------------------------------------------------------- |
| 147 | +// New streams path: pull() with uppercase transform + gzip transform |
| 148 | +// --------------------------------------------------------------------------- |
| 149 | +async function benchPull(n, filesize) { |
| 150 | + const { pull, compressGzip } = require('stream/new'); |
| 151 | + |
| 152 | + // Warm up |
| 153 | + await runPull(pull, compressGzip); |
| 154 | + |
| 155 | + bench.start(); |
| 156 | + let totalBytes = 0; |
| 157 | + for (let i = 0; i < n; i++) { |
| 158 | + totalBytes += await runPull(pull, compressGzip); |
| 159 | + } |
| 160 | + bench.end(totalBytes / (1024 * 1024)); |
| 161 | +} |
| 162 | + |
| 163 | +async function runPull(pull, compressGzip) { |
| 164 | + const fh = await fs.promises.open(filename, 'r'); |
| 165 | + try { |
| 166 | + // Stateless transform: uppercase each chunk in the batch |
| 167 | + const upper = (chunks) => { |
| 168 | + if (chunks === null) return null; |
| 169 | + const out = new Array(chunks.length); |
| 170 | + for (let j = 0; j < chunks.length; j++) { |
| 171 | + const src = chunks[j]; |
| 172 | + const buf = new Uint8Array(src.length); |
| 173 | + for (let i = 0; i < src.length; i++) { |
| 174 | + const b = src[i]; |
| 175 | + buf[i] = (b >= 0x61 && b <= 0x7a) ? b - 0x20 : b; |
| 176 | + } |
| 177 | + out[j] = buf; |
| 178 | + } |
| 179 | + return out; |
| 180 | + }; |
| 181 | + |
| 182 | + const readable = fh.pull(upper, compressGzip()); |
| 183 | + |
| 184 | + // Count bytes symmetrically with the classic path (no final |
| 185 | + // concatenation into a single buffer). |
| 186 | + let totalBytes = 0; |
| 187 | + for await (const chunks of readable) { |
| 188 | + for (let i = 0; i < chunks.length; i++) { |
| 189 | + totalBytes += chunks[i].byteLength; |
| 190 | + } |
| 191 | + } |
| 192 | + return totalBytes; |
| 193 | + } finally { |
| 194 | + await fh.close(); |
| 195 | + } |
| 196 | +} |
0 commit comments