Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: include webstreams benchmark #45876

Merged
merged 1 commit into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions benchmark/webstreams/creation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';
const common = require('../common.js');
const {
ReadableStream,
TransformStream,
WritableStream,
} = require('node:stream/web');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [50e3],
kind: ['ReadableStream', 'TransformStream', 'WritableStream']
});

let rs, ws, ts;

function main({ n, kind }) {
switch (kind) {
case 'ReadableStream':
bench.start();
for (let i = 0; i < n; ++i)
rs = new ReadableStream();
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(rs);
break;
case 'WritableStream':
bench.start();
for (let i = 0; i < n; ++i)
ws = new WritableStream();
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(ws);
break;
case 'TransformStream':
bench.start();
for (let i = 0; i < n; ++i)
ts = new TransformStream();
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(ts);
break;
default:
throw new Error('Invalid kind');
}
}
36 changes: 36 additions & 0 deletions benchmark/webstreams/pipe-to.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
const common = require('../common.js');
const {
ReadableStream,
WritableStream,
} = require('node:stream/web');

const bench = common.createBenchmark(main, {
n: [5e6],
highWaterMarkR: [512, 1024, 2048, 4096],
highWaterMarkW: [512, 1024, 2048, 4096],
});


async function main({ n, highWaterMarkR, highWaterMarkW }) {
const b = Buffer.alloc(1024);
let i = 0;
const rs = new ReadableStream({
highWaterMark: highWaterMarkR,
pull: function(controller) {
RafaelGSS marked this conversation as resolved.
Show resolved Hide resolved
if (i++ === n) {
controller.enqueue(b);
} else {
controller.close();
}
}
});
const ws = new WritableStream({
highWaterMark: highWaterMarkW,
write(chunk, controller) {},
close() { bench.end(n); },
});

bench.start();
rs.pipeTo(ws);
}
31 changes: 31 additions & 0 deletions benchmark/webstreams/readable-async-iterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
const common = require('../common.js');
const {
ReadableStream,
} = require('node:stream/web');

const bench = common.createBenchmark(main, {
n: [1e5],
});


async function main({ n }) {
const rs = new ReadableStream({
pull: function(controller) {
controller.enqueue(1);
}
});

let x = 0;

bench.start();
for await (const chunk of rs) {
anonrig marked this conversation as resolved.
Show resolved Hide resolved
x += chunk;
if (x > n) {
break;
}
}
// Use x to ensure V8 does not optimize away the loop as a noop.
console.assert(x);
bench.end(n);
}
7 changes: 7 additions & 0 deletions test/benchmark/test-benchmark-webstreams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

require('../common');

const runBenchmark = require('../common/benchmark');

runBenchmark('webstreams', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });