Skip to content

Commit 710b0ac

Browse files
committed
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: nodejs/node#46658 Reviewed-By: Darshan Sen <[email protected]>
1 parent 908095f commit 710b0ac

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed
+22-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict';
22

33
const common = require('../common.js');
4+
const assert = require('assert');
45

56
const bench = common.createBenchmark(main, {
6-
len: [16, 32, 256, 1024, 1024 * 32],
7-
n: [1e4],
7+
len: [32, 256, 1024, 1024 * 8],
8+
n: [1e6],
89
type: ['one-byte-string', 'two-byte-string', 'ascii'],
910
op: ['encode', 'encodeInto'],
1011
});
@@ -26,20 +27,24 @@ function main({ n, op, len, type }) {
2627
}
2728

2829
const input = base.repeat(len);
29-
const subarray = new Uint8Array(len);
30-
31-
bench.start();
32-
switch (op) {
33-
case 'encode': {
34-
for (let i = 0; i < n; i++)
35-
encoder.encode(input);
36-
break;
37-
}
38-
case 'encodeInto': {
39-
for (let i = 0; i < n; i++)
40-
encoder.encodeInto(input, subarray);
41-
break;
42-
}
30+
if (op === 'encode') {
31+
const expected = encoder.encode(input);
32+
let result;
33+
bench.start();
34+
for (let i = 0; i < n; i++)
35+
result = encoder.encode(input);
36+
bench.end(n);
37+
assert.deepStrictEqual(result, expected);
38+
} else {
39+
const expected = new Uint8Array(len);
40+
const subarray = new Uint8Array(len);
41+
const expectedStats = encoder.encodeInto(input, expected);
42+
let result;
43+
bench.start();
44+
for (let i = 0; i < n; i++)
45+
result = encoder.encodeInto(input, subarray);
46+
bench.end(n);
47+
assert.deepStrictEqual(subarray, expected);
48+
assert.deepStrictEqual(result, expectedStats);
4349
}
44-
bench.end(n);
4550
}

0 commit comments

Comments
 (0)