@paulirish/trace_engine 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/analyze-trace.mjs +1 -1
  2. package/core/platform/DevToolsPath.d.ts +4 -13
  3. package/core/platform/DevToolsPath.js +7 -4
  4. package/core/platform/DevToolsPath.js.map +1 -7
  5. package/core/platform/MimeType.d.ts +27 -0
  6. package/core/platform/MimeType.js +119 -86
  7. package/core/platform/MimeType.js.map +1 -7
  8. package/core/platform/Timing.d.ts +7 -0
  9. package/core/platform/Timing.js +7 -4
  10. package/core/platform/Timing.js.map +1 -7
  11. package/core/platform/UIString.d.ts +2 -5
  12. package/core/platform/UIString.js +5 -2
  13. package/core/platform/UIString.js.map +1 -7
  14. package/core/platform/UserVisibleError.js +19 -10
  15. package/core/platform/UserVisibleError.js.map +1 -7
  16. package/core/platform/array-utilities.d.ts +48 -10
  17. package/core/platform/array-utilities.js +160 -124
  18. package/core/platform/array-utilities.js.map +1 -7
  19. package/core/platform/brand.d.ts +14 -0
  20. package/core/platform/brand.js +5 -1
  21. package/core/platform/brand.js.map +1 -7
  22. package/core/platform/date-utilities.js +10 -6
  23. package/core/platform/date-utilities.js.map +1 -7
  24. package/core/platform/dom-utilities.d.ts +3 -1
  25. package/core/platform/dom-utilities.js +94 -83
  26. package/core/platform/dom-utilities.js.map +1 -7
  27. package/core/platform/keyboard-utilities.d.ts +2 -0
  28. package/core/platform/keyboard-utilities.js +15 -24
  29. package/core/platform/keyboard-utilities.js.map +1 -7
  30. package/core/platform/map-utilities.d.ts +4 -0
  31. package/core/platform/map-utilities.js +66 -60
  32. package/core/platform/map-utilities.js.map +1 -7
  33. package/core/platform/number-utilities.js +66 -55
  34. package/core/platform/number-utilities.js.map +1 -7
  35. package/core/platform/platform.d.ts +5 -1
  36. package/core/platform/platform.js +54 -37
  37. package/core/platform/platform.js.map +1 -7
  38. package/core/platform/promise-utilities.d.ts +10 -0
  39. package/core/platform/promise-utilities.js +16 -8
  40. package/core/platform/promise-utilities.js.map +1 -7
  41. package/core/platform/set-utilities.js +20 -17
  42. package/core/platform/set-utilities.js.map +1 -7
  43. package/core/platform/string-utilities.d.ts +32 -1
  44. package/core/platform/string-utilities.js +453 -379
  45. package/core/platform/string-utilities.js.map +1 -7
  46. package/core/platform/typescript-utilities.d.ts +5 -5
  47. package/core/platform/typescript-utilities.js +19 -7
  48. package/core/platform/typescript-utilities.js.map +1 -7
  49. package/generated/protocol.d.ts +2081 -347
  50. package/generated/protocol.js +5 -2230
  51. package/models/cpu_profile/CPUProfileDataModel.d.ts +77 -0
  52. package/models/cpu_profile/CPUProfileDataModel.js +492 -359
  53. package/models/cpu_profile/CPUProfileDataModel.js.map +1 -7
  54. package/models/cpu_profile/ProfileTreeModel.d.ts +29 -0
  55. package/models/cpu_profile/ProfileTreeModel.js +87 -82
  56. package/models/cpu_profile/ProfileTreeModel.js.map +1 -7
  57. package/models/cpu_profile/cpu_profile.d.ts +3 -0
  58. package/models/cpu_profile/cpu_profile.js +7 -7
  59. package/models/cpu_profile/cpu_profile.js.map +1 -7
  60. package/models/trace/EntriesFilter.d.ts +55 -0
  61. package/models/trace/EntriesFilter.js +227 -166
  62. package/models/trace/EntriesFilter.js.map +1 -7
  63. package/models/trace/LegacyTracingModel.js.map +1 -7
  64. package/models/trace/ModelImpl.d.ts +110 -0
  65. package/models/trace/ModelImpl.js +161 -102
  66. package/models/trace/ModelImpl.js.map +1 -7
  67. package/models/trace/Processor.d.ts +36 -0
  68. package/models/trace/Processor.js +197 -163
  69. package/models/trace/Processor.js.map +1 -7
  70. package/models/trace/TracingManager.js.map +1 -7
  71. package/models/trace/extras/FetchNodes.d.ts +46 -0
  72. package/models/trace/extras/FetchNodes.js +132 -91
  73. package/models/trace/extras/FetchNodes.js.map +1 -7
  74. package/models/trace/extras/FilmStrip.d.ts +19 -0
  75. package/models/trace/extras/FilmStrip.js +38 -31
  76. package/models/trace/extras/FilmStrip.js.map +1 -7
  77. package/models/trace/extras/MainThreadActivity.d.ts +2 -0
  78. package/models/trace/extras/MainThreadActivity.js +72 -56
  79. package/models/trace/extras/MainThreadActivity.js.map +1 -7
  80. package/models/trace/extras/Metadata.d.ts +2 -0
  81. package/models/trace/extras/Metadata.js +42 -26
  82. package/models/trace/extras/Metadata.js.map +1 -7
  83. package/models/trace/extras/extras.js.map +1 -7
  84. package/models/trace/handlers/AnimationHandler.d.ts +8 -0
  85. package/models/trace/handlers/AnimationHandler.js +22 -20
  86. package/models/trace/handlers/AnimationHandler.js.map +1 -7
  87. package/models/trace/handlers/AuctionWorkletsHandler.d.ts +8 -0
  88. package/models/trace/handlers/AuctionWorkletsHandler.js +143 -89
  89. package/models/trace/handlers/AuctionWorkletsHandler.js.map +1 -7
  90. package/models/trace/handlers/FramesHandler.d.ts +76 -0
  91. package/models/trace/handlers/FramesHandler.js +424 -355
  92. package/models/trace/handlers/FramesHandler.js.map +1 -7
  93. package/models/trace/handlers/GPUHandler.d.ts +11 -0
  94. package/models/trace/handlers/GPUHandler.js +41 -37
  95. package/models/trace/handlers/GPUHandler.js.map +1 -7
  96. package/models/trace/handlers/InitiatorsHandler.d.ts +10 -0
  97. package/models/trace/handlers/InitiatorsHandler.js +164 -113
  98. package/models/trace/handlers/InitiatorsHandler.js.map +1 -7
  99. package/models/trace/handlers/InvalidationsHandler.d.ts +10 -0
  100. package/models/trace/handlers/InvalidationsHandler.js +101 -79
  101. package/models/trace/handlers/InvalidationsHandler.js.map +1 -7
  102. package/models/trace/handlers/LargestImagePaintHandler.d.ts +5 -0
  103. package/models/trace/handlers/LargestImagePaintHandler.js +32 -12
  104. package/models/trace/handlers/LargestImagePaintHandler.js.map +1 -7
  105. package/models/trace/handlers/LargestTextPaintHandler.d.ts +5 -0
  106. package/models/trace/handlers/LargestTextPaintHandler.js +20 -12
  107. package/models/trace/handlers/LargestTextPaintHandler.js.map +1 -7
  108. package/models/trace/handlers/LayerTreeHandler.d.ts +13 -0
  109. package/models/trace/handlers/LayerTreeHandler.js +96 -70
  110. package/models/trace/handlers/LayerTreeHandler.js.map +1 -7
  111. package/models/trace/handlers/LayoutShiftsHandler.d.ts +44 -0
  112. package/models/trace/handlers/LayoutShiftsHandler.js +304 -227
  113. package/models/trace/handlers/LayoutShiftsHandler.js.map +1 -7
  114. package/models/trace/handlers/MemoryHandler.d.ts +7 -0
  115. package/models/trace/handlers/MemoryHandler.js +14 -11
  116. package/models/trace/handlers/MemoryHandler.js.map +1 -7
  117. package/models/trace/handlers/MetaHandler.d.ts +37 -0
  118. package/models/trace/handlers/MetaHandler.js +314 -226
  119. package/models/trace/handlers/MetaHandler.js.map +1 -7
  120. package/models/trace/handlers/ModelHandlers.d.ts +21 -0
  121. package/models/trace/handlers/ModelHandlers.js +25 -22
  122. package/models/trace/handlers/ModelHandlers.js.map +1 -7
  123. package/models/trace/handlers/NetworkRequestsHandler.d.ts +17 -0
  124. package/models/trace/handlers/NetworkRequestsHandler.js +342 -218
  125. package/models/trace/handlers/NetworkRequestsHandler.js.map +1 -7
  126. package/models/trace/handlers/PageLoadMetricsHandler.d.ts +67 -0
  127. package/models/trace/handlers/PageLoadMetricsHandler.js +357 -284
  128. package/models/trace/handlers/PageLoadMetricsHandler.js.map +1 -7
  129. package/models/trace/handlers/RendererHandler.d.ts +101 -0
  130. package/models/trace/handlers/RendererHandler.js +295 -191
  131. package/models/trace/handlers/RendererHandler.js.map +1 -7
  132. package/models/trace/handlers/SamplesHandler.d.ts +46 -0
  133. package/models/trace/handlers/SamplesHandler.js +195 -158
  134. package/models/trace/handlers/SamplesHandler.js.map +1 -7
  135. package/models/trace/handlers/ScreenshotsHandler.d.ts +7 -0
  136. package/models/trace/handlers/ScreenshotsHandler.js +63 -41
  137. package/models/trace/handlers/ScreenshotsHandler.js.map +1 -7
  138. package/models/trace/handlers/Threads.d.ts +33 -0
  139. package/models/trace/handlers/Threads.js +85 -67
  140. package/models/trace/handlers/Threads.js.map +1 -7
  141. package/models/trace/handlers/UserInteractionsHandler.d.ts +57 -0
  142. package/models/trace/handlers/UserInteractionsHandler.js +240 -141
  143. package/models/trace/handlers/UserInteractionsHandler.js.map +1 -7
  144. package/models/trace/handlers/UserTimingsHandler.d.ts +28 -0
  145. package/models/trace/handlers/UserTimingsHandler.js +91 -80
  146. package/models/trace/handlers/UserTimingsHandler.js.map +1 -7
  147. package/models/trace/handlers/WarningsHandler.d.ts +14 -0
  148. package/models/trace/handlers/WarningsHandler.js +100 -62
  149. package/models/trace/handlers/WarningsHandler.js.map +1 -7
  150. package/models/trace/handlers/WorkersHandler.d.ts +11 -0
  151. package/models/trace/handlers/WorkersHandler.js +40 -38
  152. package/models/trace/handlers/WorkersHandler.js.map +1 -7
  153. package/models/trace/handlers/handlers.d.ts +3 -0
  154. package/models/trace/handlers/handlers.js +7 -4
  155. package/models/trace/handlers/handlers.js.map +1 -7
  156. package/models/trace/handlers/types.d.ts +45 -0
  157. package/models/trace/handlers/types.js +15 -15
  158. package/models/trace/handlers/types.js.map +1 -7
  159. package/models/trace/helpers/SamplesIntegrator.d.ts +49 -0
  160. package/models/trace/helpers/SamplesIntegrator.js +381 -204
  161. package/models/trace/helpers/SamplesIntegrator.js.map +1 -7
  162. package/models/trace/helpers/Timing.d.ts +26 -0
  163. package/models/trace/helpers/Timing.js +131 -110
  164. package/models/trace/helpers/Timing.js.map +1 -7
  165. package/models/trace/helpers/Trace.d.ts +37 -0
  166. package/models/trace/helpers/Trace.js +200 -166
  167. package/models/trace/helpers/Trace.js.map +1 -7
  168. package/models/trace/helpers/TreeHelpers.d.ts +90 -0
  169. package/models/trace/helpers/TreeHelpers.js +203 -100
  170. package/models/trace/helpers/TreeHelpers.js.map +1 -7
  171. package/models/trace/helpers/helpers.d.ts +4 -0
  172. package/models/trace/helpers/helpers.js +8 -5
  173. package/models/trace/helpers/helpers.js.map +1 -7
  174. package/models/trace/root-causes/LayoutShift.d.ts +119 -0
  175. package/models/trace/root-causes/LayoutShift.js +470 -323
  176. package/models/trace/root-causes/LayoutShift.js.map +1 -7
  177. package/models/trace/root-causes/RootCauses.d.ts +14 -0
  178. package/models/trace/root-causes/RootCauses.js +9 -6
  179. package/models/trace/root-causes/RootCauses.js.map +1 -7
  180. package/models/trace/root-causes/root-causes.d.ts +1 -0
  181. package/models/trace/root-causes/root-causes.js +5 -2
  182. package/models/trace/root-causes/root-causes.js.map +1 -7
  183. package/models/trace/trace.d.ts +11 -0
  184. package/models/trace/trace.js +17 -23
  185. package/models/trace/trace.js.map +1 -7
  186. package/models/trace/types/Configuration.d.ts +33 -0
  187. package/models/trace/types/Configuration.js +25 -14
  188. package/models/trace/types/Configuration.js.map +1 -7
  189. package/models/trace/types/File.d.ts +23 -0
  190. package/models/trace/types/File.js +5 -6
  191. package/models/trace/types/File.js.map +1 -7
  192. package/models/trace/types/Timing.d.ts +25 -0
  193. package/models/trace/types/Timing.js +10 -11
  194. package/models/trace/types/Timing.js.map +1 -7
  195. package/models/trace/types/TraceEvents.d.ts +1571 -0
  196. package/models/trace/types/TraceEvents.js +174 -381
  197. package/models/trace/types/TraceEvents.js.map +1 -7
  198. package/models/trace/types/types.d.ts +4 -0
  199. package/models/trace/types/types.js +8 -5
  200. package/models/trace/types/types.js.map +1 -7
  201. package/package.json +1 -1
  202. package/TracingManager.js +0 -0
  203. package/extras/extras.js +0 -0
  204. package/trace.mjs +0 -6980
  205. package/trace.mjs.map +0 -8
@@ -1,270 +1,347 @@
1
- import * as Platform from "../../../core/platform/platform.js";
2
- import * as Helpers from "../helpers/helpers.js";
3
- import * as Types from "../types/types.js";
4
- import { data as metaHandlerData } from "./MetaHandler.js";
5
- import { ScoreClassification } from "./PageLoadMetricsHandler.js";
6
- import { HandlerState } from "./types.js";
7
- export const MAX_CLUSTER_DURATION = Helpers.Timing.millisecondsToMicroseconds(Types.Timing.MilliSeconds(5e3));
8
- export const MAX_SHIFT_TIME_DELTA = Helpers.Timing.millisecondsToMicroseconds(Types.Timing.MilliSeconds(1e3));
1
+ // Copyright 2022 The Chromium Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+ import * as Platform from '../../../core/platform/platform.js';
5
+ import * as Helpers from '../helpers/helpers.js';
6
+ import * as Types from '../types/types.js';
7
+ import { data as metaHandlerData } from './MetaHandler.js';
8
+ // This represents the maximum #time we will allow a cluster to go before we
9
+ // reset it.
10
+ export const MAX_CLUSTER_DURATION = Helpers.Timing.millisecondsToMicroseconds(Types.Timing.MilliSeconds(5000));
11
+ // This represents the maximum #time we will allow between layout shift events
12
+ // before considering it to be the start of a new cluster.
13
+ export const MAX_SHIFT_TIME_DELTA = Helpers.Timing.millisecondsToMicroseconds(Types.Timing.MilliSeconds(1000));
14
+ // Layout shifts are reported globally to the developer, irrespective of which
15
+ // frame they originated in. However, each process does have its own individual
16
+ // CLS score, so we need to segment by process. This means Layout Shifts from
17
+ // sites with one process (no subframes, or subframes from the same origin)
18
+ // will be reported together. In the case of multiple renderers (frames across
19
+ // different origins), we offer the developer the ability to switch renderer in
20
+ // the UI.
9
21
  const layoutShiftEvents = [];
22
+ // These events denote potential node resizings. We store them to link captured
23
+ // layout shifts to the resizing of unsized elements.
10
24
  const layoutInvalidationEvents = [];
11
25
  const scheduleStyleInvalidationEvents = [];
12
26
  const styleRecalcInvalidationEvents = [];
13
- const backendNodeIds = /* @__PURE__ */ new Set();
27
+ const backendNodeIds = new Set();
28
+ // Layout shifts happen during PrePaint as part of the rendering lifecycle.
29
+ // We determine if a LayoutInvalidation event is a potential root cause of a layout
30
+ // shift if the next PrePaint after the LayoutInvalidation is the parent
31
+ // node of such shift.
14
32
  const prePaintEvents = [];
15
33
  let sessionMaxScore = 0;
16
34
  let clsWindowID = -1;
17
35
  const clusters = [];
36
+ // The complete timeline of LS score changes in a trace.
37
+ // Includes drops to 0 when session windows end.
18
38
  const scoreRecords = [];
19
- let handlerState = HandlerState.UNINITIALIZED;
39
+ let handlerState = 1 /* HandlerState.UNINITIALIZED */;
20
40
  export function initialize() {
21
- if (handlerState !== HandlerState.UNINITIALIZED) {
22
- throw new Error("LayoutShifts Handler was not reset");
23
- }
24
- handlerState = HandlerState.INITIALIZED;
41
+ if (handlerState !== 1 /* HandlerState.UNINITIALIZED */) {
42
+ throw new Error('LayoutShifts Handler was not reset');
43
+ }
44
+ handlerState = 2 /* HandlerState.INITIALIZED */;
25
45
  }
26
46
  export function reset() {
27
- handlerState = HandlerState.UNINITIALIZED;
28
- layoutShiftEvents.length = 0;
29
- layoutInvalidationEvents.length = 0;
30
- scheduleStyleInvalidationEvents.length = 0;
31
- styleRecalcInvalidationEvents.length = 0;
32
- prePaintEvents.length = 0;
33
- backendNodeIds.clear();
34
- clusters.length = 0;
35
- sessionMaxScore = 0;
36
- scoreRecords.length = 0;
37
- clsWindowID = -1;
47
+ handlerState = 1 /* HandlerState.UNINITIALIZED */;
48
+ layoutShiftEvents.length = 0;
49
+ layoutInvalidationEvents.length = 0;
50
+ scheduleStyleInvalidationEvents.length = 0;
51
+ styleRecalcInvalidationEvents.length = 0;
52
+ prePaintEvents.length = 0;
53
+ backendNodeIds.clear();
54
+ clusters.length = 0;
55
+ sessionMaxScore = 0;
56
+ scoreRecords.length = 0;
57
+ clsWindowID = -1;
38
58
  }
39
59
  export function handleEvent(event) {
40
- if (handlerState !== HandlerState.INITIALIZED) {
41
- throw new Error("Handler is not initialized");
42
- }
43
- if (Types.TraceEvents.isTraceEventLayoutShift(event) && !event.args.data?.had_recent_input) {
44
- layoutShiftEvents.push(event);
45
- return;
46
- }
47
- if (Types.TraceEvents.isTraceEventLayoutInvalidationTracking(event)) {
48
- layoutInvalidationEvents.push(event);
49
- return;
50
- }
51
- if (Types.TraceEvents.isTraceEventScheduleStyleInvalidationTracking(event)) {
52
- scheduleStyleInvalidationEvents.push(event);
53
- }
54
- if (Types.TraceEvents.isTraceEventStyleRecalcInvalidationTracking(event)) {
55
- styleRecalcInvalidationEvents.push(event);
56
- }
57
- if (Types.TraceEvents.isTraceEventPrePaint(event)) {
58
- prePaintEvents.push(event);
59
- return;
60
- }
60
+ if (handlerState !== 2 /* HandlerState.INITIALIZED */) {
61
+ throw new Error('Handler is not initialized');
62
+ }
63
+ if (Types.TraceEvents.isTraceEventLayoutShift(event) && !event.args.data?.had_recent_input) {
64
+ layoutShiftEvents.push(event);
65
+ return;
66
+ }
67
+ if (Types.TraceEvents.isTraceEventLayoutInvalidationTracking(event)) {
68
+ layoutInvalidationEvents.push(event);
69
+ return;
70
+ }
71
+ if (Types.TraceEvents.isTraceEventScheduleStyleInvalidationTracking(event)) {
72
+ scheduleStyleInvalidationEvents.push(event);
73
+ }
74
+ if (Types.TraceEvents.isTraceEventStyleRecalcInvalidationTracking(event)) {
75
+ styleRecalcInvalidationEvents.push(event);
76
+ }
77
+ if (Types.TraceEvents.isTraceEventPrePaint(event)) {
78
+ prePaintEvents.push(event);
79
+ return;
80
+ }
61
81
  }
62
82
  function traceWindowFromTime(time) {
63
- return {
64
- min: time,
65
- max: time,
66
- range: Types.Timing.MicroSeconds(0)
67
- };
83
+ return {
84
+ min: time,
85
+ max: time,
86
+ range: Types.Timing.MicroSeconds(0),
87
+ };
68
88
  }
69
89
  function updateTraceWindowMax(traceWindow, newMax) {
70
- traceWindow.max = newMax;
71
- traceWindow.range = Types.Timing.MicroSeconds(traceWindow.max - traceWindow.min);
90
+ traceWindow.max = newMax;
91
+ traceWindow.range = Types.Timing.MicroSeconds(traceWindow.max - traceWindow.min);
72
92
  }
