Skip to content

Commit

Permalink
Merge pull request #41253 from microsoft/nativePerformanceHooks2
Browse files Browse the repository at this point in the history
Fix dependency order and observer registration
  • Loading branch information
rbuckton authored Oct 26, 2020
2 parents 3517af8 + 0847d85 commit 90e944d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
22 changes: 17 additions & 5 deletions src/compiler/performance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace ts.performance {
let perfHooks: PerformanceHooks | undefined;
let perfObserver: PerformanceObserver | undefined;
let perfEntryList: PerformanceObserverEntryList | undefined;
// when set, indicates the implementation of `Performance` to use for user timing.
// when unset, indicates user timing is unavailable or disabled.
let performanceImpl: Performance | undefined;
Expand Down Expand Up @@ -42,6 +41,8 @@ namespace ts.performance {
}

export const nullTimer: Timer = { enter: noop, exit: noop };
const counts = new Map<string, number>();
const durations = new Map<string, number>();

/**
* Marks a performance event.
Expand Down Expand Up @@ -71,7 +72,7 @@ namespace ts.performance {
* @param markName The name of the mark.
*/
export function getCount(markName: string) {
return perfEntryList?.getEntriesByName(markName, "mark").length || 0;
return counts.get(markName) || 0;
}

/**
Expand All @@ -80,7 +81,7 @@ namespace ts.performance {
* @param measureName The name of the measure whose durations should be accumulated.
*/
export function getDuration(measureName: string) {
return perfEntryList?.getEntriesByName(measureName, "measure").reduce((a, entry) => a + entry.duration, 0) || 0;
return durations.get(measureName) || 0;
}

/**
Expand All @@ -89,7 +90,7 @@ namespace ts.performance {
* @param cb The action to perform for each measure
*/
export function forEachMeasure(cb: (measureName: string, duration: number) => void) {
perfEntryList?.getEntriesByType("measure").forEach(({ name, duration }) => { cb(name, duration); });
durations.forEach((duration, measureName) => cb(measureName, duration));
}

/**
Expand All @@ -104,7 +105,7 @@ namespace ts.performance {
if (!performanceImpl) {
perfHooks ||= tryGetNativePerformanceHooks();
if (!perfHooks) return false;
perfObserver ||= new perfHooks.PerformanceObserver(list => perfEntryList = list);
perfObserver ||= new perfHooks.PerformanceObserver(updateStatisticsFromList);
perfObserver.observe({ entryTypes: ["mark", "measure"] });
performanceImpl = perfHooks.performance;
}
Expand All @@ -115,5 +116,16 @@ namespace ts.performance {
export function disable() {
perfObserver?.disconnect();
performanceImpl = undefined;
counts.clear();
durations.clear();
}

function updateStatisticsFromList(list: PerformanceObserverEntryList) {
for (const mark of list.getEntriesByType("mark")) {
counts.set(mark.name, (counts.get(mark.name) || 0) + 1);
}
for (const measure of list.getEntriesByType("measure")) {
durations.set(measure.name, (durations.get(measure.name) || 0) + measure.duration);
}
}
}
4 changes: 2 additions & 2 deletions src/compiler/performanceCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace ts {
// 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");
const range = new VersionRange("<12.16.3 || 13 <13.13");
if (range.test(version)) {
return {
performance: {
Expand All @@ -84,7 +84,7 @@ namespace ts {
performance.mark(end);
}
performance.measure(name, start, end);
if (end = "__performance.measure-fix__") {
if (end === "__performance.measure-fix__") {
performance.clearMarks("__performance.measure-fix__");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"corePublic.ts",
"core.ts",
"debug.ts",
"semver.ts",
"performanceCore.ts",
"performance.ts",
"perfLogger.ts",
"semver.ts",
"tracing.ts",

"types.ts",
Expand Down

0 comments on commit 90e944d

Please sign in to comment.