From 846b57b3f30f6a0b55f9a80e32a128bb047ef4c7 Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Thu, 15 Dec 2022 18:23:06 -0300 Subject: [PATCH] benchmark: include webstreams benchmark Signed-off-by: RafaelGSS --- benchmark/webstreams/creation.js | 49 +++++++++++++++++++ benchmark/webstreams/pipe-to.js | 32 ++++++++++++ .../webstreams/readable-async-iterator.js | 31 ++++++++++++ test/benchmark/test-benchmark-webstreams.js | 7 +++ 4 files changed, 119 insertions(+) create mode 100644 benchmark/webstreams/creation.js create mode 100644 benchmark/webstreams/pipe-to.js create mode 100644 benchmark/webstreams/readable-async-iterator.js create mode 100644 test/benchmark/test-benchmark-webstreams.js diff --git a/benchmark/webstreams/creation.js b/benchmark/webstreams/creation.js new file mode 100644 index 00000000000000..c811a74a35b38d --- /dev/null +++ b/benchmark/webstreams/creation.js @@ -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 assignment + assert.ok(rs); + break; + case 'WritableStream': + bench.start(); + for (let i = 0; i < n; ++i) + ws = new WritableStream(); + bench.end(n); + + // Avoid V8 deadcode assignment + assert.ok(ws); + break; + case 'TransformStream': + bench.start(); + for (let i = 0; i < n; ++i) + ts = new TransformStream(); + bench.end(n); + + // Avoid V8 deadcode assignment + assert.ok(ts); + break; + default: + throw new Error('Invalid kind'); + } +} diff --git a/benchmark/webstreams/pipe-to.js b/benchmark/webstreams/pipe-to.js new file mode 100644 index 00000000000000..e9fb68118fcdac --- /dev/null +++ b/benchmark/webstreams/pipe-to.js @@ -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); +} diff --git a/benchmark/webstreams/readable-async-iterator.js b/benchmark/webstreams/readable-async-iterator.js new file mode 100644 index 00000000000000..2a73851be6b967 --- /dev/null +++ b/benchmark/webstreams/readable-async-iterator.js @@ -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); +} diff --git a/test/benchmark/test-benchmark-webstreams.js b/test/benchmark/test-benchmark-webstreams.js new file mode 100644 index 00000000000000..c4146c859a98b9 --- /dev/null +++ b/test/benchmark/test-benchmark-webstreams.js @@ -0,0 +1,7 @@ +'use strict'; + +require('../common'); + +const runBenchmark = require('../common/benchmark'); + +runBenchmark('webstreams', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });