test: expand histogram test coverage · nodejs/node@f7d18ec · GitHub
Skip to content

Commit f7d18ec

Browse files
jasnelladuh95
authored andcommitted
test: expand histogram test coverage
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode PR-URL: #65825 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 13e61f6 commit f7d18ec

4 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions

‎test/parallel/test-perf-hooks-histogram-qrde.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ function assertClose(actual, expected, tolerance = 1e-12) {
99
`${actual} != ${expected}`);
1010
}
1111

12+
function recordRepeated(histogram, options, value, count) {
13+
const block = createHistogram(options);
14+
block.record(value);
15+
while (count > 0) {
16+
if (count % 2 === 1) histogram.add(block);
17+
count = Math.floor(count / 2);
18+
if (count > 0) block.add(block);
19+
}
20+
}
21+
1222
(async () => {
1323
const empty = createHistogram();
1424
const emptyResult = await empty.qrde();
@@ -23,6 +33,9 @@ function assertClose(actual, expected, tolerance = 1e-12) {
2333
assert.strictEqual(emptyResult.corrections, 0);
2434
assert.strictEqual(emptyResult.dequantize, 'hdr');
2535

36+
assert.throws(() => empty.qrde.call({}), {
37+
code: 'ERR_INVALID_THIS',
38+
});
2639
assert.throws(() => empty.qrde(null), {
2740
code: 'ERR_INVALID_ARG_TYPE',
2841
});
@@ -219,4 +232,18 @@ function assertClose(actual, expected, tolerance = 1e-12) {
219232
await largeCount.qrde({ bins: 2, dequantize: 'none' });
220233
assert.strictEqual(largeCountResult.count, (1n << 53n) + 1n);
221234
assertClose(largeCountResult.quantiles[1], 2);
235+
236+
// Exercise correction across the exact-to-asymptotic beta CDF threshold.
237+
const correctionOptions = { highest: 131071, figures: 5 };
238+
const correction = createHistogram(correctionOptions);
239+
recordRepeated(correction, correctionOptions, 1, 26239);
240+
recordRepeated(correction, correctionOptions, 131071, 973761);
241+
const count = 1_000_000;
242+
const threshold = (1 - Math.sqrt(1 - 100_000 / (count + 1))) / 2;
243+
const corrected = await correction.qrde({
244+
probabilities: [0, threshold - 1e-10, threshold + 1e-10, 1],
245+
dequantize: 'none',
246+
});
247+
assert.strictEqual(corrected.corrections, 1);
248+
assert.strictEqual(corrected.quantiles[1], corrected.quantiles[2]);
222249
})().then(common.mustCall());

‎test/parallel/test-perf-hooks-sliding-window-histogram.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ const {
4949
assert.strictEqual(histogram.snapshot().count, 0);
5050
histogram.record(10n);
5151
assert.strictEqual(histogram.snapshot().maxBigInt, 10n);
52+
for (const value of [0n, 2n ** 63n]) {
53+
assert.throws(() => histogram.record(value), {
54+
code: 'ERR_OUT_OF_RANGE',
55+
});
56+
}
5257

5358
assert.throws(() => new histogram.constructor(), {
5459
code: 'ERR_ILLEGAL_CONSTRUCTOR',
@@ -142,6 +147,19 @@ const {
142147
highest: 10,
143148
}), { code: 'ERR_OUT_OF_RANGE' });
144149

150+
for (const [name, value] of [
151+
['lowest', 0n],
152+
['lowest', 2n ** 63n],
153+
['highest', 0n],
154+
['highest', 2n ** 63n],
155+
]) {
156+
assert.throws(() => createSlidingWindowHistogram({
157+
chunks: 2,
158+
recordsPerChunk: 1,
159+
[name]: value,
160+
}), { code: 'ERR_OUT_OF_RANGE' });
161+
}
162+
145163
for (const bounds of [
146164
{ lowest: 1n },
147165
{ lowest: 1n, highest: 100 },
Lines changed: 54 additions & 0 deletions

0 commit comments

Comments
 (0)