From a3d0d646ae5b4843fbb2336a476b6d19dea50de5 Mon Sep 17 00:00:00 2001 From: xiao-lix Date: Tue, 13 Apr 2021 15:41:53 -0700 Subject: [PATCH 1/3] bug fix - update network time calculation in pageview performance manager --- .../Telemetry/PageViewPerformanceManager.ts | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/extensions/applicationinsights-analytics-js/src/JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts b/extensions/applicationinsights-analytics-js/src/JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts index 85d128247..2219fab46 100644 --- a/extensions/applicationinsights-analytics-js/src/JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts +++ b/extensions/applicationinsights-analytics-js/src/JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts @@ -37,22 +37,36 @@ export class PageViewPerformanceManager { * | || | | |-loadEventEnd * |---network---||---request---|---response---|---dom---| * |--------------------------total----------------------| + * + * total = The difference between the load event of the current document is completed and the first recorded timestamp of the performance entry : https://developer.mozilla.org/en-US/docs/Web/Performance/Navigation_and_resource_timings#duration + * network = Redirect time + App Cache + DNS lookup time + TCP connection time + * request = Request time : https://developer.mozilla.org/en-US/docs/Web/Performance/Navigation_and_resource_timings#request_time + * response = Response time + * dom = Document load time : https://html.spec.whatwg.org/multipage/dom.html#document-load-timing-info + * = Document processing time : https://developers.google.com/web/fundamentals/performance/navigation-and-resource-timing/#document_processing + * + Loading time : https://developers.google.com/web/fundamentals/performance/navigation-and-resource-timing/#loading */ const navigationTiming = this.getPerformanceNavigationTiming(); const timing = this.getPerformanceTiming(); + let total = 0; + let network = 0; + let request = 0; + let response = 0; + let dom = 0; + if (navigationTiming || timing) { if (navigationTiming) { - var total = navigationTiming.duration; - var network = dateTimeUtilsDuration(navigationTiming.startTime, navigationTiming.connectEnd); - var request = dateTimeUtilsDuration(navigationTiming.requestStart, navigationTiming.responseStart); - var response = dateTimeUtilsDuration(navigationTiming.responseStart, navigationTiming.responseEnd); - var dom = dateTimeUtilsDuration(navigationTiming.responseEnd, navigationTiming.loadEventEnd); + total = navigationTiming.duration; + network = total - dateTimeUtilsDuration(navigationTiming.connectEnd, navigationTiming.loadEventEnd); + request = dateTimeUtilsDuration(navigationTiming.requestStart, navigationTiming.responseStart); + response = dateTimeUtilsDuration(navigationTiming.responseStart, navigationTiming.responseEnd); + dom = dateTimeUtilsDuration(navigationTiming.responseEnd, navigationTiming.loadEventEnd); } else { - var total = dateTimeUtilsDuration(timing.navigationStart, timing.loadEventEnd); - var network = dateTimeUtilsDuration(timing.navigationStart, timing.connectEnd); - var request = dateTimeUtilsDuration(timing.requestStart, timing.responseStart); - var response = dateTimeUtilsDuration(timing.responseStart, timing.responseEnd); - var dom = dateTimeUtilsDuration(timing.responseEnd, timing.loadEventEnd); + total = dateTimeUtilsDuration(timing.navigationStart, timing.loadEventEnd); + network = dateTimeUtilsDuration(timing.navigationStart, timing.connectEnd); + request = dateTimeUtilsDuration(timing.requestStart, timing.responseStart); + response = dateTimeUtilsDuration(timing.responseStart, timing.responseEnd); + dom = dateTimeUtilsDuration(timing.responseEnd, timing.loadEventEnd); } if (total === 0) { From b3a6bf469d5438cc4ab841fc04c384cd247977fd Mon Sep 17 00:00:00 2001 From: xiao-lix Date: Wed, 14 Apr 2021 10:15:00 -0700 Subject: [PATCH 2/3] add null check --- .../JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/extensions/applicationinsights-analytics-js/src/JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts b/extensions/applicationinsights-analytics-js/src/JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts index 2219fab46..1922f2ae8 100644 --- a/extensions/applicationinsights-analytics-js/src/JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts +++ b/extensions/applicationinsights-analytics-js/src/JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts @@ -6,7 +6,7 @@ import { } from '@microsoft/applicationinsights-common'; import { IAppInsightsCore, IDiagnosticLogger, LoggingSeverity, - _InternalMessageId, getNavigator, getPerformance + _InternalMessageId, getNavigator, getPerformance, isNotNullOrUndefined } from '@microsoft/applicationinsights-core-js'; /** @@ -57,7 +57,10 @@ export class PageViewPerformanceManager { if (navigationTiming || timing) { if (navigationTiming) { total = navigationTiming.duration; - network = total - dateTimeUtilsDuration(navigationTiming.connectEnd, navigationTiming.loadEventEnd); + let afterNetwork = dateTimeUtilsDuration(navigationTiming.connectEnd, navigationTiming.loadEventEnd); + if (!isNotNullOrUndefined(afterNetwork)) { + network = total - afterNetwork; + } request = dateTimeUtilsDuration(navigationTiming.requestStart, navigationTiming.responseStart); response = dateTimeUtilsDuration(navigationTiming.responseStart, navigationTiming.responseEnd); dom = dateTimeUtilsDuration(navigationTiming.responseEnd, navigationTiming.loadEventEnd); From 5be9f0bb28b26de03ed78abb9ac9e5ac99bb0b27 Mon Sep 17 00:00:00 2001 From: xiao-lix Date: Wed, 14 Apr 2021 13:32:31 -0700 Subject: [PATCH 3/3] address comment --- .../Telemetry/PageViewPerformanceManager.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/extensions/applicationinsights-analytics-js/src/JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts b/extensions/applicationinsights-analytics-js/src/JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts index 1922f2ae8..6568a8ef4 100644 --- a/extensions/applicationinsights-analytics-js/src/JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts +++ b/extensions/applicationinsights-analytics-js/src/JavaScriptSDK/Telemetry/PageViewPerformanceManager.ts @@ -57,10 +57,12 @@ export class PageViewPerformanceManager { if (navigationTiming || timing) { if (navigationTiming) { total = navigationTiming.duration; - let afterNetwork = dateTimeUtilsDuration(navigationTiming.connectEnd, navigationTiming.loadEventEnd); - if (!isNotNullOrUndefined(afterNetwork)) { - network = total - afterNetwork; - } + /** + * support both cases: + * - startTime is always zero: https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigationTiming + * - for older browsers where the startTime is not zero + */ + network = navigationTiming.startTime === 0 ? navigationTiming.connectEnd : dateTimeUtilsDuration(navigationTiming.startTime, navigationTiming.connectEnd); request = dateTimeUtilsDuration(navigationTiming.requestStart, navigationTiming.responseStart); response = dateTimeUtilsDuration(navigationTiming.responseStart, navigationTiming.responseEnd); dom = dateTimeUtilsDuration(navigationTiming.responseEnd, navigationTiming.loadEventEnd);