node/lib/perf_hooks.js
Yash Ladha 93f0b4d35b
perf_hooks: add toJSON to performance class
Added toJSON method to the InternalPerformance class as per the
convention followed in other performance classes and per the spec:
https://www.w3.org/TR/hr-time/#tojson-method

Fixes: https://github.com/nodejs/node/issues/37623

PR-URL: https://github.com/nodejs/node/pull/37771
Fixes: https://github.com/nodejs/node/issues/37623
Reviewed-By: James M Snell <jasnell@gmail.com>
2021-04-30 13:28:27 -07:00

138 lines
2.8 KiB
JavaScript

'use strict';
const {
ObjectDefineProperties,
ObjectDefineProperty,
ObjectSetPrototypeOf,
TypeError,
} = primordials;
const {
timeOriginTimestamp,
constants,
} = internalBinding('performance');
const {
EventTarget,
} = require('internal/event_target');
const {
PerformanceEntry,
now,
} = require('internal/perf/perf');
const { PerformanceObserver } = require('internal/perf/observe');
const {
PerformanceMark,
mark,
measure,
clearMarks,
} = require('internal/perf/usertiming');
const {
createHistogram
} = require('internal/histogram');
const eventLoopUtilization = require('internal/perf/event_loop_utilization');
const monitorEventLoopDelay = require('internal/perf/event_loop_delay');
const nodeTiming = require('internal/perf/nodetiming');
const timerify = require('internal/perf/timerify');
const { customInspectSymbol: kInspect } = require('internal/util');
const { inspect } = require('util');
class Performance extends EventTarget {
constructor() {
// eslint-disable-next-line no-restricted-syntax
throw new TypeError('Illegal constructor');
}
[kInspect](depth, options) {
if (depth < 0) return this;
const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1
};
return `Performance ${inspect({
nodeTiming: this.nodeTiming,
timeOrigin: this.timeOrigin,
}, opts)}`;
}
}
function toJSON() {
return {
nodeTiming: this.nodeTiming,
timeOrigin: this.timeOrigin,
eventLoopUtilization: this.eventLoopUtilization()
};
}
class InternalPerformance extends EventTarget {}
InternalPerformance.prototype.constructor = Performance.prototype.constructor;
ObjectSetPrototypeOf(InternalPerformance.prototype, Performance.prototype);
ObjectDefineProperties(Performance.prototype, {
clearMarks: {
configurable: true,
enumerable: false,
value: clearMarks,
},
eventLoopUtilization: {
configurable: true,
enumerable: false,
value: eventLoopUtilization,
},
mark: {
configurable: true,
enumerable: false,
value: mark,
},
measure: {
configurable: true,
enumerable: false,
value: measure,
},
nodeTiming: {
configurable: true,
enumerable: false,
value: nodeTiming,
},
now: {
configurable: true,
enumerable: false,
value: now,
},
timerify: {
configurable: true,
enumerable: false,
value: timerify,
},
timeOrigin: {
configurable: true,
enumerable: true,
value: timeOriginTimestamp,
},
toJSON: {
configurable: true,
enumerable: true,
value: toJSON,
}
});
module.exports = {
PerformanceEntry,
PerformanceMark,
PerformanceObserver,
monitorEventLoopDelay,
createHistogram,
performance: new InternalPerformance(),
};
ObjectDefineProperty(module.exports, 'constants', {
configurable: false,
enumerable: true,
value: constants
});