|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + ObjectDefineProperty, |
| 5 | + ObjectDefineProperties, |
| 6 | + ObjectSetPrototypeOf, |
| 7 | + TypeError, |
| 8 | +} = primordials; |
| 9 | + |
| 10 | +const { |
| 11 | + EventTarget, |
| 12 | +} = require('internal/event_target'); |
| 13 | + |
| 14 | +const { now } = require('internal/perf/utils'); |
| 15 | + |
| 16 | +const { |
| 17 | + mark, |
| 18 | + measure, |
| 19 | + clearMarks, |
| 20 | +} = require('internal/perf/usertiming'); |
| 21 | + |
| 22 | +const eventLoopUtilization = require('internal/perf/event_loop_utilization'); |
| 23 | +const nodeTiming = require('internal/perf/nodetiming'); |
| 24 | +const timerify = require('internal/perf/timerify'); |
| 25 | +const { customInspectSymbol: kInspect } = require('internal/util'); |
| 26 | +const { inspect } = require('util'); |
| 27 | + |
| 28 | +const { |
| 29 | + getTimeOriginTimestamp |
| 30 | +} = internalBinding('performance'); |
| 31 | + |
| 32 | +class Performance extends EventTarget { |
| 33 | + constructor() { |
| 34 | + // eslint-disable-next-line no-restricted-syntax |
| 35 | + throw new TypeError('Illegal constructor'); |
| 36 | + } |
| 37 | + |
| 38 | + [kInspect](depth, options) { |
| 39 | + if (depth < 0) return this; |
| 40 | + |
| 41 | + const opts = { |
| 42 | + ...options, |
| 43 | + depth: options.depth == null ? null : options.depth - 1 |
| 44 | + }; |
| 45 | + |
| 46 | + return `Performance ${inspect({ |
| 47 | + nodeTiming: this.nodeTiming, |
| 48 | + timeOrigin: this.timeOrigin, |
| 49 | + }, opts)}`; |
| 50 | + } |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +function toJSON() { |
| 55 | + return { |
| 56 | + nodeTiming: this.nodeTiming, |
| 57 | + timeOrigin: this.timeOrigin, |
| 58 | + eventLoopUtilization: this.eventLoopUtilization() |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +class InternalPerformance extends EventTarget {} |
| 63 | +InternalPerformance.prototype.constructor = Performance.prototype.constructor; |
| 64 | +ObjectSetPrototypeOf(InternalPerformance.prototype, Performance.prototype); |
| 65 | + |
| 66 | +ObjectDefineProperties(Performance.prototype, { |
| 67 | + clearMarks: { |
| 68 | + configurable: true, |
| 69 | + enumerable: false, |
| 70 | + value: clearMarks, |
| 71 | + }, |
| 72 | + eventLoopUtilization: { |
| 73 | + configurable: true, |
| 74 | + enumerable: false, |
| 75 | + value: eventLoopUtilization, |
| 76 | + }, |
| 77 | + mark: { |
| 78 | + configurable: true, |
| 79 | + enumerable: false, |
| 80 | + value: mark, |
| 81 | + }, |
| 82 | + measure: { |
| 83 | + configurable: true, |
| 84 | + enumerable: false, |
| 85 | + value: measure, |
| 86 | + }, |
| 87 | + nodeTiming: { |
| 88 | + configurable: true, |
| 89 | + enumerable: false, |
| 90 | + value: nodeTiming, |
| 91 | + }, |
| 92 | + now: { |
| 93 | + configurable: true, |
| 94 | + enumerable: false, |
| 95 | + value: now, |
| 96 | + }, |
| 97 | + timerify: { |
| 98 | + configurable: true, |
| 99 | + enumerable: false, |
| 100 | + value: timerify, |
| 101 | + }, |
| 102 | + // This would be updated during pre-execution in case |
| 103 | + // the process is launched from a snapshot. |
| 104 | + // TODO(joyeecheung): we may want to warn about access to |
| 105 | + // this during snapshot building. |
| 106 | + timeOrigin: { |
| 107 | + configurable: true, |
| 108 | + enumerable: true, |
| 109 | + value: getTimeOriginTimestamp(), |
| 110 | + }, |
| 111 | + toJSON: { |
| 112 | + configurable: true, |
| 113 | + enumerable: true, |
| 114 | + value: toJSON, |
| 115 | + } |
| 116 | +}); |
| 117 | + |
| 118 | +function refreshTimeOrigin() { |
| 119 | + ObjectDefineProperty(Performance.prototype, 'timeOrigin', { |
| 120 | + configurable: true, |
| 121 | + enumerable: true, |
| 122 | + value: getTimeOriginTimestamp(), |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +module.exports = { |
| 127 | + InternalPerformance, |
| 128 | + refreshTimeOrigin |
| 129 | +}; |
0 commit comments