From d8951303d1ee2a8768717dc77b4eb4fd9f8e4953 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Wed, 25 Oct 2023 22:46:33 +0200 Subject: [PATCH] benchmark: add benchmarks for encodings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/50348 Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Yagiz Nizipli --- benchmark/buffers/buffer-base64-decode.js | 10 ++++++- benchmark/buffers/buffer-base64-encode.js | 11 ++++++- benchmark/buffers/buffer-base64url-decode.js | 29 +++++++++++++++++++ benchmark/buffers/buffer-base64url-encode.js | 29 +++++++++++++++++++ benchmark/buffers/buffer-hex-decode.js | 29 +++++++++++++++++++ .../{buffer-hex.js => buffer-hex-encode.js} | 6 +++- 6 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 benchmark/buffers/buffer-base64url-decode.js create mode 100644 benchmark/buffers/buffer-base64url-encode.js create mode 100644 benchmark/buffers/buffer-hex-decode.js rename benchmark/buffers/{buffer-hex.js => buffer-hex-encode.js} (74%) diff --git a/benchmark/buffers/buffer-base64-decode.js b/benchmark/buffers/buffer-base64-decode.js index 4a8ed4466df413..f98e621c2dd836 100644 --- a/benchmark/buffers/buffer-base64-decode.js +++ b/benchmark/buffers/buffer-base64-decode.js @@ -15,7 +15,15 @@ function main({ n, size }) { assert.strictEqual(s.length % 4, 0); const b = Buffer.allocUnsafe(encodedSize); b.write(s, 0, encodedSize, 'base64'); + + let tmp; + bench.start(); - for (let i = 0; i < n; i += 1) b.base64Write(s, 0, s.length); + + for (let i = 0; i < n; i += 1) + tmp = b.base64Write(s, 0, s.length); + bench.end(n); + + assert.strictEqual(tmp, encodedSize); } diff --git a/benchmark/buffers/buffer-base64-encode.js b/benchmark/buffers/buffer-base64-encode.js index 0571f3181ae4e2..99f633b65fd871 100644 --- a/benchmark/buffers/buffer-base64-encode.js +++ b/benchmark/buffers/buffer-base64-encode.js @@ -21,6 +21,7 @@ 'use strict'; const common = require('../common.js'); +const assert = require('assert'); const bench = common.createBenchmark(main, { len: [64 * 1024 * 1024], @@ -35,7 +36,15 @@ function main({ n, len }) { let i; for (i = 0; i < 256; ++i) s += String.fromCharCode(i); for (i = 0; i < len; i += 256) b.write(s, i, 256, 'ascii'); + + let tmp; + bench.start(); - for (i = 0; i < n; ++i) b.toString('base64'); + + for (i = 0; i < n; ++i) + tmp = b.toString('base64'); + bench.end(n); + + assert.strictEqual(typeof tmp, 'string'); } diff --git a/benchmark/buffers/buffer-base64url-decode.js b/benchmark/buffers/buffer-base64url-decode.js new file mode 100644 index 00000000000000..276dfc8d23b543 --- /dev/null +++ b/benchmark/buffers/buffer-base64url-decode.js @@ -0,0 +1,29 @@ +'use strict'; +const assert = require('assert'); +const common = require('../common.js'); + +const bench = common.createBenchmark(main, { + n: [32], + size: [8 << 20], +}); + +function main({ n, size }) { + const s = 'abcd'.repeat(size); + const encodedSize = s.length * 3 / 4; + // eslint-disable-next-line node-core/no-unescaped-regexp-dot + s.match(/./); // Flatten string. + assert.strictEqual(s.length % 4, 0); + const b = Buffer.allocUnsafe(encodedSize); + b.write(s, 0, encodedSize, 'base64url'); + + let tmp; + + bench.start(); + + for (let i = 0; i < n; i += 1) + tmp = b.base64Write(s, 0, s.length); + + bench.end(n); + + assert.strictEqual(tmp, encodedSize); +} diff --git a/benchmark/buffers/buffer-base64url-encode.js b/benchmark/buffers/buffer-base64url-encode.js new file mode 100644 index 00000000000000..cb4ea28c29d005 --- /dev/null +++ b/benchmark/buffers/buffer-base64url-encode.js @@ -0,0 +1,29 @@ +'use strict'; +const common = require('../common.js'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + len: [64 * 1024 * 1024], + n: [32], +}, { + test: { len: 256 }, +}); + +function main({ n, len }) { + const b = Buffer.allocUnsafe(len); + let s = ''; + let i; + for (i = 0; i < 256; ++i) s += String.fromCharCode(i); + for (i = 0; i < len; i += 256) b.write(s, i, 256, 'ascii'); + + let tmp; + + bench.start(); + + for (i = 0; i < n; ++i) + tmp = b.toString('base64url'); + + bench.end(n); + + assert.strictEqual(typeof tmp, 'string'); +} diff --git a/benchmark/buffers/buffer-hex-decode.js b/benchmark/buffers/buffer-hex-decode.js new file mode 100644 index 00000000000000..4d4db07238c630 --- /dev/null +++ b/benchmark/buffers/buffer-hex-decode.js @@ -0,0 +1,29 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + len: [64, 1024], + n: [1e6], +}); + +function main({ len, n }) { + const buf = Buffer.alloc(len); + + for (let i = 0; i < buf.length; i++) + buf[i] = i & 0xff; + + const plain = buf; + + bench.start(); + + let tmp; + + for (let i = 0; i < n; i += 1) + tmp = plain.toString('hex'); + + bench.end(n); + + assert.strictEqual(typeof tmp, 'string'); +} diff --git a/benchmark/buffers/buffer-hex.js b/benchmark/buffers/buffer-hex-encode.js similarity index 74% rename from benchmark/buffers/buffer-hex.js rename to benchmark/buffers/buffer-hex-encode.js index 6e61d71c9bacf1..d038ce95f0aec8 100644 --- a/benchmark/buffers/buffer-hex.js +++ b/benchmark/buffers/buffer-hex-encode.js @@ -1,6 +1,7 @@ 'use strict'; const common = require('../common.js'); +const assert = require('assert'); const bench = common.createBenchmark(main, { len: [64, 1024], @@ -14,11 +15,14 @@ function main({ len, n }) { buf[i] = i & 0xff; const hex = buf.toString('hex'); + let tmp; bench.start(); for (let i = 0; i < n; i += 1) - Buffer.from(hex, 'hex'); + tmp = Buffer.from(hex, 'hex'); bench.end(n); + + assert.strictEqual(typeof tmp, 'object'); }