-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Migrate 'ts.performance' to use native performance hooks when available #40593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a197bee
Migrate 'ts.performance' to use native performance hooks when available
rbuckton dfa55ad
Write message instead of crashing when native perf API not found.
rbuckton c52e3b2
Add unit tests for PerformanceHooks shim
rbuckton 68806c6
Simplify tests for enablement in mark/measure, remove onProfilerEvent
rbuckton c5800d1
Remove shims, workaround for bug in peformance.measure
rbuckton 7b0d049
PR feedback
rbuckton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,121 @@ | ||
/*@internal*/ | ||
namespace ts { | ||
// The following definitions provide the minimum compatible support for the Web Performance User Timings API | ||
// between browsers and NodeJS: | ||
|
||
export interface PerformanceHooks { | ||
performance: Performance; | ||
PerformanceObserver: PerformanceObserverConstructor; | ||
} | ||
|
||
export interface Performance { | ||
mark(name: string): void; | ||
measure(name: string, startMark?: string, endMark?: string): void; | ||
now(): number; | ||
timeOrigin: number; | ||
} | ||
|
||
export interface PerformanceEntry { | ||
name: string; | ||
entryType: string; | ||
startTime: number; | ||
duration: number; | ||
} | ||
|
||
export interface PerformanceObserverEntryList { | ||
getEntries(): PerformanceEntryList; | ||
getEntriesByName(name: string, type?: string): PerformanceEntryList; | ||
getEntriesByType(type: string): PerformanceEntryList; | ||
} | ||
|
||
export interface PerformanceObserver { | ||
disconnect(): void; | ||
observe(options: { entryTypes: readonly string[] }): void; | ||
} | ||
|
||
export type PerformanceObserverConstructor = new (callback: (list: PerformanceObserverEntryList, observer: PerformanceObserver) => void) => PerformanceObserver; | ||
export type PerformanceEntryList = PerformanceEntry[]; | ||
|
||
// Browser globals for the Web Performance User Timings API | ||
declare const performance: Performance | undefined; | ||
declare const PerformanceObserver: PerformanceObserverConstructor | undefined; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
function hasRequiredAPI(performance: Performance | undefined, PerformanceObserver: PerformanceObserverConstructor | undefined) { | ||
return typeof performance === "object" && | ||
typeof performance.timeOrigin === "number" && | ||
typeof performance.mark === "function" && | ||
typeof performance.measure === "function" && | ||
typeof performance.now === "function" && | ||
typeof PerformanceObserver === "function"; | ||
} | ||
|
||
function tryGetWebPerformanceHooks(): PerformanceHooks | undefined { | ||
if (typeof performance === "object" && | ||
typeof PerformanceObserver === "function" && | ||
hasRequiredAPI(performance, PerformanceObserver)) { | ||
return { | ||
performance, | ||
PerformanceObserver | ||
}; | ||
} | ||
} | ||
|
||
function tryGetNodePerformanceHooks(): PerformanceHooks | undefined { | ||
if (typeof module === "object" && typeof require === "function") { | ||
try { | ||
const { performance, PerformanceObserver } = require("perf_hooks") as typeof import("perf_hooks"); | ||
if (hasRequiredAPI(performance, PerformanceObserver)) { | ||
// There is a bug in Node's performance.measure prior to 12.16.3/13.13.0 that does not | ||
// match the Web Performance API specification. Node's implementation did not allow | ||
// optional `start` and `end` arguments for `performance.measure`. | ||
// See https://github.com/nodejs/node/pull/32651 for more information. | ||
const version = new Version(process.versions.node); | ||
const range = new VersionRange("<12 || 13 <13.13"); | ||
if (range.test(version)) { | ||
return { | ||
performance: { | ||
get timeOrigin() { return performance.timeOrigin; }, | ||
now() { return performance.now(); }, | ||
mark(name) { return performance.mark(name); }, | ||
measure(name, start = "nodeStart", end?) { | ||
if (end === undefined) { | ||
end = "__performance.measure-fix__"; | ||
performance.mark(end); | ||
} | ||
performance.measure(name, start, end); | ||
if (end = "__performance.measure-fix__") { | ||
performance.clearMarks("__performance.measure-fix__"); | ||
} | ||
} | ||
}, | ||
PerformanceObserver | ||
}; | ||
} | ||
return { | ||
performance, | ||
PerformanceObserver | ||
}; | ||
} | ||
} | ||
catch { | ||
// ignore errors | ||
} | ||
} | ||
} | ||
|
||
// Unlike with the native Map/Set 'tryGet' functions in corePublic.ts, we eagerly evaluate these | ||
// since we will need them for `timestamp`, below. | ||
const nativePerformanceHooks = tryGetWebPerformanceHooks() || tryGetNodePerformanceHooks(); | ||
const nativePerformance = nativePerformanceHooks?.performance; | ||
|
||
export function tryGetNativePerformanceHooks() { | ||
return nativePerformanceHooks; | ||
} | ||
|
||
/** Gets a timestamp with (at least) ms resolution */ | ||
export const timestamp = | ||
nativePerformance ? () => nativePerformance.now() : | ||
Date.now ? Date.now : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that I copied this in my earlier comment, so just in case: didn't you say that |
||
() => +(new Date()); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -4,6 +4,6 @@ | |
"outFile": "../../built/local/shims.js" | ||
}, | ||
"files": [ | ||
"collectionShims.ts" | ||
"collectionShims.ts", | ||
] | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.