forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose.js
More file actions
99 lines (82 loc) · 2.09 KB
/
Copy pathcompose.js
File metadata and controls
99 lines (82 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const common = require('../common.js');
const {
PassThrough,
Readable,
Writable,
compose,
} = require('node:stream');
const bench = common.createBenchmark(main, {
type: ['creation', 'throughput'],
n: [1, 1e3],
streams: [100],
chunks: [1e4],
}, {
combinationFilter({ type, n }) {
return type === 'creation' ? n === 1e3 : n === 1;
},
test: {
n: [1, 1e3],
type: ['creation', 'throughput'],
},
});
function main({ type, n, streams, chunks }) {
switch (type) {
case 'creation':
return benchCreation(n, streams);
case 'throughput':
return benchThroughput(n, streams, chunks);
}
}
function benchCreation(n, numberOfPassThroughs) {
const cachedPassThroughs = [];
const cachedReadables = [];
const cachedWritables = [];
for (let i = 0; i < n; i++) {
const passThroughs = [];
for (let i = 0; i < numberOfPassThroughs; i++) {
passThroughs.push(new PassThrough());
}
const readable = Readable.from(['hello', 'world']);
const writable = new Writable({ objectMode: true, write: (chunk, encoding, cb) => cb() });
cachedPassThroughs.push(passThroughs);
cachedReadables.push(readable);
cachedWritables.push(writable);
}
bench.start();
for (let i = 0; i < n; i++) {
const composed = compose(cachedReadables[i], ...cachedPassThroughs[i], cachedWritables[i]);
composed.end();
}
bench.end(n);
}
function benchThroughput(n, numberOfPassThroughs, chunks) {
const chunk = Buffer.alloc(1024);
let i = 0;
bench.start();
function run() {
if (i++ === n) {
bench.end(n * chunks);
return;
}
const passThroughs = [];
for (let i = 0; i < numberOfPassThroughs; i++) {
passThroughs.push(new PassThrough());
}
let remaining = chunks;
const composed = compose(...passThroughs);
composed.on('data', () => {});
composed.on('end', run);
write();
function write() {
while (remaining-- > 0) {
if (!composed.write(chunk)) {
composed.once('drain', write);
return;
}
}
composed.end();
}
}
run();
}