• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const {
4  customInspectSymbol: kInspect,
5} = require('internal/util');
6
7const { format } = require('util');
8const { Map, Symbol } = primordials;
9
10const {
11  ERR_INVALID_ARG_TYPE,
12  ERR_INVALID_ARG_VALUE,
13} = require('internal/errors').codes;
14
15const kDestroy = Symbol('kDestroy');
16const kHandle = Symbol('kHandle');
17
18// Histograms are created internally by Node.js and used to
19// record various metrics. This Histogram class provides a
20// generally read-only view of the internal histogram.
21class Histogram {
22  #handle = undefined;
23  #map = new Map();
24
25  constructor(internal) {
26    this.#handle = internal;
27  }
28
29  [kInspect]() {
30    const obj = {
31      min: this.min,
32      max: this.max,
33      mean: this.mean,
34      exceeds: this.exceeds,
35      stddev: this.stddev,
36      percentiles: this.percentiles,
37    };
38    return `Histogram ${format(obj)}`;
39  }
40
41  get min() {
42    return this.#handle ? this.#handle.min() : undefined;
43  }
44
45  get max() {
46    return this.#handle ? this.#handle.max() : undefined;
47  }
48
49  get mean() {
50    return this.#handle ? this.#handle.mean() : undefined;
51  }
52
53  get exceeds() {
54    return this.#handle ? this.#handle.exceeds() : undefined;
55  }
56
57  get stddev() {
58    return this.#handle ? this.#handle.stddev() : undefined;
59  }
60
61  percentile(percentile) {
62    if (typeof percentile !== 'number')
63      throw new ERR_INVALID_ARG_TYPE('percentile', 'number', percentile);
64
65    if (percentile <= 0 || percentile > 100)
66      throw new ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile);
67
68    return this.#handle ? this.#handle.percentile(percentile) : undefined;
69  }
70
71  get percentiles() {
72    this.#map.clear();
73    if (this.#handle)
74      this.#handle.percentiles(this.#map);
75    return this.#map;
76  }
77
78  reset() {
79    if (this.#handle)
80      this.#handle.reset();
81  }
82
83  [kDestroy]() {
84    this.#handle = undefined;
85  }
86
87  get [kHandle]() { return this.#handle; }
88}
89
90module.exports = {
91  Histogram,
92  kDestroy,
93  kHandle,
94};
95