73
93
  function buildScoreRecords() {
74
- const { traceBounds } = metaHandlerData();
75
- scoreRecords.push({ ts: traceBounds.min, score: 0 });
76
- for (const cluster of clusters) {
77
- let clusterScore = 0;
78
- if (cluster.events[0].args.data) {
79
- scoreRecords.push({ ts: cluster.clusterWindow.min, score: cluster.events[0].args.data.weighted_score_delta });
80
- }
81
- for (let i = 0; i < cluster.events.length; i++) {
82
- const event = cluster.events[i];
83
- if (!event.args.data) {
84
- continue;
85
- }
86
- clusterScore += event.args.data.weighted_score_delta;
87
- scoreRecords.push({ ts: event.ts, score: clusterScore });
94
+ const { traceBounds } = metaHandlerData();
95
+ scoreRecords.push({ ts: traceBounds.min, score: 0 });
96
+ for (const cluster of clusters) {
97
+ let clusterScore = 0;
98
+ if (cluster.events[0].args.data) {
99
+ scoreRecords.push({ ts: cluster.clusterWindow.min, score: cluster.events[0].args.data.weighted_score_delta });
100
+ }
101
+ for (let i = 0; i < cluster.events.length; i++) {
102
+ const event = cluster.events[i];
103
+ if (!event.args.data) {
104
+ continue;
105
+ }
106
+ clusterScore += event.args.data.weighted_score_delta;
107
+ scoreRecords.push({ ts: event.ts, score: clusterScore });
108
+ }
109
+ scoreRecords.push({ ts: cluster.clusterWindow.max, score: 0 });
88
110
  }
89
- scoreRecords.push({ ts: cluster.clusterWindow.max, score: 0 });
90
- }
91
111
  }
112
+ /**
113
+ * Collects backend node ids coming from LayoutShift and LayoutInvalidation
114
+ * events.
115
+ */
92
116
  function collectNodes() {
93
- backendNodeIds.clear();
94
- for (const layoutShift of layoutShiftEvents) {
95
- if (!layoutShift.args.data?.impacted_nodes) {
96
- continue;
97
- }
98
- for (const node of layoutShift.args.data.impacted_nodes) {
99
- backendNodeIds.add(node.node_id);
117
+ backendNodeIds.clear();
118
+ // Collect the node ids present in the shifts.
119
+ for (const layoutShift of layoutShiftEvents) {
120
+ if (!layoutShift.args.data?.impacted_nodes) {
121
+ continue;
122
+ }
123
+ for (const node of layoutShift.args.data.impacted_nodes) {
124
+ backendNodeIds.add(node.node_id);
125
+ }
100
126
  }
101
- }
102
- for (const layoutInvalidation of layoutInvalidationEvents) {
103
- if (!layoutInvalidation.args.data?.nodeId) {
104
- continue;
127
+ // Collect the node ids present in LayoutInvalidation & scheduleStyleInvalidation events.
128
+ for (const layoutInvalidation of layoutInvalidationEvents) {
129
+ if (!layoutInvalidation.args.data?.nodeId) {
130
+ continue;
131
+ }
132
+ backendNodeIds.add(layoutInvalidation.args.data.nodeId);
105
133
  }
106
- backendNodeIds.add(layoutInvalidation.args.data.nodeId);
107
- }
108
- for (const scheduleStyleInvalidation of scheduleStyleInvalidationEvents) {
109
- if (!scheduleStyleInvalidation.args.data?.nodeId) {
110
- continue;
134
+ for (const scheduleStyleInvalidation of scheduleStyleInvalidationEvents) {
135
+ if (!scheduleStyleInvalidation.args.data?.nodeId) {
136
+ continue;
137
+ }
138
+ backendNodeIds.add(scheduleStyleInvalidation.args.data.nodeId);
111
139
  }
112
- backendNodeIds.add(scheduleStyleInvalidation.args.data.nodeId);
113
- }
114
140
  }
115
141
  export async function finalize() {
116
- layoutShiftEvents.sort((a, b) => a.ts - b.ts);
117
- prePaintEvents.sort((a, b) => a.ts - b.ts);
118
- layoutInvalidationEvents.sort((a, b) => a.ts - b.ts);
119
- await buildLayoutShiftsClusters();
120
- buildScoreRecords();
121
- collectNodes();
122
- handlerState = HandlerState.FINALIZED;
142
+ // Ensure the events are sorted by #time ascending.
143
+ layoutShiftEvents.sort((a, b) => a.ts - b.ts);
144
+ prePaintEvents.sort((a, b) => a.ts - b.ts);
145
+ layoutInvalidationEvents.sort((a, b) => a.ts - b.ts);
146
+ // Each function transforms the data used by the next, as such the invoke order
147
+ // is important.
148
+ await buildLayoutShiftsClusters();
149
+ buildScoreRecords();
150
+ collectNodes();
151
+ handlerState = 3 /* HandlerState.FINALIZED */;
123
152
  }
124
153
  async function buildLayoutShiftsClusters() {
125
- const { navigationsByFrameId, mainFrameId, traceBounds } = metaHandlerData();
126
- const navigations = navigationsByFrameId.get(mainFrameId) || [];
127
- if (layoutShiftEvents.length === 0) {
128
- return;
129
- }
130
- let firstShiftTime = layoutShiftEvents[0].ts;
131
- let lastShiftTime = layoutShiftEvents[0].ts;
132
- let lastShiftNavigation = null;
133
- for (const event of layoutShiftEvents) {
134
- const clusterDurationExceeded = event.ts - firstShiftTime > MAX_CLUSTER_DURATION;
135
- const maxTimeDeltaSinceLastShiftExceeded = event.ts - lastShiftTime > MAX_SHIFT_TIME_DELTA;
136
- const currentShiftNavigation = Platform.ArrayUtilities.nearestIndexFromEnd(navigations, (nav) => nav.ts < event.ts);
137
- const hasNavigated = lastShiftNavigation !== currentShiftNavigation && currentShiftNavigation !== null;
138
- if (clusterDurationExceeded || maxTimeDeltaSinceLastShiftExceeded || hasNavigated || !clusters.length) {
139
- const clusterStartTime = event.ts;
140
- const endTimeByMaxSessionDuration = clusterDurationExceeded ? firstShiftTime + MAX_CLUSTER_DURATION : Infinity;
141
- const endTimeByMaxShiftGap = maxTimeDeltaSinceLastShiftExceeded ? lastShiftTime + MAX_SHIFT_TIME_DELTA : Infinity;
142
- const endTimeByNavigation = hasNavigated ? navigations[currentShiftNavigation].ts : Infinity;
143
- const previousClusterEndTime = Math.min(endTimeByMaxSessionDuration, endTimeByMaxShiftGap, endTimeByNavigation);
144
- if (clusters.length > 0) {
145
- const currentCluster2 = clusters[clusters.length - 1];
146
- updateTraceWindowMax(currentCluster2.clusterWindow, Types.Timing.MicroSeconds(previousClusterEndTime));
147
- }
148
- clusters.push({
149
- events: [],
150
- clusterWindow: traceWindowFromTime(clusterStartTime),
151
- clusterCumulativeScore: 0,
152
- scoreWindows: {
153
- good: traceWindowFromTime(clusterStartTime),
154
- needsImprovement: null,
155
- bad: null
156
- }
157
- });
158
- firstShiftTime = clusterStartTime;
154
+ const { navigationsByFrameId, mainFrameId, traceBounds } = metaHandlerData();
155
+ const navigations = navigationsByFrameId.get(mainFrameId) || [];
156
+ if (layoutShiftEvents.length === 0) {
157
+ return;
159
158
  }
160
- const currentCluster = clusters[clusters.length - 1];
161
- const timeFromNavigation = currentShiftNavigation !== null ? Types.Timing.MicroSeconds(event.ts - navigations[currentShiftNavigation].ts) : void 0;
162
- currentCluster.clusterCumulativeScore += event.args.data ? event.args.data.weighted_score_delta : 0;
163
- if (!event.args.data) {
164
- continue;
165
- }
166
- const shift = {
167
- ...event,
168
- args: {
169
- frame: event.args.frame,
170
- data: {
171
- ...event.args.data,
172
- rawEvent: event
159
+ let firstShiftTime = layoutShiftEvents[0].ts;
160
+ let lastShiftTime = layoutShiftEvents[0].ts;
161
+ let lastShiftNavigation = null;
162
+ // Now step through each and create clusters.
163
+ // A cluster is equivalent to a session window (see https://web.dev/cls/#what-is-cls).
164
+ // To make the line chart clear, we explicitly demark the limits of each session window
165
+ // by starting the cumulative score of the window at the time of the first layout shift
166
+ // and ending it (dropping the line back to 0) when the window ends according to the
167
+ // thresholds (MAX_CLUSTER_DURATION, MAX_SHIFT_TIME_DELTA).
168
+ for (const event of layoutShiftEvents) {
169
+ // First detect if either the cluster duration or the #time between this and
170
+ // the last shift has been exceeded.
171
+ const clusterDurationExceeded = event.ts - firstShiftTime > MAX_CLUSTER_DURATION;
172
+ const maxTimeDeltaSinceLastShiftExceeded = event.ts - lastShiftTime > MAX_SHIFT_TIME_DELTA;
173
+ // Next take a look at navigations. If between this and the last shift we have navigated,
174
+ // note it.
175
+ const currentShiftNavigation = Platform.ArrayUtilities.nearestIndexFromEnd(navigations, nav => nav.ts < event.ts);
176
+ const hasNavigated = lastShiftNavigation !== currentShiftNavigation && currentShiftNavigation !== null;
177
+ // If any of the above criteria are met or if we don't have any cluster yet we should
178
+ // start a new one.
179
+ if (clusterDurationExceeded || maxTimeDeltaSinceLastShiftExceeded || hasNavigated || !clusters.length) {
180
+ // The cluster starts #time should be the timestamp of the first layout shift in it.
181
+ const clusterStartTime = event.ts;
182
+ // If the last session window ended because the max delta time between shifts
183
+ // was exceeded set the endtime to MAX_SHIFT_TIME_DELTA microseconds after the
184
+ // last shift in the session.
185
+ const endTimeByMaxSessionDuration = clusterDurationExceeded ? firstShiftTime + MAX_CLUSTER_DURATION : Infinity;
186
+ // If the last session window ended because the max session duration was
187
+ // surpassed, set the endtime so that the window length = MAX_CLUSTER_DURATION;
188
+ const endTimeByMaxShiftGap = maxTimeDeltaSinceLastShiftExceeded ? lastShiftTime + MAX_SHIFT_TIME_DELTA : Infinity;
189
+ // If there was a navigation during the last window, close it at the time
190
+ // of the navigation.
191
+ const endTimeByNavigation = hasNavigated ? navigations[currentShiftNavigation].ts : Infinity;
192
+ // End the previous cluster at the time of the first of the criteria above that was met.
193
+ const previousClusterEndTime = Math.min(endTimeByMaxSessionDuration, endTimeByMaxShiftGap, endTimeByNavigation);
194
+ // If there is an existing cluster update its closing time.
195
+ if (clusters.length > 0) {
196
+ const currentCluster = clusters[clusters.length - 1];
197
+ updateTraceWindowMax(currentCluster.clusterWindow, Types.Timing.MicroSeconds(previousClusterEndTime));
198
+ }
199
+ clusters.push({
200
+ events: [],
201
+ clusterWindow: traceWindowFromTime(clusterStartTime),
202
+ clusterCumulativeScore: 0,
203
+ scoreWindows: {
204
+ good: traceWindowFromTime(clusterStartTime),
205
+ needsImprovement: null,
206
+ bad: null,
207
+ },
208
+ });
209
+ firstShiftTime = clusterStartTime;
173
210
  }
174
- },
175
- parsedData: {
176
- timeFromNavigation,
177
- cumulativeWeightedScoreInWindow: currentCluster.clusterCumulativeScore,
178
- sessionWindowData: { cumulativeWindowScore: 0, id: clusters.length }
179
- }
180
- };
181
- currentCluster.events.push(shift);
182
- updateTraceWindowMax(currentCluster.clusterWindow, event.ts);
183
- lastShiftTime = event.ts;
184
- lastShiftNavigation = currentShiftNavigation;
185
- }
186
- for (const cluster of clusters) {
187
- let weightedScore = 0;
188
- let windowID = -1;
189
- if (cluster === clusters[clusters.length - 1]) {
190
- const clusterEndByMaxDuration = MAX_CLUSTER_DURATION + cluster.clusterWindow.min;
191
- const clusterEndByMaxGap = cluster.clusterWindow.max + MAX_SHIFT_TIME_DELTA;
192
- const nextNavigationIndex = Platform.ArrayUtilities.nearestIndexFromBeginning(navigations, (nav) => nav.ts > cluster.clusterWindow.max);
193
- const nextNavigationTime = nextNavigationIndex ? navigations[nextNavigationIndex].ts : Infinity;
194
- const clusterEnd = Math.min(clusterEndByMaxDuration, clusterEndByMaxGap, traceBounds.max, nextNavigationTime);
195
- updateTraceWindowMax(cluster.clusterWindow, Types.Timing.MicroSeconds(clusterEnd));
211
+ // Given the above we should have a cluster available, so pick the most
212
+ // recent one and append the shift, bump its score and window values accordingly.
213
+ const currentCluster = clusters[clusters.length - 1];
214
+ const timeFromNavigation = currentShiftNavigation !== null ?
215
+ Types.Timing.MicroSeconds(event.ts - navigations[currentShiftNavigation].ts) :
216
+ undefined;
217
+ currentCluster.clusterCumulativeScore += event.args.data ? event.args.data.weighted_score_delta : 0;
218
+ if (!event.args.data) {
219
+ continue;
220
+ }
221
+ const shift = {
222
+ ...event,
223
+ args: {
224
+ frame: event.args.frame,
225
+ data: {
226
+ ...event.args.data,
227
+ rawEvent: event,
228
+ },
229
+ },
230
+ parsedData: {
231
+ timeFromNavigation,
232
+ cumulativeWeightedScoreInWindow: currentCluster.clusterCumulativeScore,
233
+ // The score of the session window is temporarily set to 0 just
234
+ // to initialize it. Since we need to get the score of all shifts
235
+ // in the session window to determine its value, its definite
236
+ // value is set when stepping through the built clusters.
237
+ sessionWindowData: { cumulativeWindowScore: 0, id: clusters.length },
238
+ },
239
+ };
240
+ currentCluster.events.push(shift);
241
+ updateTraceWindowMax(currentCluster.clusterWindow, event.ts);
242
+ lastShiftTime = event.ts;
243
+ lastShiftNavigation = currentShiftNavigation;
196
244
  }
197
- for (const shift of cluster.events) {
198
- weightedScore += shift.args.data ? shift.args.data.weighted_score_delta : 0;
199
- windowID = shift.parsedData.sessionWindowData.id;
200
- const ts = shift.ts;
201
- shift.parsedData.sessionWindowData.cumulativeWindowScore = cluster.clusterCumulativeScore;
202
- if (weightedScore < LayoutShiftsThreshold.NEEDS_IMPROVEMENT) {
203
- updateTraceWindowMax(cluster.scoreWindows.good, ts);
204
- } else if (weightedScore >= LayoutShiftsThreshold.NEEDS_IMPROVEMENT && weightedScore < LayoutShiftsThreshold.BAD) {
205
- if (!cluster.scoreWindows.needsImprovement) {
206
- updateTraceWindowMax(cluster.scoreWindows.good, Types.Timing.MicroSeconds(ts - 1));
207
- cluster.scoreWindows.needsImprovement = traceWindowFromTime(ts);
245
+ // Now step through each cluster and set up the times at which the value
246
+ // goes from Good, to needs improvement, to Bad. Note that if there is a
247
+ // large jump we may go from Good to Bad without ever creating a Needs
248
+ // Improvement window at all.
249
+ for (const cluster of clusters) {
250
+ let weightedScore = 0;
251
+ let windowID = -1;
252
+ // If this is the last cluster update its window. The cluster duration is determined
253
+ // by the minimum between: time to next navigation, trace end time, time to maximum
254
+ // cluster duration and time to maximum gap between layout shifts.
255
+ if (cluster === clusters[clusters.length - 1]) {
256
+ const clusterEndByMaxDuration = MAX_CLUSTER_DURATION + cluster.clusterWindow.min;
257
+ const clusterEndByMaxGap = cluster.clusterWindow.max + MAX_SHIFT_TIME_DELTA;
258
+ const nextNavigationIndex = Platform.ArrayUtilities.nearestIndexFromBeginning(navigations, nav => nav.ts > cluster.clusterWindow.max);
259
+ const nextNavigationTime = nextNavigationIndex ? navigations[nextNavigationIndex].ts : Infinity;
260
+ const clusterEnd = Math.min(clusterEndByMaxDuration, clusterEndByMaxGap, traceBounds.max, nextNavigationTime);
261
+ updateTraceWindowMax(cluster.clusterWindow, Types.Timing.MicroSeconds(clusterEnd));
208
262
  }
209
- updateTraceWindowMax(cluster.scoreWindows.needsImprovement, ts);
210
- } else if (weightedScore >= LayoutShiftsThreshold.BAD) {
211
- if (!cluster.scoreWindows.bad) {
212
- if (cluster.scoreWindows.needsImprovement) {
213
- updateTraceWindowMax(cluster.scoreWindows.needsImprovement, Types.Timing.MicroSeconds(ts - 1));
214
- } else {
215
- updateTraceWindowMax(cluster.scoreWindows.good, Types.Timing.MicroSeconds(ts - 1));
216
- }
217
- cluster.scoreWindows.bad = traceWindowFromTime(shift.ts);
263
+ for (const shift of cluster.events) {
264
+ weightedScore += shift.args.data ? shift.args.data.weighted_score_delta : 0;
265
+ windowID = shift.parsedData.sessionWindowData.id;
266
+ const ts = shift.ts;
267
+ // Update the the CLS score of this shift's session window now that
268
+ // we have it.
269
+ shift.parsedData.sessionWindowData.cumulativeWindowScore = cluster.clusterCumulativeScore;
270
+ if (weightedScore < 0.1 /* LayoutShiftsThreshold.NEEDS_IMPROVEMENT */) {
271
+ // Expand the Good window.
272
+ updateTraceWindowMax(cluster.scoreWindows.good, ts);
273
+ }
274
+ else if (weightedScore >= 0.1 /* LayoutShiftsThreshold.NEEDS_IMPROVEMENT */ && weightedScore < 0.25 /* LayoutShiftsThreshold.BAD */) {
275
+ if (!cluster.scoreWindows.needsImprovement) {
276
+ // Close the Good window, and open the needs improvement window.
277
+ updateTraceWindowMax(cluster.scoreWindows.good, Types.Timing.MicroSeconds(ts - 1));
278
+ cluster.scoreWindows.needsImprovement = traceWindowFromTime(ts);
279
+ }
280
+ // Expand the needs improvement window.
281
+ updateTraceWindowMax(cluster.scoreWindows.needsImprovement, ts);
282
+ }
283
+ else if (weightedScore >= 0.25 /* LayoutShiftsThreshold.BAD */) {
284
+ if (!cluster.scoreWindows.bad) {
285
+ // We may jump from Good to Bad here, so update whichever window is open.
286
+ if (cluster.scoreWindows.needsImprovement) {
287
+ updateTraceWindowMax(cluster.scoreWindows.needsImprovement, Types.Timing.MicroSeconds(ts - 1));
288
+ }
289
+ else {
290
+ updateTraceWindowMax(cluster.scoreWindows.good, Types.Timing.MicroSeconds(ts - 1));
291
+ }
292
+ cluster.scoreWindows.bad = traceWindowFromTime(shift.ts);
293
+ }
294
+ // Expand the Bad window.
295
+ updateTraceWindowMax(cluster.scoreWindows.bad, ts);
296
+ }
297
+ // At this point the windows are set by the timestamps of the events, but the
298
+ // next cluster begins at the timestamp of its first event. As such we now
299
+ // need to expand the score window to the end of the cluster, and we do so
300
+ // by using the Bad widow if it's there, or the NI window, or finally the
301
+ // Good window.
302
+ if (cluster.scoreWindows.bad) {
303
+ updateTraceWindowMax(cluster.scoreWindows.bad, cluster.clusterWindow.max);
304
+ }
305
+ else if (cluster.scoreWindows.needsImprovement) {
306
+ updateTraceWindowMax(cluster.scoreWindows.needsImprovement, cluster.clusterWindow.max);
307
+ }
308
+ else {
309
+ updateTraceWindowMax(cluster.scoreWindows.good, cluster.clusterWindow.max);
310
+ }
311
+ }
312
+ if (weightedScore > sessionMaxScore) {
313
+ clsWindowID = windowID;
314
+ sessionMaxScore = weightedScore;
218
315
  }
219
- updateTraceWindowMax(cluster.scoreWindows.bad, ts);
220
- }
221
- if (cluster.scoreWindows.bad) {
222
- updateTraceWindowMax(cluster.scoreWindows.bad, cluster.clusterWindow.max);
223
- } else if (cluster.scoreWindows.needsImprovement) {
224
- updateTraceWindowMax(cluster.scoreWindows.needsImprovement, cluster.clusterWindow.max);
225
- } else {
226
- updateTraceWindowMax(cluster.scoreWindows.good, cluster.clusterWindow.max);
227
- }
228
- }
229
- if (weightedScore > sessionMaxScore) {
230
- clsWindowID = windowID;
231
- sessionMaxScore = weightedScore;
232
316
  }
233
- }
234
317
  }
235
318
  export function data() {
236
- if (handlerState !== HandlerState.FINALIZED) {
237
- throw new Error("Layout Shifts Handler is not finalized");
238
- }
239
- return {
240
- clusters: [...clusters],
241
- sessionMaxScore,
242
- clsWindowID,
243
- prePaintEvents: [...prePaintEvents],
244
- layoutInvalidationEvents: [...layoutInvalidationEvents],
245
- scheduleStyleInvalidationEvents: [...scheduleStyleInvalidationEvents],
246
- styleRecalcInvalidationEvents: [],
247
- scoreRecords: [...scoreRecords],
248
- backendNodeIds: [...backendNodeIds]
249
- };
319
+ if (handlerState !== 3 /* HandlerState.FINALIZED */) {
320
+ throw new Error('Layout Shifts Handler is not finalized');
321
+ }
322
+ return {
323
+ clusters: [...clusters],
324
+ sessionMaxScore: sessionMaxScore,
325
+ clsWindowID,
326
+ prePaintEvents: [...prePaintEvents],
327
+ layoutInvalidationEvents: [...layoutInvalidationEvents],
328
+ scheduleStyleInvalidationEvents: [...scheduleStyleInvalidationEvents],
329
+ styleRecalcInvalidationEvents: [],
330
+ scoreRecords: [...scoreRecords],
331
+ backendNodeIds: [...backendNodeIds],
332
+ };
250
333
  }
251
334
  export function deps() {
252
- return ["Screenshots", "Meta"];
335
+ return ['Screenshots', 'Meta'];
253
336
  }
254
337
  export function stateForLayoutShiftScore(score) {
255
- let state = ScoreClassification.GOOD;
256
- if (score >= LayoutShiftsThreshold.NEEDS_IMPROVEMENT) {
257
- state = ScoreClassification.OK;
258
- }
259
- if (score >= LayoutShiftsThreshold.BAD) {
260
- state = ScoreClassification.BAD;
261
- }
262
- return state;
338
+ let state = "good" /* ScoreClassification.GOOD */;
339
+ if (score >= 0.1 /* LayoutShiftsThreshold.NEEDS_IMPROVEMENT */) {
340
+ state = "ok" /* ScoreClassification.OK */;
341
+ }
342
+ if (score >= 0.25 /* LayoutShiftsThreshold.BAD */) {
343
+ state = "bad" /* ScoreClassification.BAD */;
344
+ }
345
+ return state;
263
346
  }
264
- export var LayoutShiftsThreshold = /* @__PURE__ */ ((LayoutShiftsThreshold2) => {
265
- LayoutShiftsThreshold2[LayoutShiftsThreshold2["GOOD"] = 0] = "GOOD";
266
- LayoutShiftsThreshold2[LayoutShiftsThreshold2["NEEDS_IMPROVEMENT"] = 0.1] = "NEEDS_IMPROVEMENT";
267
- LayoutShiftsThreshold2[LayoutShiftsThreshold2["BAD"] = 0.25] = "BAD";
268
- return LayoutShiftsThreshold2;
269
- })(LayoutShiftsThreshold || {});
270
- //# sourceMappingURL=LayoutShiftsHandler.js.map
347
+ //# sourceMappingURL=LayoutShiftsHandler.js.map