From b70757476cce04b46e551a89e2973ca4a03f77bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Tue, 19 Sep 2023 22:55:09 -0300 Subject: [PATCH] benchmark: added new benchmarks for blob PR-URL: https://github.com/nodejs/node/pull/49730 Reviewed-By: Yagiz Nizipli Reviewed-By: Zeyu "Alex" Yang Reviewed-By: Matthew Aitken Reviewed-By: Benjamin Gruenbaum Reviewed-By: Rafael Gonzaga Reviewed-By: Stephen Belanger --- benchmark/blob/clone.js | 22 ++++++++++++++ benchmark/blob/creation.js | 48 ++++++++++++++++++++++++++++++ benchmark/blob/resolveObjectURL.js | 22 ++++++++++++++ benchmark/blob/slice.js | 27 +++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 benchmark/blob/clone.js create mode 100644 benchmark/blob/creation.js create mode 100644 benchmark/blob/resolveObjectURL.js create mode 100644 benchmark/blob/slice.js diff --git a/benchmark/blob/clone.js b/benchmark/blob/clone.js new file mode 100644 index 00000000000000..7f66255ad9da0c --- /dev/null +++ b/benchmark/blob/clone.js @@ -0,0 +1,22 @@ +'use strict'; +const common = require('../common.js'); +const { + Blob, +} = require('node:buffer'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [50e3], +}); + +let _cloneResult; + +function main({ n }) { + bench.start(); + for (let i = 0; i < n; ++i) + _cloneResult = structuredClone(new Blob(['hello'])); + bench.end(n); + + // Avoid V8 deadcode (elimination) + assert.ok(_cloneResult); +} diff --git a/benchmark/blob/creation.js b/benchmark/blob/creation.js new file mode 100644 index 00000000000000..906d301bca850a --- /dev/null +++ b/benchmark/blob/creation.js @@ -0,0 +1,48 @@ +'use strict'; +const common = require('../common.js'); +const { + Blob, +} = require('node:buffer'); +const assert = require('assert'); + +const options = { + flags: ['--expose-internals'], +}; + +const bench = common.createBenchmark(main, { + n: [50e3], + kind: [ + 'Blob', + 'createBlob', + ], +}, options); + +let _blob; +let _createBlob; + +function main({ n, kind }) { + switch (kind) { + case 'Blob': { + bench.start(); + for (let i = 0; i < n; ++i) + _blob = new Blob(['hello']); + bench.end(n); + + // Avoid V8 deadcode (elimination) + assert.ok(_blob); + break; + } case 'createBlob': { + const { createBlob } = require('internal/blob'); + const reusableBlob = new Blob(['hello']); + bench.start(); + for (let i = 0; i < n; ++i) + _createBlob = createBlob(reusableBlob, reusableBlob.size); + bench.end(n); + + // Avoid V8 deadcode (elimination) + assert.ok(_createBlob); + break; + } default: + throw new Error('Invalid kind'); + } +} diff --git a/benchmark/blob/resolveObjectURL.js b/benchmark/blob/resolveObjectURL.js new file mode 100644 index 00000000000000..3b70c69f66dea6 --- /dev/null +++ b/benchmark/blob/resolveObjectURL.js @@ -0,0 +1,22 @@ +'use strict'; +const common = require('../common.js'); +const { Blob, resolveObjectURL } = require('node:buffer'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [50e3], +}); + +let _resolveObjectURL; + +function main({ n }) { + const blob = new Blob(['hello']); + const id = URL.createObjectURL(blob); + bench.start(); + for (let i = 0; i < n; ++i) + _resolveObjectURL = resolveObjectURL(id); + bench.end(n); + + // Avoid V8 deadcode (elimination) + assert.ok(_resolveObjectURL); +} diff --git a/benchmark/blob/slice.js b/benchmark/blob/slice.js new file mode 100644 index 00000000000000..fcad0d68584353 --- /dev/null +++ b/benchmark/blob/slice.js @@ -0,0 +1,27 @@ +'use strict'; +const common = require('../common.js'); +const { Blob } = require('node:buffer'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [50e3], + dataSize: [ + 5, + 30 * 1024, + ], +}); + +let _sliceResult; + +function main({ n, dataSize }) { + const data = 'a'.repeat(dataSize); + const reusableBlob = new Blob([data]); + + bench.start(); + for (let i = 0; i < n; ++i) + _sliceResult = reusableBlob.slice(0, Math.floor(data.length / 2)); + bench.end(n); + + // Avoid V8 deadcode (elimination) + assert.ok(_sliceResult); +}