• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const {
3  ReflectConstruct,
4  SafeMap,
5  Symbol,
6} = primordials;
7
8const {
9  codes: {
10    ERR_ILLEGAL_CONSTRUCTOR,
11    ERR_INVALID_THIS,
12  },
13} = require('internal/errors');
14
15const {
16  createELDHistogram,
17} = internalBinding('performance');
18
19const {
20  validateInteger,
21  validateObject,
22} = require('internal/validators');
23
24const {
25  Histogram,
26  kHandle,
27  kMap,
28} = require('internal/histogram');
29
30const {
31  kEmptyObject,
32} = require('internal/util');
33
34const {
35  makeTransferable,
36} = require('internal/worker/js_transferable');
37
38const kEnabled = Symbol('kEnabled');
39
40class ELDHistogram extends Histogram {
41  constructor(i) {
42    throw new ERR_ILLEGAL_CONSTRUCTOR();
43  }
44
45  /**
46   * @returns {boolean}
47   */
48  enable() {
49    if (this[kEnabled] === undefined)
50      throw new ERR_INVALID_THIS('ELDHistogram');
51    if (this[kEnabled]) return false;
52    this[kEnabled] = true;
53    this[kHandle].start();
54    return true;
55  }
56
57  /**
58   * @returns {boolean}
59   */
60  disable() {
61    if (this[kEnabled] === undefined)
62      throw new ERR_INVALID_THIS('ELDHistogram');
63    if (!this[kEnabled]) return false;
64    this[kEnabled] = false;
65    this[kHandle].stop();
66    return true;
67  }
68}
69
70/**
71 * @param {{
72 *   resolution : number
73 * }} [options]
74 * @returns {ELDHistogram}
75 */
76function monitorEventLoopDelay(options = kEmptyObject) {
77  validateObject(options, 'options');
78
79  const { resolution = 10 } = options;
80  validateInteger(resolution, 'options.resolution', 1);
81
82  return makeTransferable(ReflectConstruct(
83    function() {
84      this[kEnabled] = false;
85      this[kHandle] = createELDHistogram(resolution);
86      this[kMap] = new SafeMap();
87    }, [], ELDHistogram));
88}
89
90module.exports = monitorEventLoopDelay;
91