1414 * limitations under the License.
1515 */
1616
17- import { getDateForPerfTime , getIsoDateStrForPerfTime , getPerfTimeOrigin } from '../src/common/time-util' ;
17+ import { adjustPerfTimeOrigin , getDateForPerfTime , getIsoDateStrForPerfTime , getPerfTimeOrigin , TEST_ONLY } from '../src/common/time-util' ;
1818
1919describe ( 'getPerfTimeOrigin' , ( ) => {
2020 it ( 'returns `performance.timeOrigin` if set' , ( ) => {
@@ -32,6 +32,63 @@ describe('getPerfTimeOrigin', () => {
3232 } ) ;
3333} ) ;
3434
35+ describe ( 'adjustPerfTimeOrigin' , ( ) => {
36+ const CLIENT_TIME_ORIGIN = 1548000000000 ;
37+ beforeEach ( ( ) => {
38+ spyOnProperty ( performance , 'timeOrigin' )
39+ . and . returnValue ( CLIENT_TIME_ORIGIN ) ;
40+ } ) ;
41+ afterEach ( ( ) => {
42+ TEST_ONLY . clearAdjustedPerfTime ( ) ;
43+ } ) ;
44+
45+ it ( 'keeps client time origin if no performance timing missing' , ( ) => {
46+ spyOnProperty ( performance , 'timing' ) . and . returnValue ( undefined ) ;
47+ adjustPerfTimeOrigin ( 1548000001000.2 , 5.1 ) ;
48+ expect ( getPerfTimeOrigin ( ) ) . toBe ( CLIENT_TIME_ORIGIN ) ;
49+ } ) ;
50+
51+ it ( 'keeps client time origin if server time longer than client' , ( ) => {
52+ // Client nav fetch duration is 5ms
53+ spyOnProperty ( performance . timing , 'requestStart' ) . and . returnValue ( 10.1 ) ;
54+ spyOnProperty ( performance . timing , 'responseStart' ) . and . returnValue ( 15.1 ) ;
55+
56+ // Server nav fetch duration is 10ms
57+ adjustPerfTimeOrigin ( 1548000001000.2 , /* serverNavFetchDuration */ 10 ) ;
58+
59+ expect ( getPerfTimeOrigin ( ) ) . toBe ( CLIENT_TIME_ORIGIN ) ;
60+ } ) ;
61+
62+ it ( 'adjusts origin to center server span in client span' , ( ) => {
63+ const clientNavFetchStartInPerfTime = 10 ; // Performance clock millis.
64+ spyOnProperty ( performance . timing , 'requestStart' )
65+ . and . returnValue ( clientNavFetchStartInPerfTime ) ;
66+ const clientNavFetchEndInPerfTime = 18 ; // Performance clock millis.
67+ spyOnProperty ( performance . timing , 'responseStart' )
68+ . and . returnValue ( clientNavFetchEndInPerfTime ) ;
69+
70+ const serverNavFetchStartEpochMillis = 1500000001000 ; // Epoch millis.
71+ const serverNavFetchDuration = 6 ; // Duration millis
72+ adjustPerfTimeOrigin (
73+ serverNavFetchStartEpochMillis , serverNavFetchDuration ) ;
74+
75+ // Calculations to make the expectation clearer:
76+ const clientNavFetchDuration =
77+ clientNavFetchEndInPerfTime - clientNavFetchStartInPerfTime ;
78+ expect ( clientNavFetchDuration ) . toBe ( 8 ) ; // Duration millis
79+ const networkTime = clientNavFetchDuration - serverNavFetchDuration ;
80+ expect ( networkTime ) . toBe ( 2 ) ; // Duration millis
81+ const clientNavStartInEpochMillis =
82+ serverNavFetchStartEpochMillis - networkTime / 2 ;
83+ expect ( clientNavStartInEpochMillis ) . toBe ( 1500000000999 ) ;
84+ const perfOriginInEpochMillis =
85+ clientNavStartInEpochMillis - clientNavFetchStartInPerfTime ;
86+ expect ( perfOriginInEpochMillis ) . toBe ( 1500000000989 ) ;
87+
88+ expect ( getPerfTimeOrigin ( ) ) . toBe ( perfOriginInEpochMillis ) ;
89+ } ) ;
90+ } ) ;
91+
3592describe ( 'getDateForPerfTime' , ( ) => {
3693 it ( 'calculates date for perf time based on time origin' , ( ) => {
3794 spyOnProperty ( performance , 'timeOrigin' ) . and . returnValue ( 1548000000000 ) ;
0 commit comments