Skip to content

Commit f8ecd4f

Browse files
committed
perf_hooks: ensure timerify records at least 1ns in histogram
1 parent 68d7b6f commit f8ecd4f

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

lib/internal/perf/timerify.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
FunctionPrototypeBind,
55
MathCeil,
6+
MathMax,
67
ObjectDefineProperties,
78
ReflectApply,
89
ReflectConstruct,
@@ -37,7 +38,7 @@ const {
3738
function processComplete(name, start, args, histogram) {
3839
const duration = now() - start;
3940
if (histogram !== undefined)
40-
histogram.record(MathCeil(duration * 1e6));
41+
histogram.record(MathMax(1, MathCeil(duration * 1e6)));
4142
const entry =
4243
createPerformanceNodeEntry(
4344
name,

test/parallel/test-perf-hooks-timerify-histogram-sync.mjs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,32 @@ import { sleepSync } from '../common/index.mjs';
44
import assert from 'assert';
55
import { createHistogram, timerify } from 'perf_hooks';
66

7-
const histogram = createHistogram();
7+
{
8+
const histogram = createHistogram();
89

9-
const m = () => {
10-
// Deterministic blocking delay (~1 millisecond). The histogram operates on
11-
// nanosecond precision, so this should be sufficient to prevent zero timings.
12-
sleepSync(1);
13-
};
14-
const n = timerify(m, { histogram });
15-
assert.strictEqual(histogram.max, 0);
16-
for (let i = 0; i < 10; i++) {
17-
n();
10+
const m = () => {
11+
// Deterministic blocking delay (~1 millisecond). The histogram operates on
12+
// nanosecond precision, so this should be sufficient to prevent zero timings.
13+
sleepSync(1);
14+
};
15+
const n = timerify(m, { histogram });
16+
assert.strictEqual(histogram.max, 0);
17+
for (let i = 0; i < 10; i++) {
18+
n();
19+
}
20+
assert.notStrictEqual(histogram.max, 0);
21+
}
22+
23+
// Regression test for https://github.com/nodejs/node/issues/54803
24+
// Sub-nanosecond functions must not throw ERR_OUT_OF_RANGE.
25+
{
26+
const histogram = createHistogram();
27+
const m = () => {};
28+
const n = timerify(m, { histogram });
29+
for (let j = 0; j < 100; j++) {
30+
for (let i = 0; i < 1000; i++) {
31+
n();
32+
}
33+
}
34+
assert.ok(histogram.min >= 1);
1835
}
19-
assert.notStrictEqual(histogram.max, 0);

0 commit comments

Comments
 (0)