node/lib/internal/perf/event_loop_delay.js
James M Snell 23637e9a3b
perf_hooks: multiple fixes for Histogram
* The createHistogram(options) options weren't actually implemented
* Add a new count property that tracks the number of samples
* Adds BigInt options for relevant properties
* Adds add(other) method for RecordableHistogram
* Cleans up and expands tests
* Eliminates unnecessary ELDHistogram native class
* Improve/Simplify histogram transfer impl

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

perf_hooks: simplify Histogram constructor options

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

PR-URL: https://github.com/nodejs/node/pull/41153
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2021-12-19 09:56:36 -08:00

87 lines
1.6 KiB
JavaScript

'use strict';
const {
ReflectConstruct,
SafeMap,
Symbol,
} = primordials;
const {
codes: {
ERR_ILLEGAL_CONSTRUCTOR,
ERR_INVALID_THIS,
}
} = require('internal/errors');
const {
createELDHistogram,
} = internalBinding('performance');
const {
validateInteger,
validateObject,
} = require('internal/validators');
const {
Histogram,
kHandle,
kMap,
} = require('internal/histogram');
const {
makeTransferable,
} = require('internal/worker/js_transferable');
const kEnabled = Symbol('kEnabled');
class ELDHistogram extends Histogram {
constructor(i) {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}
/**
* @returns {boolean}
*/
enable() {
if (this[kEnabled] === undefined)
throw new ERR_INVALID_THIS('ELDHistogram');
if (this[kEnabled]) return false;
this[kEnabled] = true;
this[kHandle].start();
return true;
}
/**
* @returns {boolean}
*/
disable() {
if (this[kEnabled] === undefined)
throw new ERR_INVALID_THIS('ELDHistogram');
if (!this[kEnabled]) return false;
this[kEnabled] = false;
this[kHandle].stop();
return true;
}
}
/**
* @param {{
* resolution : number
* }} [options]
* @returns {ELDHistogram}
*/
function monitorEventLoopDelay(options = {}) {
validateObject(options, 'options');
const { resolution = 10 } = options;
validateInteger(resolution, 'options.resolution', 1);
return makeTransferable(ReflectConstruct(
function() {
this[kEnabled] = false;
this[kHandle] = createELDHistogram(resolution);
this[kMap] = new SafeMap();
}, [], ELDHistogram));
}
module.exports = monitorEventLoopDelay;