mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
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:
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());
|
||||
Reference in new issue
Block a user