Fix dependency order and observer registration

This commit is contained in:
Ron Buckton
2020-10-26 12:27:34 -07:00
parent 3517af8f80
commit 0847d85a4c
3 changed files with 20 additions and 8 deletions

View File

@@ -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;
@@ -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.
@@ -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;
}
/**
@@ -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;
}
/**
@@ -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));
}
/**
@@ -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;
}
@@ -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);
}
}
}

View File

@@ -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: {
@@ -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__");
}
}

View File

@@ -13,10 +13,10 @@
"corePublic.ts",
"core.ts",
"debug.ts",
"semver.ts",
"performanceCore.ts",
"performance.ts",
"perfLogger.ts",
"semver.ts",
"tracing.ts",
"types.ts",