8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b8a4fef commit 3db9b40Copy full SHA for 3db9b40
benchmark/readline/readline-iterable.js
@@ -5,6 +5,7 @@ const { Readable } = require('stream');
5
6
const bench = common.createBenchmark(main, {
7
n: [1e1, 1e2, 1e3, 1e4, 1e5, 1e6],
8
+ type: ['old', 'new'],
9
});
10
11
const loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
@@ -21,6 +22,37 @@ Condimentum mattis pellentesque id nibh tortor id aliquet lectus proin.
21
22
Diam in arcu cursus euismod quis viverra nibh.
23
Rest of line`;
24
25
+function oldWay() {
26
+ const readable = new Readable({
27
+ objectMode: true,
28
+ read: () => {
29
+ this.resume();
30
+ },
31
+ destroy: (err, cb) => {
32
+ this.off('line', lineListener);
33
+ this.off('close', closeListener);
34
+ this.close();
35
+ cb(err);
36
37
+ });
38
+ const lineListener = (input) => {
39
+ if (!readable.push(input)) {
40
+ // TODO(rexagod): drain to resume flow
41
+ this.pause();
42
+ }
43
+ };
44
+ const closeListener = () => {
45
+ readable.push(null);
46
47
+ const errorListener = (err) => {
48
+ readable.destroy(err);
49
50
+ this.on('error', errorListener);
51
+ this.on('line', lineListener);
52
+ this.on('close', closeListener);
53
+ return readable[Symbol.asyncIterator]();
54
+}
55
+
56
function getLoremIpsumStream(repetitions) {
57
const readable = Readable({
58
objectMode: true,
@@ -32,16 +64,18 @@ function getLoremIpsumStream(repetitions) {
64
return readable;
65
}
66
-async function main({ n }) {
67
+async function main({ n, type }) {
68
bench.start();
69
let lineCount = 0;
70
71
const iterable = readline.createInterface({
72
input: getLoremIpsumStream(n),
73
74
75
+ const readlineIterable = type === 'old' ? oldWay.call(iterable) : iterable;
76
77
// eslint-disable-next-line no-unused-vars
- for await (const _ of iterable) {
78
+ for await (const _ of readlineIterable) {
79
lineCount++;
80
81
bench.end(lineCount);
test/parallel/test-readline-async-iterators.js
@@ -73,115 +73,6 @@ async function testMutual() {
-async function testPerformance() {
- const loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit,
-sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
-Dui accumsan sit amet nulla facilisi morbi tempus iaculis urna.
-Eget dolor morbi non arcu risus quis varius quam quisque.
-Lacus viverra vitae congue eu consequat ac felis donec.
82
-Amet porttitor eget dolor morbi non arcu.
83
-Velit ut tortor pretium viverra suspendisse.
84
-Mauris nunc congue nisi vitae suscipit tellus.
85
-Amet nisl suscipit adipiscing bibendum est ultricies integer.
86
-Sit amet dictum sit amet justo donec enim diam.
87
-Condimentum mattis pellentesque id nibh tortor id aliquet lectus proin.
88
-Diam in arcu cursus euismod quis viverra nibh.
89
-`;
90
-
91
- const REPETITIONS = 10000;
92
- const SAMPLE = 100;
93
- const THRESHOLD = 81;
94
95
- function getLoremIpsumStream() {
96
- const readable = Readable({
97
- objectMode: true,
98
- });
99
- let i = 0;
100
- readable._read = () => readable.push(
101
- i++ >= REPETITIONS ? null : loremIpsum
102
- );
103
- return readable;
104
- }
105
106
- function oldWay() {
107
- const readable = new Readable({
108
109
- read: () => {
110
- this.resume();
111
- },
112
- destroy: (err, cb) => {
113
- this.off('line', lineListener);
114
- this.off('close', closeListener);
115
- this.close();
116
- cb(err);
117
118
119
- const lineListener = (input) => {
120
- if (!readable.push(input)) {
121
- // TODO(rexagod): drain to resume flow
122
- this.pause();
123
124
- };
125
- const closeListener = () => {
126
- readable.push(null);
127
128
- const errorListener = (err) => {
129
- readable.destroy(err);
130
131
- this.on('error', errorListener);
132
- this.on('line', lineListener);
133
- this.on('close', closeListener);
134
- return readable[Symbol.asyncIterator]();
135
136
137
- function getAvg(mean, x, n) {
138
- return (mean * n + x) / (n + 1);
139
140
141
- let totalTimeOldWay = 0;
142
- let totalTimeNewWay = 0;
143
- let totalCharsOldWay = 0;
144
- let totalCharsNewWay = 0;
145
- const linesOldWay = [];
146
- const linesNewWay = [];
147
148
- for (let time = 0; time < SAMPLE; time++) {
149
- const rlOldWay = readline.createInterface({
150
- input: getLoremIpsumStream(),
151
152
- let currenttotalTimeOldWay = Date.now();
153
- for await (const line of oldWay.call(rlOldWay)) {
154
- totalCharsOldWay += line.length;
155
- if (time === 0) {
156
- linesOldWay.push(line);
157
158
159
- currenttotalTimeOldWay = Date.now() - currenttotalTimeOldWay;
160
- totalTimeOldWay = getAvg(totalTimeOldWay, currenttotalTimeOldWay, SAMPLE);
161
162
- const rlNewWay = readline.createInterface({
163
164
165
- let currentTotalTimeNewWay = Date.now();
166
- for await (const line of rlNewWay) {
167
- totalCharsNewWay += line.length;
168
169
- linesNewWay.push(line);
170
171
172
- currentTotalTimeNewWay = Date.now() - currentTotalTimeNewWay;
173
- totalTimeNewWay = getAvg(totalTimeNewWay, currentTotalTimeNewWay, SAMPLE);
174
175
176
- assert.strictEqual(totalCharsOldWay, totalCharsNewWay);
177
- assert.strictEqual(linesOldWay.length, linesNewWay.length);
178
- linesOldWay.forEach((line, index) =>
179
- assert.strictEqual(line, linesNewWay[index])
180
181
- const percentage = totalTimeNewWay * 100 / totalTimeOldWay;
182
- assert.ok(percentage <= THRESHOLD, `Failed: ${totalTimeNewWay} isn't lesser than ${THRESHOLD}% of ${totalTimeOldWay}. Actual percentage: ${percentage.toFixed(2)}%`);
183
-}
184
185
async function testSlowStreamForLeaks() {
186
const message = 'a\nb\nc\n';
187
const DELAY = 1;
@@ -225,6 +116,5 @@ async function testSlowStreamForLeaks() {
225
226
testSimple()
227
.then(testMutual)
228
- .then(testPerformance)
229
.then(testSlowStreamForLeaks)
230
.then(common.mustCall());