All files / lib/internal histogram.js

99.01% Statements 201/203
94.29% Branches 33/35
100% Functions 16/16
99.01% Lines 201/203

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 20436x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 12x 12x 12x 36x 36x 36x 3x 3x 3x 3x 36x 36x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 36x 36x 8x 8x 36x 36x 11x 11x 36x 36x 6x 6x 36x 36x 4x 4x 36x 36x 6x 6x 36x 36x 1001x 1001x 1001x 1001x 993x 1001x 1001x 36x 36x 3x 3x 3x 3x 36x 36x 2x 2x 36x 36x     36x 36x 1x 1x 1x 1x 1x 1x 36x 36x 2x 2x 36x 36x 36x 36x 1x 1x 36x 36x 28x 1x 1x 1x 27x 27x 28x 22x 28x 28x 21x 28x 28x 36x 36x 1x 1x 36x 36x 1x 1x 1x 1x 1x 1x 36x 36x 36x 36x 1x 1x 1x 1x 36x 36x 36x 36x 6x 6x 6x 6x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 5x 5x 5x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x  
'use strict';
 
const {
  NumberIsNaN,
  NumberIsInteger,
  NumberMAX_SAFE_INTEGER,
  ObjectSetPrototypeOf,
  SafeMap,
  Symbol,
} = primordials;
 
const {
  Histogram: _Histogram
} = internalBinding('performance');
 
const {
  customInspectSymbol: kInspect,
} = require('internal/util');
 
const { inspect } = require('util');
 
const {
  codes: {
    ERR_ILLEGAL_CONSTRUCTOR,
    ERR_INVALID_ARG_VALUE,
    ERR_INVALID_ARG_TYPE,
    ERR_OUT_OF_RANGE,
  },
} = require('internal/errors');
 
const {
  validateNumber,
} = require('internal/validators');
 
const kDestroy = Symbol('kDestroy');
const kHandle = Symbol('kHandle');
const kMap = Symbol('kMap');
 
const {
  kClone,
  kDeserialize,
  JSTransferable,
} = require('internal/worker/js_transferable');
 
function isHistogram(object) {
  return object?.[kHandle] !== undefined;
}
 
class Histogram extends JSTransferable {
  constructor(internal) {
    super();
    this[kHandle] = internal;
    this[kMap] = new SafeMap();
  }
 
  [kInspect](depth, options) {
    if (depth < 0)
      return this;
 
    const opts = {
      ...options,
      depth: options.depth == null ? null : options.depth - 1
    };
 
    return `Histogram ${inspect({
      min: this.min,
      max: this.max,
      mean: this.mean,
      exceeds: this.exceeds,
      stddev: this.stddev,
      percentiles: this.percentiles,
    }, opts)}`;
  }
 
  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) {
    validateNumber(percentile, 'percentile');
 
    if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100)
      throw new ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile);
 
    return this[kHandle]?.percentile(percentile);
  }
 
  get percentiles() {
    this[kMap].clear();
    this[kHandle]?.percentiles(this[kMap]);
    return this[kMap];
  }
 
  reset() {
    this[kHandle]?.reset();
  }
 
  [kDestroy]() {
    this[kHandle] = undefined;
  }
 
  [kClone]() {
    const handle = this[kHandle];
    return {
      data: { handle },
      deserializeInfo: 'internal/histogram:InternalHistogram'
    };
  }
 
  [kDeserialize]({ handle }) {
    this[kHandle] = handle;
  }
}
 
class RecordableHistogram extends Histogram {
  constructor() {
    throw new ERR_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());
}
 
module.exports = {
  Histogram,
  RecordableHistogram,
  InternalHistogram,
  InternalRecordableHistogram,
  isHistogram,
  kDestroy,
  kHandle,
  createHistogram,
};