2020-02-27 21:14:38 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-30 17:26:15 +00:00
|
|
|
const {
|
|
|
|
NumberIsNaN,
|
|
|
|
NumberIsInteger,
|
|
|
|
NumberMAX_SAFE_INTEGER,
|
|
|
|
ObjectSetPrototypeOf,
|
|
|
|
SafeMap,
|
|
|
|
Symbol,
|
|
|
|
TypeError,
|
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
const {
|
|
|
|
Histogram: _Histogram
|
|
|
|
} = internalBinding('performance');
|
|
|
|
|
2020-02-27 21:14:38 +00:00
|
|
|
const {
|
|
|
|
customInspectSymbol: kInspect,
|
|
|
|
} = require('internal/util');
|
|
|
|
|
2021-01-30 17:26:15 +00:00
|
|
|
const { inspect } = require('util');
|
|
|
|
|
|
|
|
const {
|
|
|
|
codes: {
|
|
|
|
ERR_INVALID_ARG_VALUE,
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_OUT_OF_RANGE,
|
|
|
|
},
|
|
|
|
} = require('internal/errors');
|
2020-02-27 21:14:38 +00:00
|
|
|
|
2021-01-30 17:26:15 +00:00
|
|
|
const {
|
|
|
|
validateNumber,
|
|
|
|
} = require('internal/validators');
|
2020-02-27 21:14:38 +00:00
|
|
|
|
|
|
|
const kDestroy = Symbol('kDestroy');
|
|
|
|
const kHandle = Symbol('kHandle');
|
2021-01-30 17:26:15 +00:00
|
|
|
const kMap = Symbol('kMap');
|
2020-02-27 21:14:38 +00:00
|
|
|
|
2021-01-30 17:26:15 +00:00
|
|
|
const {
|
|
|
|
kClone,
|
|
|
|
kDeserialize,
|
|
|
|
JSTransferable,
|
|
|
|
} = require('internal/worker/js_transferable');
|
2020-02-27 21:14:38 +00:00
|
|
|
|
2021-02-22 17:16:32 +00:00
|
|
|
function isHistogram(object) {
|
|
|
|
return object?.[kHandle] !== undefined;
|
|
|
|
}
|
|
|
|
|
2021-01-30 17:26:15 +00:00
|
|
|
class Histogram extends JSTransferable {
|
2020-02-27 21:14:38 +00:00
|
|
|
constructor(internal) {
|
2021-01-30 17:26:15 +00:00
|
|
|
super();
|
2020-12-09 13:43:07 +00:00
|
|
|
this[kHandle] = internal;
|
2021-01-30 17:26:15 +00:00
|
|
|
this[kMap] = new SafeMap();
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 17:26:15 +00:00
|
|
|
[kInspect](depth, options) {
|
|
|
|
if (depth < 0)
|
|
|
|
return this;
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
...options,
|
|
|
|
depth: options.depth == null ? null : options.depth - 1
|
|
|
|
};
|
|
|
|
|
|
|
|
return `Histogram ${inspect({
|
2020-02-27 21:14:38 +00:00
|
|
|
min: this.min,
|
|
|
|
max: this.max,
|
|
|
|
mean: this.mean,
|
|
|
|
exceeds: this.exceeds,
|
|
|
|
stddev: this.stddev,
|
|
|
|
percentiles: this.percentiles,
|
2021-01-30 17:26:15 +00:00
|
|
|
}, opts)}`;
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get min() {
|
2020-12-09 13:43:07 +00:00
|
|
|
return this[kHandle]?.min();
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get max() {
|
2020-12-09 13:43:07 +00:00
|
|
|
return this[kHandle]?.max();
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get mean() {
|
2020-12-09 13:43:07 +00:00
|
|
|
return this[kHandle]?.mean();
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get exceeds() {
|
2020-12-09 13:43:07 +00:00
|
|
|
return this[kHandle]?.exceeds();
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get stddev() {
|
2020-12-09 13:43:07 +00:00
|
|
|
return this[kHandle]?.stddev();
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
percentile(percentile) {
|
2021-01-19 11:23:27 +00:00
|
|
|
validateNumber(percentile, 'percentile');
|
2020-02-27 21:14:38 +00:00
|
|
|
|
2021-01-15 04:49:53 +00:00
|
|
|
if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100)
|
2020-02-27 21:14:38 +00:00
|
|
|
throw new ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile);
|
|
|
|
|
2020-12-09 13:43:07 +00:00
|
|
|
return this[kHandle]?.percentile(percentile);
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get percentiles() {
|
2021-01-30 17:26:15 +00:00
|
|
|
this[kMap].clear();
|
|
|
|
this[kHandle]?.percentiles(this[kMap]);
|
|
|
|
return this[kMap];
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
2020-12-09 13:43:07 +00:00
|
|
|
this[kHandle]?.reset();
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[kDestroy]() {
|
2020-12-09 13:43:07 +00:00
|
|
|
this[kHandle] = undefined;
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
2021-01-30 17:26:15 +00:00
|
|
|
|
|
|
|
[kClone]() {
|
|
|
|
const handle = this[kHandle];
|
|
|
|
return {
|
|
|
|
data: { handle },
|
|
|
|
deserializeInfo: 'internal/histogram:InternalHistogram'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[kDeserialize]({ handle }) {
|
|
|
|
this[kHandle] = handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RecordableHistogram extends Histogram {
|
|
|
|
constructor() {
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
throw new TypeError('illegal constructor');
|
|
|
|
}
|
|
|
|
|
|
|
|
record(val) {
|
|
|
|
if (typeof val === 'bigint') {
|
|
|
|
this[kHandle]?.record(val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NumberIsInteger(val))
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('val', ['integer', 'bigint'], val);
|
|
|
|
|
|
|
|
if (val < 1 || val > NumberMAX_SAFE_INTEGER)
|
|
|
|
throw new ERR_OUT_OF_RANGE('val', 'a safe integer greater than 0', val);
|
|
|
|
|
|
|
|
this[kHandle]?.record(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
recordDelta() {
|
|
|
|
this[kHandle]?.recordDelta();
|
|
|
|
}
|
|
|
|
|
|
|
|
[kClone]() {
|
|
|
|
const handle = this[kHandle];
|
|
|
|
return {
|
|
|
|
data: { handle },
|
|
|
|
deserializeInfo: 'internal/histogram:InternalRecordableHistogram'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class InternalHistogram extends JSTransferable {
|
|
|
|
constructor(handle) {
|
|
|
|
super();
|
|
|
|
this[kHandle] = handle;
|
|
|
|
this[kMap] = new SafeMap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class InternalRecordableHistogram extends JSTransferable {
|
|
|
|
constructor(handle) {
|
|
|
|
super();
|
|
|
|
this[kHandle] = handle;
|
|
|
|
this[kMap] = new SafeMap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InternalHistogram.prototype.constructor = Histogram;
|
|
|
|
ObjectSetPrototypeOf(
|
|
|
|
InternalHistogram.prototype,
|
|
|
|
Histogram.prototype);
|
|
|
|
|
|
|
|
InternalRecordableHistogram.prototype.constructor = RecordableHistogram;
|
|
|
|
ObjectSetPrototypeOf(
|
|
|
|
InternalRecordableHistogram.prototype,
|
|
|
|
RecordableHistogram.prototype);
|
|
|
|
|
|
|
|
function createHistogram() {
|
|
|
|
return new InternalRecordableHistogram(new _Histogram());
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Histogram,
|
2021-01-30 17:26:15 +00:00
|
|
|
RecordableHistogram,
|
|
|
|
InternalHistogram,
|
|
|
|
InternalRecordableHistogram,
|
2021-02-22 17:16:32 +00:00
|
|
|
isHistogram,
|
2020-02-27 21:14:38 +00:00
|
|
|
kDestroy,
|
|
|
|
kHandle,
|
2021-01-30 17:26:15 +00:00
|
|
|
createHistogram,
|
2020-02-27 21:14:38 +00:00
|
|
|
};
|