1'use strict';
2
3const {
4  ObjectDefineProperties,
5  ObjectSetPrototypeOf,
6} = primordials;
7
8const { PerformanceEntry } = require('internal/perf/performance_entry');
9
10const {
11  now,
12  getMilestoneTimestamp,
13} = require('internal/perf/utils');
14
15const {
16  customInspectSymbol: kInspect,
17} = require('internal/util');
18
19const { inspect } = require('util');
20
21const {
22  constants: {
23    NODE_PERFORMANCE_MILESTONE_NODE_START,
24    NODE_PERFORMANCE_MILESTONE_V8_START,
25    NODE_PERFORMANCE_MILESTONE_LOOP_START,
26    NODE_PERFORMANCE_MILESTONE_LOOP_EXIT,
27    NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE,
28    NODE_PERFORMANCE_MILESTONE_ENVIRONMENT,
29  },
30  loopIdleTime,
31} = internalBinding('performance');
32
33class PerformanceNodeTiming {
34  constructor() {
35    ObjectDefineProperties(this, {
36      name: {
37        __proto__: null,
38        enumerable: true,
39        configurable: true,
40        value: 'node',
41      },
42
43      entryType: {
44        __proto__: null,
45        enumerable: true,
46        configurable: true,
47        value: 'node',
48      },
49
50      startTime: {
51        __proto__: null,
52        enumerable: true,
53        configurable: true,
54        value: 0,
55      },
56
57      duration: {
58        __proto__: null,
59        enumerable: true,
60        configurable: true,
61        get: now,
62      },
63
64      nodeStart: {
65        __proto__: null,
66        enumerable: true,
67        configurable: true,
68        get() {
69          return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_NODE_START);
70        },
71      },
72
73      v8Start: {
74        __proto__: null,
75        enumerable: true,
76        configurable: true,
77        get() {
78          return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_V8_START);
79        },
80      },
81
82      environment: {
83        __proto__: null,
84        enumerable: true,
85        configurable: true,
86        get() {
87          return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_ENVIRONMENT);
88        },
89      },
90
91      loopStart: {
92        __proto__: null,
93        enumerable: true,
94        configurable: true,
95        get() {
96          return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_START);
97        },
98      },
99
100      loopExit: {
101        __proto__: null,
102        enumerable: true,
103        configurable: true,
104        get() {
105          return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
106        },
107      },
108
109      bootstrapComplete: {
110        __proto__: null,
111        enumerable: true,
112        configurable: true,
113        get() {
114          return getMilestoneTimestamp(
115            NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
116        },
117      },
118
119      idleTime: {
120        __proto__: null,
121        enumerable: true,
122        configurable: true,
123        get: loopIdleTime,
124      },
125    });
126  }
127
128  [kInspect](depth, options) {
129    if (depth < 0) return this;
130
131    const opts = {
132      ...options,
133      depth: options.depth == null ? null : options.depth - 1,
134    };
135
136    return `PerformanceNodeTiming ${inspect(this.toJSON(), opts)}`;
137  }
138
139  toJSON() {
140    return {
141      name: 'node',
142      entryType: 'node',
143      startTime: this.startTime,
144      duration: this.duration,
145      nodeStart: this.nodeStart,
146      v8Start: this.v8Start,
147      bootstrapComplete: this.bootstrapComplete,
148      environment: this.environment,
149      loopStart: this.loopStart,
150      loopExit: this.loopExit,
151      idleTime: this.idleTime,
152    };
153  }
154}
155
156ObjectSetPrototypeOf(
157  PerformanceNodeTiming.prototype,
158  PerformanceEntry.prototype);
159
160module.exports = new PerformanceNodeTiming();
161