-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf_hooks: refactor perf_hooks for snapshot building
- Move Performance and InternalPerformance to a new lib/internal/perf/performance.js - Move now() getMilestoneTimestamp() into lib/internal/perf/utils.js - Rename lib/internal/perf/perf.js to lib/internal/perf/performance_entry.js - Refresh time origin at startup (this means the time origins could differ between snapshot building time and snapshot creation time) PR-URL: #38971 Refs: #35711 Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
0e55cb7
commit 7ea98fb
Showing
15 changed files
with
224 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
'use strict'; | ||
|
||
const { | ||
ObjectDefineProperty, | ||
ObjectDefineProperties, | ||
ObjectSetPrototypeOf, | ||
TypeError, | ||
} = primordials; | ||
|
||
const { | ||
EventTarget, | ||
} = require('internal/event_target'); | ||
|
||
const { now } = require('internal/perf/utils'); | ||
|
||
const { | ||
mark, | ||
measure, | ||
clearMarks, | ||
} = require('internal/perf/usertiming'); | ||
|
||
const eventLoopUtilization = require('internal/perf/event_loop_utilization'); | ||
const nodeTiming = require('internal/perf/nodetiming'); | ||
const timerify = require('internal/perf/timerify'); | ||
const { customInspectSymbol: kInspect } = require('internal/util'); | ||
const { inspect } = require('util'); | ||
|
||
const { | ||
getTimeOriginTimestamp | ||
} = internalBinding('performance'); | ||
|
||
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, | ||
}, | ||
// This would be updated during pre-execution in case | ||
// the process is launched from a snapshot. | ||
// TODO(joyeecheung): we may want to warn about access to | ||
// this during snapshot building. | ||
timeOrigin: { | ||
configurable: true, | ||
enumerable: true, | ||
value: getTimeOriginTimestamp(), | ||
}, | ||
toJSON: { | ||
configurable: true, | ||
enumerable: true, | ||
value: toJSON, | ||
} | ||
}); | ||
|
||
function refreshTimeOrigin() { | ||
ObjectDefineProperty(Performance.prototype, 'timeOrigin', { | ||
configurable: true, | ||
enumerable: true, | ||
value: getTimeOriginTimestamp(), | ||
}); | ||
} | ||
|
||
module.exports = { | ||
InternalPerformance, | ||
refreshTimeOrigin | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
const binding = internalBinding('performance'); | ||
const { | ||
milestones, | ||
getTimeOrigin, | ||
} = binding; | ||
|
||
// TODO(joyeecheung): we may want to warn about access to | ||
// this during snapshot building. | ||
let timeOrigin = getTimeOrigin(); | ||
|
||
function now() { | ||
const hr = process.hrtime(); | ||
return (hr[0] * 1000 + hr[1] / 1e6) - timeOrigin; | ||
} | ||
|
||
function getMilestoneTimestamp(milestoneIdx) { | ||
const ns = milestones[milestoneIdx]; | ||
if (ns === -1) | ||
return ns; | ||
return ns / 1e6 - timeOrigin; | ||
} | ||
|
||
function refreshTimeOrigin() { | ||
timeOrigin = getTimeOrigin(); | ||
} | ||
|
||
module.exports = { | ||
now, | ||
getMilestoneTimestamp, | ||
refreshTimeOrigin | ||
}; |
Oops, something went wrong.