2020-02-27 21:14:38 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const {
|
|
|
|
customInspectSymbol: kInspect,
|
|
|
|
} = require('internal/util');
|
|
|
|
|
|
|
|
const { format } = require('util');
|
2021-01-15 04:49:53 +00:00
|
|
|
const { NumberIsNaN, SafeMap, Symbol } = primordials;
|
2020-02-27 21:14:38 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_INVALID_ARG_VALUE,
|
|
|
|
} = require('internal/errors').codes;
|
|
|
|
|
|
|
|
const kDestroy = Symbol('kDestroy');
|
|
|
|
const kHandle = Symbol('kHandle');
|
|
|
|
|
|
|
|
// Histograms are created internally by Node.js and used to
|
|
|
|
// record various metrics. This Histogram class provides a
|
|
|
|
// generally read-only view of the internal histogram.
|
|
|
|
class Histogram {
|
2020-12-09 13:43:07 +00:00
|
|
|
#map = new SafeMap();
|
2020-02-27 21:14:38 +00:00
|
|
|
|
|
|
|
constructor(internal) {
|
2020-12-09 13:43:07 +00:00
|
|
|
this[kHandle] = internal;
|
2020-02-27 21:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[kInspect]() {
|
|
|
|
const obj = {
|
|
|
|
min: this.min,
|
|
|
|
max: this.max,
|
|
|
|
mean: this.mean,
|
|
|
|
exceeds: this.exceeds,
|
|
|
|
stddev: this.stddev,
|
|
|
|
percentiles: this.percentiles,
|
|
|
|
};
|
|
|
|
return `Histogram ${format(obj)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (typeof percentile !== 'number')
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('percentile', 'number', percentile);
|
|
|
|
|
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() {
|
|
|
|
this.#map.clear();
|
2020-12-09 13:43:07 +00:00
|
|
|
this[kHandle]?.percentiles(this.#map);
|
2020-02-27 21:14:38 +00:00
|
|
|
return this.#map;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Histogram,
|
|
|
|
kDestroy,
|
|
|
|
kHandle,
|
|
|
|
};
|