Skip to content

Commit ed8af3a

Browse files
mmomtchevcodebytere
authored andcommitted
perf_hooks: make nodeTiming a first-class object
Render all properties of nodeTiming enumerable so JSON.stringify and Object.keys can access them Fixes: #35936 PR-URL: #35977 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent eefc6aa commit ed8af3a

File tree

2 files changed

+84
-33
lines changed

2 files changed

+84
-33
lines changed

lib/perf_hooks.js

+77-33
Original file line numberDiff line numberDiff line change
@@ -166,50 +166,94 @@ function getMilestoneTimestamp(milestoneIdx) {
166166
}
167167

168168
class PerformanceNodeTiming extends PerformanceEntry {
169-
get name() {
170-
return 'node';
171-
}
169+
constructor() {
170+
super();
172171

173-
get entryType() {
174-
return 'node';
175-
}
172+
ObjectDefineProperties(this, {
173+
name: {
174+
enumerable: true,
175+
configurable: true,
176+
value: 'node'
177+
},
176178

177-
get startTime() {
178-
return 0;
179-
}
179+
entryType: {
180+
enumerable: true,
181+
configurable: true,
182+
value: 'node'
183+
},
180184

181-
get duration() {
182-
return now() - timeOrigin;
183-
}
185+
startTime: {
186+
enumerable: true,
187+
configurable: true,
188+
value: 0
189+
},
184190

185-
get nodeStart() {
186-
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_NODE_START);
187-
}
191+
duration: {
192+
enumerable: true,
193+
configurable: true,
194+
get() {
195+
return now() - timeOrigin;
196+
}
197+
},
188198

189-
get v8Start() {
190-
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_V8_START);
191-
}
199+
nodeStart: {
200+
enumerable: true,
201+
configurable: true,
202+
get() {
203+
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_NODE_START);
204+
}
205+
},
192206

193-
get environment() {
194-
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_ENVIRONMENT);
195-
}
207+
v8Start: {
208+
enumerable: true,
209+
configurable: true,
210+
get() {
211+
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_V8_START);
212+
}
213+
},
196214

197-
get loopStart() {
198-
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_START);
199-
}
215+
environment: {
216+
enumerable: true,
217+
configurable: true,
218+
get() {
219+
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_ENVIRONMENT);
220+
}
221+
},
200222

201-
get loopExit() {
202-
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
203-
}
223+
loopStart: {
224+
enumerable: true,
225+
configurable: true,
226+
get() {
227+
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_START);
228+
}
229+
},
204230

205-
get bootstrapComplete() {
206-
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
207-
}
231+
loopExit: {
232+
enumerable: true,
233+
configurable: true,
234+
get() {
235+
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
236+
}
237+
},
208238

209-
get idleTime() {
210-
return loopIdleTime();
211-
}
239+
bootstrapComplete: {
240+
enumerable: true,
241+
configurable: true,
242+
get() {
243+
return getMilestoneTimestamp(
244+
NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
245+
}
246+
},
212247

248+
idleTime: {
249+
enumerable: true,
250+
configurable: true,
251+
get() {
252+
return loopIdleTime();
253+
}
254+
}
255+
});
256+
}
213257
[kInspect]() {
214258
return {
215259
name: 'node',

test/parallel/test-performance-eventlooputil.js

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ if (nodeTiming.loopStart === -1) {
2222
{ idle: 0, active: 0, utilization: 0 });
2323
}
2424

25+
const nodeTimingProps = ['name', 'entryType', 'startTime', 'duration',
26+
'nodeStart', 'v8Start', 'environment', 'loopStart',
27+
'loopExit', 'bootstrapComplete', 'idleTime'];
28+
for (const p of nodeTimingProps)
29+
assert.ok(typeof JSON.parse(JSON.stringify(nodeTiming))[p] ===
30+
typeof nodeTiming[p]);
31+
2532
setTimeout(mustCall(function r() {
2633
const elu1 = eventLoopUtilization();
2734

0 commit comments

Comments
 (0)