| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a08cfcf commit f954ab3
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,16 +9,35 @@ const { | |||
| 9 | 9 | } = require('node:stream'); | |
| 10 | 10 | ||
| 11 | 11 | const bench = common.createBenchmark(main, { | |
| 12 | - n: [1e3], | ||
| 12 | + type: ['creation', 'throughput'], | ||
| 13 | + n: [1, 1e3], | ||
| 14 | + streams: [100], | ||
| 15 | + chunks: [1e4], | ||
| 16 | + }, { | ||
| 17 | + combinationFilter({ type, n }) { | ||
| 18 | + return type === 'creation' ? n === 1e3 : n === 1; | ||
| 19 | + }, | ||
| 20 | + test: { | ||
| 21 | + n: [1, 1e3], | ||
| 22 | + type: ['creation', 'throughput'], | ||
| 23 | + }, | ||
| 13 | 24 | }); | |
| 14 | 25 | ||
| 15 | - function main({ n }) { | ||
| 26 | + function main({ type, n, streams, chunks }) { | ||
| 27 | + switch (type) { | ||
| 28 | + case 'creation': | ||
| 29 | + return benchCreation(n, streams); | ||
| 30 | + case 'throughput': | ||
| 31 | + return benchThroughput(n, streams, chunks); | ||
| 32 | + } | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + function benchCreation(n, numberOfPassThroughs) { | ||
| 16 | 36 | const cachedPassThroughs = []; | |
| 17 | 37 | const cachedReadables = []; | |
| 18 | 38 | const cachedWritables = []; | |
| 19 | 39 | ||
| 20 | 40 | for (let i = 0; i < n; i++) { | |
| 21 | - const numberOfPassThroughs = 100; | ||
| 22 | 41 | const passThroughs = []; | |
| 23 | 42 | ||
| 24 | 43 | for (let i = 0; i < numberOfPassThroughs; i++) { | |
@@ -40,3 +59,41 @@ function main({ n }) { | |||
| 40 | 59 | } | |
| 41 | 60 | bench.end(n); | |
| 42 | 61 | } | |
| 62 | + | ||
| 63 | + function benchThroughput(n, numberOfPassThroughs, chunks) { | ||
| 64 | + const chunk = Buffer.alloc(1024); | ||
| 65 | + | ||
| 66 | + let i = 0; | ||
| 67 | + bench.start(); | ||
| 68 | + | ||
| 69 | + function run() { | ||
| 70 | + if (i++ === n) { | ||
| 71 | + bench.end(n * chunks); | ||
| 72 | + return; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + const passThroughs = []; | ||
| 76 | + for (let i = 0; i < numberOfPassThroughs; i++) { | ||
| 77 | + passThroughs.push(new PassThrough()); | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + let remaining = chunks; | ||
| 81 | + const composed = compose(...passThroughs); | ||
| 82 | + composed.on('data', () => {}); | ||
| 83 | + composed.on('end', run); | ||
| 84 | + | ||
| 85 | + write(); | ||
| 86 | + | ||
| 87 | + function write() { | ||
| 88 | + while (remaining-- > 0) { | ||
| 89 | + if (!composed.write(chunk)) { | ||
| 90 | + composed.once('drain', write); | ||
| 91 | + return; | ||
| 92 | + } | ||
| 93 | + } | ||
| 94 | + composed.end(); | ||
| 95 | + } | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + run(); | ||
| 99 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,7 +82,6 @@ module.exports = function compose(...streams) { | |||
| 82 | 82 | ||
| 83 | 83 | let ondrain; | |
| 84 | 84 | let onfinish; | |
| 85 | - let onreadable; | ||
| 86 | 85 | let onclose; | |
| 87 | 86 | let d; | |
| 88 | 87 | ||
@@ -184,31 +183,19 @@ module.exports = function compose(...streams) { | |||
| 184 | 183 | ||
| 185 | 184 | if (readable) { | |
| 186 | 185 | if (isNodeStream(tail)) { | |
| 187 | - tail.on('readable', function() { | ||
| 188 | - if (onreadable) { | ||
| 189 | - const cb = onreadable; | ||
| 190 | - onreadable = null; | ||
| 191 | - cb(); | ||
| 186 | + d._read = function() { | ||
| 187 | + tail.resume(); | ||
| 188 | + }; | ||
| 189 | + | ||
| 190 | + tail.on('data', function(chunk) { | ||
| 191 | + if (!d.push(chunk)) { | ||
| 192 | + tail.pause(); | ||
| 192 | 193 | } | |
| 193 | 194 | }); | |
| 194 | 195 | ||
| 195 | 196 | tail.on('end', function() { | |
| 196 | 197 | d.push(null); | |
| 197 | 198 | }); | |
| 198 | - | ||
| 199 | - d._read = function() { | ||
| 200 | - while (true) { | ||
| 201 | - const buf = tail.read(); | ||
| 202 | - if (buf === null) { | ||
| 203 | - onreadable = d._read; | ||
| 204 | - return; | ||
| 205 | - } | ||
| 206 | - | ||
| 207 | - if (!d.push(buf)) { | ||
| 208 | - return; | ||
| 209 | - } | ||
| 210 | - } | ||
| 211 | - }; | ||
| 212 | 199 | } else if (isWebStream(tail)) { | |
| 213 | 200 | const readable = isTransformStream(tail) ? tail.readable : tail; | |
| 214 | 201 | const reader = readable.getReader(); | |
@@ -238,7 +225,6 @@ module.exports = function compose(...streams) { | |||
| 238 | 225 | err = new AbortError(); | |
| 239 | 226 | } | |
| 240 | 227 | ||
| 241 | - onreadable = null; | ||
| 242 | 228 | ondrain = null; | |
| 243 | 229 | onfinish = null; | |
| 244 | 230 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments