Skip to content

Commit

Permalink
benchmark: stablize encode benchmark
Browse files Browse the repository at this point in the history
- 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: #46658
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
joyeecheung authored and targos committed Nov 10, 2023
1 parent 66e4ba5 commit 24d3fcf
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions benchmark/util/text-encoder.js
Original file line number Diff line number Diff line change
@@ -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'],
});
Expand All @@ -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);
}

0 comments on commit 24d3fcf

Please sign in to comment.