-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Integrate feedback from @mihailik to performance framework #9845
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
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,109 @@ | ||
/*@internal*/ | ||
namespace ts { | ||
declare const performance: { now?(): number } | undefined; | ||
/** Gets a timestamp with (at least) ms resolution */ | ||
export const timestamp = typeof performance !== "undefined" && performance.now ? performance.now : Date.now ? Date.now : () => +(new Date()); | ||
} | ||
|
||
/*@internal*/ | ||
namespace ts.performance { | ||
/** Performance measurements for the compiler. */ | ||
export namespace performance { | ||
declare const onProfilerEvent: { (markName: string): void; profiler: boolean; }; | ||
declare const performance: { now?(): number } | undefined; | ||
let profilerEvent: (markName: string) => void; | ||
let markInternal: () => number; | ||
let counters: Map<number>; | ||
let measures: Map<number>; | ||
declare const onProfilerEvent: { (markName: string): void; profiler: boolean; }; | ||
let profilerEvent: (markName: string) => void; | ||
let counters: Map<number>; | ||
let measures: Map<number>; | ||
|
||
/** | ||
* Emit a performance event if ts-profiler is connected. This is primarily used | ||
* to generate heap snapshots. | ||
* | ||
* @param eventName A name for the event. | ||
*/ | ||
export function emit(eventName: string) { | ||
if (profilerEvent) { | ||
profilerEvent(eventName); | ||
} | ||
/** | ||
* Emit a performance event if ts-profiler is connected. This is primarily used | ||
* to generate heap snapshots. | ||
* | ||
* @param eventName A name for the event. | ||
*/ | ||
export function emit(eventName: string) { | ||
if (profilerEvent) { | ||
profilerEvent(eventName); | ||
} | ||
} | ||
|
||
/** | ||
* Increments a counter with the specified name. | ||
* | ||
* @param counterName The name of the counter. | ||
*/ | ||
export function increment(counterName: string) { | ||
if (counters) { | ||
counters[counterName] = (getProperty(counters, counterName) || 0) + 1; | ||
} | ||
/** | ||
* Increments a counter with the specified name. | ||
* | ||
* @param counterName The name of the counter. | ||
*/ | ||
export function increment(counterName: string) { | ||
if (counters) { | ||
counters[counterName] = (getProperty(counters, counterName) || 0) + 1; | ||
} | ||
} | ||
|
||
/** | ||
* Gets the value of the counter with the specified name. | ||
* | ||
* @param counterName The name of the counter. | ||
*/ | ||
export function getCount(counterName: string) { | ||
return counters && getProperty(counters, counterName) || 0; | ||
} | ||
/** | ||
* Gets the value of the counter with the specified name. | ||
* | ||
* @param counterName The name of the counter. | ||
*/ | ||
export function getCount(counterName: string) { | ||
return counters && getProperty(counters, counterName) || 0; | ||
} | ||
|
||
/** | ||
* Marks the start of a performance measurement. | ||
*/ | ||
export function mark() { | ||
return measures ? markInternal() : 0; | ||
} | ||
/** | ||
* Marks the start of a performance measurement. | ||
*/ | ||
export function mark() { | ||
return measures ? timestamp() : 0; | ||
} | ||
|
||
/** | ||
* Adds a performance measurement with the specified name. | ||
* | ||
* @param measureName The name of the performance measurement. | ||
* @param marker The timestamp of the starting mark. | ||
*/ | ||
export function measure(measureName: string, marker: number) { | ||
if (measures) { | ||
measures[measureName] = (getProperty(measures, measureName) || 0) + (Date.now() - marker); | ||
} | ||
/** | ||
* Adds a performance measurement with the specified name. | ||
* | ||
* @param measureName The name of the performance measurement. | ||
* @param marker The timestamp of the starting mark. | ||
*/ | ||
export function measure(measureName: string, marker: number) { | ||
if (measures) { | ||
measures[measureName] = (getProperty(measures, measureName) || 0) + (timestamp() - marker); | ||
} | ||
} | ||
|
||
/** | ||
* Iterate over each measure, performing some action | ||
* | ||
* @param cb The action to perform for each measure | ||
*/ | ||
export function forEachMeasure(cb: (measureName: string, duration: number) => void) { | ||
return forEachKey(measures, key => cb(key, measures[key])); | ||
} | ||
/** | ||
* Iterate over each measure, performing some action | ||
* | ||
* @param cb The action to perform for each measure | ||
*/ | ||
export function forEachMeasure(cb: (measureName: string, duration: number) => void) { | ||
return forEachKey(measures, key => cb(key, measures[key])); | ||
} | ||
|
||
/** | ||
* Gets the total duration of all measurements with the supplied name. | ||
* | ||
* @param measureName The name of the measure whose durations should be accumulated. | ||
*/ | ||
export function getDuration(measureName: string) { | ||
return measures && getProperty(measures, measureName) || 0; | ||
} | ||
/** | ||
* Gets the total duration of all measurements with the supplied name. | ||
* | ||
* @param measureName The name of the measure whose durations should be accumulated. | ||
*/ | ||
export function getDuration(measureName: string) { | ||
return measures && getProperty(measures, measureName) || 0; | ||
} | ||
|
||
/** Enables (and resets) performance measurements for the compiler. */ | ||
export function enable() { | ||
counters = { }; | ||
measures = { | ||
"I/O Read": 0, | ||
"I/O Write": 0, | ||
"Program": 0, | ||
"Parse": 0, | ||
"Bind": 0, | ||
"Check": 0, | ||
"Emit": 0, | ||
}; | ||
/** Enables (and resets) performance measurements for the compiler. */ | ||
export function enable() { | ||
counters = { }; | ||
measures = { | ||
"I/O Read": 0, | ||
"I/O Write": 0, | ||
"Program": 0, | ||
"Parse": 0, | ||
"Bind": 0, | ||
"Check": 0, | ||
"Emit": 0, | ||
}; | ||
|
||
profilerEvent = typeof onProfilerEvent === "function" && onProfilerEvent.profiler === true | ||
? onProfilerEvent | ||
: undefined; | ||
markInternal = performance && performance.now ? performance.now : Date.now ? Date.now : () => new Date().getTime(); | ||
} | ||
profilerEvent = typeof onProfilerEvent === "function" && onProfilerEvent.profiler === true | ||
? onProfilerEvent | ||
: undefined; | ||
} | ||
|
||
/** Disables (and clears) performance measurements for the compiler. */ | ||
export function disable() { | ||
counters = undefined; | ||
measures = undefined; | ||
profilerEvent = undefined; | ||
} | ||
/** Disables (and clears) performance measurements for the compiler. */ | ||
export function disable() { | ||
counters = undefined; | ||
measures = undefined; | ||
profilerEvent = undefined; | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this also be
/*@internal*/
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we not want to publicly expose
timestamp
? I guess we don't have a good reason to expose it, so may as well not.