-
-
Notifications
You must be signed in to change notification settings - Fork 34.9k
[DRAFT] stream: prototype for new stream implementation #62066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jasnell
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
jasnell:jasnell/new-streams-prototype
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+9,162
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| // Compare FileHandle.createReadStream() vs readableWebStream() vs pull() | ||
| // reading a large file through two transforms: uppercase then gzip compress. | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common.js'); | ||
| const fs = require('fs'); | ||
| const zlib = require('zlib'); | ||
| const { Transform, Writable, pipeline } = require('stream'); | ||
|
|
||
| const tmpdir = require('../../test/common/tmpdir'); | ||
| tmpdir.refresh(); | ||
| const filename = tmpdir.resolve(`.removeme-benchmark-garbage-${process.pid}`); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| api: ['classic', 'webstream', 'pull'], | ||
| filesize: [1024 * 1024, 16 * 1024 * 1024, 64 * 1024 * 1024], | ||
| n: [5], | ||
| }); | ||
|
|
||
| function main({ api, filesize, n }) { | ||
| // Create the fixture file with repeating lowercase ASCII | ||
| const chunk = Buffer.alloc(Math.min(filesize, 64 * 1024), 'abcdefghij'); | ||
| const fd = fs.openSync(filename, 'w'); | ||
| let remaining = filesize; | ||
| while (remaining > 0) { | ||
| const toWrite = Math.min(remaining, chunk.length); | ||
| fs.writeSync(fd, chunk, 0, toWrite); | ||
| remaining -= toWrite; | ||
| } | ||
| fs.closeSync(fd); | ||
|
|
||
| if (api === 'classic') { | ||
| benchClassic(n, filesize).then(() => cleanup()); | ||
| } else if (api === 'webstream') { | ||
| benchWebStream(n, filesize).then(() => cleanup()); | ||
| } else { | ||
| benchPull(n, filesize).then(() => cleanup()); | ||
| } | ||
| } | ||
|
|
||
| function cleanup() { | ||
| try { fs.unlinkSync(filename); } catch { /* ignore */ } | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Classic streams path: createReadStream -> Transform (upper) -> createGzip | ||
| // --------------------------------------------------------------------------- | ||
| async function benchClassic(n, filesize) { | ||
| // Warm up | ||
| await runClassic(); | ||
|
|
||
| bench.start(); | ||
| let totalBytes = 0; | ||
| for (let i = 0; i < n; i++) { | ||
| totalBytes += await runClassic(); | ||
| } | ||
| bench.end(totalBytes / (1024 * 1024)); | ||
| } | ||
|
|
||
| function runClassic() { | ||
| return new Promise((resolve, reject) => { | ||
| const rs = fs.createReadStream(filename); | ||
|
|
||
| // Transform 1: uppercase | ||
| const upper = new Transform({ | ||
| transform(chunk, encoding, callback) { | ||
| const buf = Buffer.allocUnsafe(chunk.length); | ||
| for (let i = 0; i < chunk.length; i++) { | ||
| const b = chunk[i]; | ||
| buf[i] = (b >= 0x61 && b <= 0x7a) ? b - 0x20 : b; | ||
| } | ||
| callback(null, buf); | ||
| }, | ||
| }); | ||
|
|
||
| // Transform 2: gzip | ||
| const gz = zlib.createGzip(); | ||
|
|
||
| // Sink: count compressed bytes | ||
| let totalBytes = 0; | ||
| const sink = new Writable({ | ||
| write(chunk, encoding, callback) { | ||
| totalBytes += chunk.length; | ||
| callback(); | ||
| }, | ||
| }); | ||
|
|
||
| pipeline(rs, upper, gz, sink, (err) => { | ||
| if (err) reject(err); | ||
| else resolve(totalBytes); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // WebStream path: readableWebStream -> TransformStream (upper) -> CompressionStream | ||
| // --------------------------------------------------------------------------- | ||
| async function benchWebStream(n, filesize) { | ||
| // Warm up | ||
| await runWebStream(); | ||
|
|
||
| bench.start(); | ||
| let totalBytes = 0; | ||
| for (let i = 0; i < n; i++) { | ||
| totalBytes += await runWebStream(); | ||
| } | ||
| bench.end(totalBytes / (1024 * 1024)); | ||
| } | ||
|
|
||
| async function runWebStream() { | ||
| const fh = await fs.promises.open(filename, 'r'); | ||
| try { | ||
| const rs = fh.readableWebStream(); | ||
|
|
||
| // Transform 1: uppercase | ||
| const upper = new TransformStream({ | ||
| transform(chunk, controller) { | ||
| const buf = new Uint8Array(chunk.length); | ||
| for (let i = 0; i < chunk.length; i++) { | ||
| const b = chunk[i]; | ||
| // a-z (0x61-0x7a) -> A-Z (0x41-0x5a) | ||
| buf[i] = (b >= 0x61 && b <= 0x7a) ? b - 0x20 : b; | ||
| } | ||
| controller.enqueue(buf); | ||
| }, | ||
| }); | ||
|
|
||
| // Transform 2: gzip via CompressionStream | ||
| const compress = new CompressionStream('gzip'); | ||
|
|
||
| const output = rs.pipeThrough(upper).pipeThrough(compress); | ||
| const reader = output.getReader(); | ||
|
|
||
| let totalBytes = 0; | ||
| while (true) { | ||
| const { done, value } = await reader.read(); | ||
| if (done) break; | ||
| totalBytes += value.byteLength; | ||
| } | ||
| return totalBytes; | ||
| } finally { | ||
| await fh.close(); | ||
| } | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // New streams path: pull() with uppercase transform + gzip transform | ||
| // --------------------------------------------------------------------------- | ||
| async function benchPull(n, filesize) { | ||
| const { pull, compressGzip } = require('stream/new'); | ||
|
|
||
| // Warm up | ||
| await runPull(pull, compressGzip); | ||
|
|
||
| bench.start(); | ||
| let totalBytes = 0; | ||
| for (let i = 0; i < n; i++) { | ||
| totalBytes += await runPull(pull, compressGzip); | ||
| } | ||
| bench.end(totalBytes / (1024 * 1024)); | ||
| } | ||
|
|
||
| async function runPull(pull, compressGzip) { | ||
| const fh = await fs.promises.open(filename, 'r'); | ||
| try { | ||
| // Stateless transform: uppercase each chunk in the batch | ||
| const upper = (chunks) => { | ||
| if (chunks === null) return null; | ||
| const out = new Array(chunks.length); | ||
| for (let j = 0; j < chunks.length; j++) { | ||
| const src = chunks[j]; | ||
| const buf = new Uint8Array(src.length); | ||
| for (let i = 0; i < src.length; i++) { | ||
| const b = src[i]; | ||
| buf[i] = (b >= 0x61 && b <= 0x7a) ? b - 0x20 : b; | ||
| } | ||
| out[j] = buf; | ||
| } | ||
| return out; | ||
| }; | ||
|
|
||
| const readable = fh.pull(upper, compressGzip()); | ||
|
|
||
| // Count bytes symmetrically with the classic path (no final | ||
| // concatenation into a single buffer). | ||
| let totalBytes = 0; | ||
| for await (const chunks of readable) { | ||
| for (let i = 0; i < chunks.length; i++) { | ||
| totalBytes += chunks[i].byteLength; | ||
| } | ||
| } | ||
| return totalBytes; | ||
| } finally { | ||
| await fh.close(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.