From e0b8f007f37b828d725f2ab3a12ac00006a23617 Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Thu, 5 Nov 2020 14:11:44 +0100 Subject: [PATCH 1/5] perf_hooks: make nodeTiming a first-class object Render all properties of nodeTiming enumerable so JSON.stringify and Object.keys can access them Fixes: https://github.com/nodejs/node/issues/35936 --- lib/perf_hooks.js | 105 ++++++++++++------ .../test-performance-eventlooputil.js | 6 + 2 files changed, 78 insertions(+), 33 deletions(-) diff --git a/lib/perf_hooks.js b/lib/perf_hooks.js index 3fd7e7eded0352..69e2ab0c8a5e08 100644 --- a/lib/perf_hooks.js +++ b/lib/perf_hooks.js @@ -166,50 +166,89 @@ function getMilestoneTimestamp(milestoneIdx) { } class PerformanceNodeTiming extends PerformanceEntry { - get name() { - return 'node'; - } + constructor() { + super(); - get entryType() { - return 'node'; - } + ObjectDefineProperties(this, { + name: { + enumerable: true, + get() { + return 'node'; + } + }, - get startTime() { - return 0; - } + entryType: { + enumerable: true, + get() { + return 'node'; + } + }, - get duration() { - return now() - timeOrigin; - } + startTime: { + enumerable: true, + get() { + return 0; + } + }, - get nodeStart() { - return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_NODE_START); - } + duration: { + enumerable: true, + get() { + return now() - timeOrigin; + } + }, - get v8Start() { - return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_V8_START); - } + nodeStart: { + enumerable: true, + get() { + return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_NODE_START); + } + }, - get environment() { - return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_ENVIRONMENT); - } + v8Start: { + enumerable: true, + get() { + return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_V8_START); + } + }, - get loopStart() { - return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_START); - } + environment: { + enumerable: true, + get() { + return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_ENVIRONMENT); + } + }, - get loopExit() { - return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_EXIT); - } + loopStart: { + enumerable: true, + get() { + return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_START); + } + }, - get bootstrapComplete() { - return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE); - } + loopExit: { + enumerable: true, + get() { + return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_EXIT); + } + }, - get idleTime() { - return loopIdleTime(); - } + bootstrapComplete: { + enumerable: true, + get() { + return getMilestoneTimestamp( + NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE); + } + }, + idleTime: { + enumerable: true, + get() { + return loopIdleTime(); + } + } + }); + } [kInspect]() { return { name: 'node', diff --git a/test/parallel/test-performance-eventlooputil.js b/test/parallel/test-performance-eventlooputil.js index 5d039209dadde7..3d5749b588e1ac 100644 --- a/test/parallel/test-performance-eventlooputil.js +++ b/test/parallel/test-performance-eventlooputil.js @@ -22,6 +22,12 @@ if (nodeTiming.loopStart === -1) { { idle: 0, active: 0, utilization: 0 }); } +for (const p of ['name', 'entryType', 'startTime', 'duration', + 'nodeStart', 'v8Start', 'environment', 'loopStart', 'loopExit', + 'bootstrapComplete', 'idleTime']) + assert.ok(typeof JSON.parse(JSON.stringify(nodeTiming))[p] === + typeof nodeTiming[p]); + setTimeout(mustCall(function r() { const t = Date.now(); const elu1 = eventLoopUtilization(); From 7d64d79045c27f56192745d379d278b0bac95e21 Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Thu, 5 Nov 2020 18:53:57 +0100 Subject: [PATCH 2/5] perf-hooks: Make nodeTiming a first-class object Improve readability as per @aduh95 suggestion Refs: https://github.com/nodejs/node/pull/35977 --- test/parallel/test-performance-eventlooputil.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-performance-eventlooputil.js b/test/parallel/test-performance-eventlooputil.js index 3d5749b588e1ac..58643dea901ea8 100644 --- a/test/parallel/test-performance-eventlooputil.js +++ b/test/parallel/test-performance-eventlooputil.js @@ -22,9 +22,10 @@ if (nodeTiming.loopStart === -1) { { idle: 0, active: 0, utilization: 0 }); } -for (const p of ['name', 'entryType', 'startTime', 'duration', - 'nodeStart', 'v8Start', 'environment', 'loopStart', 'loopExit', - 'bootstrapComplete', 'idleTime']) +const nodeTimingProps = ['name', 'entryType', 'startTime', 'duration', + 'nodeStart', 'v8Start', 'environment', 'loopStart', + 'loopExit', 'bootstrapComplete', 'idleTime']; +for (const p of nodeTimingProps) assert.ok(typeof JSON.parse(JSON.stringify(nodeTiming))[p] === typeof nodeTiming[p]); From 170242b2ded895a856091831d8f773c5afd16982 Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Fri, 6 Nov 2020 12:20:59 +0100 Subject: [PATCH 3/5] perf_hooks: make nodeTiming a first-class object Transform the static values to plain properties (@addaleax review) Refs: https://github.com/nodejs/node/pull/35977 --- lib/perf_hooks.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/perf_hooks.js b/lib/perf_hooks.js index 69e2ab0c8a5e08..09fb49c461c4fe 100644 --- a/lib/perf_hooks.js +++ b/lib/perf_hooks.js @@ -172,23 +172,17 @@ class PerformanceNodeTiming extends PerformanceEntry { ObjectDefineProperties(this, { name: { enumerable: true, - get() { - return 'node'; - } + value: 'node' }, entryType: { enumerable: true, - get() { - return 'node'; - } + value: 'node' }, startTime: { enumerable: true, - get() { - return 0; - } + value: 0 }, duration: { From 166f6b3b4c47933b13ac5fea8d49c1bcc5a3d922 Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Fri, 6 Nov 2020 12:25:38 +0100 Subject: [PATCH 4/5] perf_hooks: make nodeTiming a first-class object Render all properties configurable (@addaleax review) https://github.com/nodejs/node/pull/35977 --- lib/perf_hooks.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/perf_hooks.js b/lib/perf_hooks.js index 09fb49c461c4fe..86239cedf451dc 100644 --- a/lib/perf_hooks.js +++ b/lib/perf_hooks.js @@ -165,70 +165,74 @@ function getMilestoneTimestamp(milestoneIdx) { return ns / 1e6 - timeOrigin; } +const PerformanceNodeTimingProps = { + enumerable: true, + configurable: true +}; class PerformanceNodeTiming extends PerformanceEntry { constructor() { super(); ObjectDefineProperties(this, { name: { - enumerable: true, + ...PerformanceNodeTimingProps, value: 'node' }, entryType: { - enumerable: true, + ...PerformanceNodeTimingProps, value: 'node' }, startTime: { - enumerable: true, + ...PerformanceNodeTimingProps, value: 0 }, duration: { - enumerable: true, + ...PerformanceNodeTimingProps, get() { return now() - timeOrigin; } }, nodeStart: { - enumerable: true, + ...PerformanceNodeTimingProps, get() { return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_NODE_START); } }, v8Start: { - enumerable: true, + ...PerformanceNodeTimingProps, get() { return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_V8_START); } }, environment: { - enumerable: true, + ...PerformanceNodeTimingProps, get() { return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_ENVIRONMENT); } }, loopStart: { - enumerable: true, + ...PerformanceNodeTimingProps, get() { return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_START); } }, loopExit: { - enumerable: true, + ...PerformanceNodeTimingProps, get() { return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_EXIT); } }, bootstrapComplete: { - enumerable: true, + ...PerformanceNodeTimingProps, get() { return getMilestoneTimestamp( NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE); @@ -236,7 +240,7 @@ class PerformanceNodeTiming extends PerformanceEntry { }, idleTime: { - enumerable: true, + ...PerformanceNodeTimingProps, get() { return loopIdleTime(); } From 778f60b7626f0a715ae4071cdb944165251028e9 Mon Sep 17 00:00:00 2001 From: Momtchil Momtchev Date: Sat, 7 Nov 2020 00:33:32 +0100 Subject: [PATCH 5/5] perf_hooks: make nodeTiming a first-class object Render all properties of nodeTiming enumerable so JSON.stringify and Object.keys can access them Refs: https://github.com/nodejs/node/pull/35977 --- lib/perf_hooks.js | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/perf_hooks.js b/lib/perf_hooks.js index 86239cedf451dc..a2a4cb4f7fa8a4 100644 --- a/lib/perf_hooks.js +++ b/lib/perf_hooks.js @@ -165,74 +165,80 @@ function getMilestoneTimestamp(milestoneIdx) { return ns / 1e6 - timeOrigin; } -const PerformanceNodeTimingProps = { - enumerable: true, - configurable: true -}; class PerformanceNodeTiming extends PerformanceEntry { constructor() { super(); ObjectDefineProperties(this, { name: { - ...PerformanceNodeTimingProps, + enumerable: true, + configurable: true, value: 'node' }, entryType: { - ...PerformanceNodeTimingProps, + enumerable: true, + configurable: true, value: 'node' }, startTime: { - ...PerformanceNodeTimingProps, + enumerable: true, + configurable: true, value: 0 }, duration: { - ...PerformanceNodeTimingProps, + enumerable: true, + configurable: true, get() { return now() - timeOrigin; } }, nodeStart: { - ...PerformanceNodeTimingProps, + enumerable: true, + configurable: true, get() { return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_NODE_START); } }, v8Start: { - ...PerformanceNodeTimingProps, + enumerable: true, + configurable: true, get() { return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_V8_START); } }, environment: { - ...PerformanceNodeTimingProps, + enumerable: true, + configurable: true, get() { return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_ENVIRONMENT); } }, loopStart: { - ...PerformanceNodeTimingProps, + enumerable: true, + configurable: true, get() { return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_START); } }, loopExit: { - ...PerformanceNodeTimingProps, + enumerable: true, + configurable: true, get() { return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_EXIT); } }, bootstrapComplete: { - ...PerformanceNodeTimingProps, + enumerable: true, + configurable: true, get() { return getMilestoneTimestamp( NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE); @@ -240,7 +246,8 @@ class PerformanceNodeTiming extends PerformanceEntry { }, idleTime: { - ...PerformanceNodeTimingProps, + enumerable: true, + configurable: true, get() { return loopIdleTime(); }