mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
The previous busy loop wasn't robust enough in making sure that the function runs for more than 1 nanosecond - and when it runs faster than that on a fast machine, it measures to 0 for nanosecond precision and throws a RangeErorr as histogram.record() only takes positive values. Update it to use Atomics.wait() to make sure that the function being measured runs for at least 1 millisecond so that the histogram always records a positive value. PR-URL: https://github.com/nodejs/node/pull/60639 Fixes: https://github.com/nodejs/node/issues/60638 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
20 lines
604 B
JavaScript
20 lines
604 B
JavaScript
// Test that timerify works with histogram option for synchronous functions.
|
|
|
|
import { sleepSync } from '../common/index.mjs';
|
|
import assert from 'assert';
|
|
import { createHistogram, timerify } from 'perf_hooks';
|
|
|
|
const histogram = createHistogram();
|
|
|
|
const m = () => {
|
|
// Deterministic blocking delay (~1 millisecond). The histogram operates on
|
|
// nanosecond precision, so this should be sufficient to prevent zero timings.
|
|
sleepSync(1);
|
|
};
|
|
const n = timerify(m, { histogram });
|
|
assert.strictEqual(histogram.max, 0);
|
|
for (let i = 0; i < 10; i++) {
|
|
n();
|
|
}
|
|
assert.notStrictEqual(histogram.max, 0);
|