Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 4959430

Browse files
Make adjustPerfTimeOrigin take nav timing (#20)
1 parent ce1f30e commit 4959430

5 files changed

Lines changed: 67 additions & 31 deletions

File tree

packages/opencensus-web-core/src/common/time-util.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ export function getPerfTimeOrigin(): number {
4343
* the client in a <script> in the rendered HTML.
4444
* @param serverNavFetchDuration The server's measurement of the request
4545
* duration in milliseconds. This would also be sent to the client.
46+
* @param perfNavTiming The performance navigation timing, which can be
47+
* retrieved by `performance.getEntriesByType('navigation')[0]`, provided
48+
* that the browser supports it.
4649
*/
4750
export function adjustPerfTimeOrigin(
48-
serverNavFetchStartTime: number, serverNavFetchDuration: number) {
49-
// If there is no performance timing info, we don't have the client's
50-
// start/end times for the navigation fetch, so we can't accurately use the
51-
// server times to adjust the client time origin.
52-
if (!performance.timing) return;
53-
const clientStart = performance.timing.requestStart;
54-
const clientEnd = performance.timing.responseStart;
51+
serverNavFetchStartTime: number, serverNavFetchDuration: number,
52+
perfNavTiming: PerformanceNavigationTiming) {
53+
const clientStart = perfNavTiming.requestStart;
54+
const clientEnd = perfNavTiming.responseStart;
5555
const clientNavFetchDuration = clientEnd - clientStart;
5656

5757
// Server time is more than client time, which we don't expect, so don't try

packages/opencensus-web-core/test/test-time-util.ts

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,47 @@ describe('time utils', () => {
4040
});
4141

4242
describe('adjustPerfTimeOrigin', () => {
43+
function createNavTiming({requestStart, responseStart}:
44+
{requestStart: number, responseStart: number}):
45+
PerformanceNavigationTiming {
46+
return {
47+
// This are used by the time adjustment function below.
48+
requestStart, // Client start time in performance clock millis.
49+
responseStart, // Client end time in performance clock millis.
50+
// These are needed to satisfy the interface.
51+
connectEnd: 0,
52+
connectStart: 0,
53+
decodedBodySize: 0,
54+
domComplete: 0,
55+
domContentLoadedEventEnd: 0,
56+
domContentLoadedEventStart: 0,
57+
domInteractive: 0,
58+
domainLookupEnd: 0,
59+
domainLookupStart: 0,
60+
duration: 0,
61+
encodedBodySize: 0,
62+
entryType: '',
63+
fetchStart: 0,
64+
initiatorType: '',
65+
loadEventEnd: 0,
66+
loadEventStart: 0,
67+
name: '',
68+
nextHopProtocol: '',
69+
redirectCount: 0,
70+
redirectEnd: 0,
71+
redirectStart: 0,
72+
responseEnd: 0,
73+
secureConnectionStart: 0,
74+
startTime: 0,
75+
toJSON: () => ({}),
76+
transferSize: 0,
77+
type: 'navigate',
78+
unloadEventEnd: 0,
79+
unloadEventStart: 0,
80+
workerStart: 0,
81+
};
82+
}
83+
4384
const CLIENT_TIME_ORIGIN = 1548000000000;
4485
beforeEach(() => {
4586
mockGetterOrValue(performance, 'timeOrigin', CLIENT_TIME_ORIGIN);
@@ -48,35 +89,31 @@ describe('time utils', () => {
4889
TEST_ONLY.clearAdjustedPerfTime();
4990
});
5091

51-
it('keeps client time origin if performance timing missing', () => {
52-
spyOnProperty(performance, 'timing').and.returnValue(undefined);
53-
adjustPerfTimeOrigin(1548000001000.2, 5.1);
54-
expect(getPerfTimeOrigin()).toBe(CLIENT_TIME_ORIGIN);
55-
});
56-
5792
it('keeps client time origin if server time longer than client', () => {
5893
// Client nav fetch duration is 5ms
59-
spyOnProperty(performance.timing, 'requestStart').and.returnValue(10.1);
60-
spyOnProperty(performance.timing, 'responseStart').and.returnValue(15.1);
94+
const perfNavTiming =
95+
createNavTiming({requestStart: 10.1, responseStart: 15.1});
6196

6297
// Server nav fetch duration is 10ms
63-
adjustPerfTimeOrigin(1548000001000.2, /* serverNavFetchDuration */ 10);
98+
adjustPerfTimeOrigin(
99+
1548000001000.2, /* serverNavFetchDuration */ 10, perfNavTiming);
64100

65101
expect(getPerfTimeOrigin()).toBe(CLIENT_TIME_ORIGIN);
66102
});
67103

68104
it('adjusts origin to center server span in client span', () => {
69-
const clientNavFetchStartInPerfTime = 10; // Performance clock millis.
70-
spyOnProperty(performance.timing, 'requestStart')
71-
.and.returnValue(clientNavFetchStartInPerfTime);
72-
const clientNavFetchEndInPerfTime = 18; // Performance clock millis.
73-
spyOnProperty(performance.timing, 'responseStart')
74-
.and.returnValue(clientNavFetchEndInPerfTime);
75-
105+
const clientNavFetchStartInPerfTime = 10;
106+
const clientNavFetchEndInPerfTime = 18;
107+
const perfNavTiming = createNavTiming({
108+
requestStart: clientNavFetchStartInPerfTime,
109+
responseStart: clientNavFetchEndInPerfTime,
110+
});
76111
const serverNavFetchStartEpochMillis = 1500000001000; // Epoch millis.
77112
const serverNavFetchDuration = 6; // Duration millis
113+
78114
adjustPerfTimeOrigin(
79-
serverNavFetchStartEpochMillis, serverNavFetchDuration);
115+
serverNavFetchStartEpochMillis, serverNavFetchDuration,
116+
perfNavTiming);
80117

81118
// Calculations to make the expectation clearer:
82119
const clientNavFetchDuration =

packages/opencensus-web-instrumentation-perf/src/perf-types.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export declare interface PerformanceNavigationTimingExtended extends
9191
readonly type: NavigationType;
9292
readonly unloadEventEnd: number;
9393
readonly unloadEventStart: number;
94+
readonly redirectCount: number;
9495
}
9596

9697
/**
@@ -158,12 +159,8 @@ export declare interface PerformanceLongTaskTiming extends PerformanceEntry {
158159
readonly attribution: TaskAttributionTiming[];
159160
}
160161

161-
/** Enables casting `window` to have optional `PerformanceObserver` field. */
162-
export declare type WindowWithPerformanceObserver = Window & {
163-
readonly PerformanceObserver?: PerformanceObserver;
164-
};
165-
166162
/** Type for the `window` object with a field to track LongTask timings. */
167-
export declare type WindowWithLongTasks = WindowWithPerformanceObserver & {
163+
export declare interface WindowWithLongTasks extends Window {
164+
readonly PerformanceObserver?: PerformanceObserver;
168165
ocwLt?: PerformanceLongTaskTiming[];
169-
};
166+
}

packages/opencensus-web-instrumentation-perf/test/test-perf-grouper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const RESOURCE_ENTRY: PerformanceResourceTimingExtended = {
9191
nextHopProtocol: 'h2',
9292
redirectEnd: 0,
9393
redirectStart: 0,
94+
redirectCount: 0,
9495
requestStart: 0,
9596
responseEnd: 280.1000000035856,
9697
responseStart: 0,

packages/opencensus-web-instrumentation-perf/test/test-resource-span.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe('getResourceSpan', () => {
4747
nextHopProtocol: 'h2',
4848
redirectEnd: 0,
4949
redirectStart: 0,
50+
redirectCount: 0,
5051
requestStart: 0,
5152
responseEnd: 280.1,
5253
responseStart: 0,

0 commit comments

Comments
 (0)