/
histogram.js
89 lines (70 loc) Β· 1.68 KB
/
histogram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict';
const {
customInspectSymbol: kInspect,
} = require('internal/util');
const { format } = require('util');
const { SafeMap, Symbol } = primordials;
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 {
#map = new SafeMap();
constructor(internal) {
this[kHandle] = internal;
}
[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() {
return this[kHandle]?.min();
}
get max() {
return this[kHandle]?.max();
}
get mean() {
return this[kHandle]?.mean();
}
get exceeds() {
return this[kHandle]?.exceeds();
}
get stddev() {
return this[kHandle]?.stddev();
}
percentile(percentile) {
if (typeof percentile !== 'number')
throw new ERR_INVALID_ARG_TYPE('percentile', 'number', percentile);
if (percentile <= 0 || percentile > 100)
throw new ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile);
return this[kHandle]?.percentile(percentile);
}
get percentiles() {
this.#map.clear();
this[kHandle]?.percentiles(this.#map);
return this.#map;
}
reset() {
this[kHandle]?.reset();
}
[kDestroy]() {
this[kHandle] = undefined;
}
}
module.exports = {
Histogram,
kDestroy,
kHandle,
};