Skip to content

Commit 79e0bf9

Browse files
RafaelGSSjuanarbol
authored andcommitted
benchmark: include webstreams benchmark
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #45876 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
1 parent 546e083 commit 79e0bf9

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

benchmark/webstreams/creation.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const {
4+
ReadableStream,
5+
TransformStream,
6+
WritableStream,
7+
} = require('node:stream/web');
8+
const assert = require('assert');
9+
10+
const bench = common.createBenchmark(main, {
11+
n: [50e3],
12+
kind: ['ReadableStream', 'TransformStream', 'WritableStream']
13+
});
14+
15+
let rs, ws, ts;
16+
17+
function main({ n, kind }) {
18+
switch (kind) {
19+
case 'ReadableStream':
20+
bench.start();
21+
for (let i = 0; i < n; ++i)
22+
rs = new ReadableStream();
23+
bench.end(n);
24+
25+
// Avoid V8 deadcode (elimination)
26+
assert.ok(rs);
27+
break;
28+
case 'WritableStream':
29+
bench.start();
30+
for (let i = 0; i < n; ++i)
31+
ws = new WritableStream();
32+
bench.end(n);
33+
34+
// Avoid V8 deadcode (elimination)
35+
assert.ok(ws);
36+
break;
37+
case 'TransformStream':
38+
bench.start();
39+
for (let i = 0; i < n; ++i)
40+
ts = new TransformStream();
41+
bench.end(n);
42+
43+
// Avoid V8 deadcode (elimination)
44+
assert.ok(ts);
45+
break;
46+
default:
47+
throw new Error('Invalid kind');
48+
}
49+
}

benchmark/webstreams/pipe-to.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const {
4+
ReadableStream,
5+
WritableStream,
6+
} = require('node:stream/web');
7+
8+
const bench = common.createBenchmark(main, {
9+
n: [5e6],
10+
highWaterMarkR: [512, 1024, 2048, 4096],
11+
highWaterMarkW: [512, 1024, 2048, 4096],
12+
});
13+
14+
15+
async function main({ n, highWaterMarkR, highWaterMarkW }) {
16+
const b = Buffer.alloc(1024);
17+
let i = 0;
18+
const rs = new ReadableStream({
19+
highWaterMark: highWaterMarkR,
20+
pull: function(controller) {
21+
if (i++ === n) {
22+
controller.enqueue(b);
23+
} else {
24+
controller.close();
25+
}
26+
}
27+
});
28+
const ws = new WritableStream({
29+
highWaterMark: highWaterMarkW,
30+
write(chunk, controller) {},
31+
close() { bench.end(n); },
32+
});
33+
34+
bench.start();
35+
rs.pipeTo(ws);
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const {
4+
ReadableStream,
5+
} = require('node:stream/web');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e5],
9+
});
10+
11+
12+
async function main({ n }) {
13+
const rs = new ReadableStream({
14+
pull: function(controller) {
15+
controller.enqueue(1);
16+
}
17+
});
18+
19+
let x = 0;
20+
21+
bench.start();
22+
for await (const chunk of rs) {
23+
x += chunk;
24+
if (x > n) {
25+
break;
26+
}
27+
}
28+
// Use x to ensure V8 does not optimize away the loop as a noop.
29+
console.assert(x);
30+
bench.end(n);
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const runBenchmark = require('../common/benchmark');
6+
7+
runBenchmark('webstreams', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

0 commit comments

Comments
 (0)