Skip to content

Commit 429e78d

Browse files
committed
test: refactor test-readline-async-iterators into a benchmark
Fixes: #49224 Added old way of readline async iterator to benchmark.
1 parent 64a5c01 commit 429e78d

File tree

2 files changed

+36
-112
lines changed

2 files changed

+36
-112
lines changed

benchmark/readline/readline-iterable.js

+36-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { Readable } = require('stream');
55

66
const bench = common.createBenchmark(main, {
77
n: [1e1, 1e2, 1e3, 1e4, 1e5, 1e6],
8+
type: ['old', 'new'],
89
});
910

1011
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.
2122
Diam in arcu cursus euismod quis viverra nibh.
2223
Rest of line`;
2324

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+
2456
function getLoremIpsumStream(repetitions) {
2557
const readable = Readable({
2658
objectMode: true,
@@ -32,16 +64,18 @@ function getLoremIpsumStream(repetitions) {
3264
return readable;
3365
}
3466

35-
async function main({ n }) {
67+
async function main({ n, type }) {
3668
bench.start();
3769
let lineCount = 0;
3870

3971
const iterable = readline.createInterface({
4072
input: getLoremIpsumStream(n),
4173
});
4274

75+
const readlineIterable = type === 'old' ? oldWay.call(iterable) : iterable;
76+
4377
// eslint-disable-next-line no-unused-vars
44-
for await (const _ of iterable) {
78+
for await (const _ of readlineIterable) {
4579
lineCount++;
4680
}
4781
bench.end(lineCount);

test/parallel/test-readline-async-iterators.js

-110
Original file line numberDiff line numberDiff line change
@@ -73,115 +73,6 @@ async function testMutual() {
7373
}
7474
}
7575

76-
async function testPerformance() {
77-
const loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit,
78-
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
79-
Dui accumsan sit amet nulla facilisi morbi tempus iaculis urna.
80-
Eget dolor morbi non arcu risus quis varius quam quisque.
81-
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-
objectMode: true,
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-
input: getLoremIpsumStream(),
164-
});
165-
let currentTotalTimeNewWay = Date.now();
166-
for await (const line of rlNewWay) {
167-
totalCharsNewWay += line.length;
168-
if (time === 0) {
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-
18576
async function testSlowStreamForLeaks() {
18677
const message = 'a\nb\nc\n';
18778
const DELAY = 1;
@@ -225,6 +116,5 @@ async function testSlowStreamForLeaks() {
225116

226117
testSimple()
227118
.then(testMutual)
228-
.then(testPerformance)
229119
.then(testSlowStreamForLeaks)
230120
.then(common.mustCall());

0 commit comments

Comments
 (0)