perf_hooks: add histogram option to timerify

Allows setting a `Histogram` object option on timerify to
record function execution times over time.

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/37475
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
James M Snell committed 2021-03-05 08:16:03 -08:00
1 parent 9c1274a123
commit b803bca4fa
4 files changed
+82 -5

No files matched your search

@@ -4,10 +4,15 @@ const common = require('../common');
const assert = require('assert');
const {
createHistogram,
performance,
PerformanceObserver
} = require('perf_hooks');
const {
setTimeout: sleep
} = require('timers/promises');
{
// Intentional non-op. Do not wrap in common.mustCall();
const n = performance.timerify(function noop() {});
@@ -81,3 +86,38 @@ const {
assert.strictEqual(n.length, m.length);
assert.strictEqual(n.name, 'timerified m');
}
(async () => {
const histogram = createHistogram();
const m = (a, b = 1) => {};
const n = performance.timerify(m, { histogram });
assert.strictEqual(histogram.max, 0);
for (let i = 0; i < 10; i++) {
n();
await sleep(10);
}
assert.notStrictEqual(histogram.max, 0);
[1, '', {}, [], false].forEach((histogram) => {
assert.throws(() => performance.timerify(m, { histogram }), {
code: 'ERR_INVALID_ARG_TYPE'
});
});
})().then(common.mustCall());
(async () => {
const histogram = createHistogram();
const m = async (a, b = 1) => {
await sleep(10);
};
const n = performance.timerify(m, { histogram });
assert.strictEqual(histogram.max, 0);
for (let i = 0; i < 10; i++) {
await n();
}
assert.notStrictEqual(histogram.max, 0);
[1, '', {}, [], false].forEach((histogram) => {
assert.throws(() => performance.timerify(m, { histogram }), {
code: 'ERR_INVALID_ARG_TYPE'
});
});
})().then(common.mustCall());