Skip to content

Commit

Permalink
benchmark: include webstreams benchmark
Browse files Browse the repository at this point in the history
Signed-off-by: RafaelGSS <[email protected]>
  • Loading branch information
RafaelGSS committed Dec 15, 2022
1 parent 4166d40 commit e880275
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
46 changes: 46 additions & 0 deletions benchmark/webstreams/creation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
const common = require('../common.js');
const {
ReadableStream,
TransformStream,
WritableStream,
} = require('node:stream/web');

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

function main({ n, kind }) {
switch (kind) {
case 'ReadableStream':
new ReadableStream({});
new ReadableStream();

bench.start();
for (let i = 0; i < n; ++i)
new ReadableStream().cancel();
bench.end(n);
break;
case 'WritableStream':
new WritableStream({});
new WritableStream();

bench.start();
for (let i = 0; i < n; ++i)
new WritableStream();
bench.end(n);
break;
case 'TransformStream':
new TransformStream({});
new TransformStream();

bench.start();
for (let i = 0; i < n; ++i)
new TransformStream();
bench.end(n);
break;
default:
throw new Error('Invalid kind');
}
}
32 changes: 32 additions & 0 deletions benchmark/webstreams/pipe-to.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
const common = require('../common.js');
const {
ReadableStream,
WritableStream,
} = require('node:stream/web');

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


async function main({ n }) {
const b = Buffer.alloc(1024);
let i = 0;
const rs = new ReadableStream({
pull: function(controller) {
if (i++ === n) {
controller.enqueue(b);
} else {
controller.close();
}
}
});
const ws = new WritableStream({
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;
for await (const chunk of rs) {
x += chunk;
if (x > n) {
break;
}
}

bench.start();
// Use x to ensure V8 does not optimize away the loop as a noop.
console.assert(x);
bench.end(n);
}

0 comments on commit e880275

Please sign in to comment.