From 710b0ac680f0a8078361251ebd812b71d0263a92 Mon Sep 17 00:00:00 2001 From: Sergey Chernyshev Date: Thu, 25 Apr 2024 01:53:13 +0200 Subject: [PATCH] benchmark: stablize encode benchmark - Increase the number of iteration to 1e6 to reduce flakes. 1e4 can introduce flakes even when comparing the main branch against itself - Replace the 1024 * 32 length test with 1024 * 8 since it would otherwise take too long to complete. Remove the 16 length test since it's not too different from 32. - Check the results of the encoding methods at the end. PR-URL: https://github.com/nodejs/node/pull/46658 Reviewed-By: Darshan Sen --- graal-nodejs/benchmark/util/text-encoder.js | 39 ++++++++++++--------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/graal-nodejs/benchmark/util/text-encoder.js b/graal-nodejs/benchmark/util/text-encoder.js index 1429710e9cb..3150a6fbc88 100644 --- a/graal-nodejs/benchmark/util/text-encoder.js +++ b/graal-nodejs/benchmark/util/text-encoder.js @@ -1,10 +1,11 @@ 'use strict'; const common = require('../common.js'); +const assert = require('assert'); const bench = common.createBenchmark(main, { - len: [16, 32, 256, 1024, 1024 * 32], - n: [1e4], + len: [32, 256, 1024, 1024 * 8], + n: [1e6], type: ['one-byte-string', 'two-byte-string', 'ascii'], op: ['encode', 'encodeInto'], }); @@ -26,20 +27,24 @@ function main({ n, op, len, type }) { } const input = base.repeat(len); - const subarray = new Uint8Array(len); - - bench.start(); - switch (op) { - case 'encode': { - for (let i = 0; i < n; i++) - encoder.encode(input); - break; - } - case 'encodeInto': { - for (let i = 0; i < n; i++) - encoder.encodeInto(input, subarray); - break; - } + if (op === 'encode') { + const expected = encoder.encode(input); + let result; + bench.start(); + for (let i = 0; i < n; i++) + result = encoder.encode(input); + bench.end(n); + assert.deepStrictEqual(result, expected); + } else { + const expected = new Uint8Array(len); + const subarray = new Uint8Array(len); + const expectedStats = encoder.encodeInto(input, expected); + let result; + bench.start(); + for (let i = 0; i < n; i++) + result = encoder.encodeInto(input, subarray); + bench.end(n); + assert.deepStrictEqual(subarray, expected); + assert.deepStrictEqual(result, expectedStats); } - bench.end(n); }