@paulirish/trace_engine 0.0.25 → 0.0.27

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 (176) hide show
  1. package/core/platform/TypedArrayUtilities.d.ts +7 -0
  2. package/core/platform/TypedArrayUtilities.js +41 -0
  3. package/core/platform/TypedArrayUtilities.js.map +1 -1
  4. package/core/platform/devtools_entrypoint-bundle-typescript-tsconfig.json +0 -1
  5. package/core/platform/platform-tsconfig.json +0 -1
  6. package/generated/protocol.d.ts +36 -2
  7. package/models/cpu_profile/cpu_profile-tsconfig.json +0 -1
  8. package/models/cpu_profile/devtools_entrypoint-bundle-typescript-tsconfig.json +0 -1
  9. package/models/trace/LanternComputationData.d.ts +8 -0
  10. package/models/trace/LanternComputationData.js +368 -0
  11. package/models/trace/LanternComputationData.js.map +1 -0
  12. package/models/trace/devtools_entrypoint-bundle-typescript-tsconfig.json +0 -1
  13. package/models/trace/extras/devtools_entrypoint-bundle-typescript-tsconfig.json +0 -1
  14. package/models/trace/extras/extras-tsconfig.json +0 -1
  15. package/models/trace/handlers/EnhancedTracesHandler.d.ts +46 -0
  16. package/models/trace/handlers/EnhancedTracesHandler.js +137 -0
  17. package/models/trace/handlers/EnhancedTracesHandler.js.map +1 -0
  18. package/models/trace/handlers/LayoutShiftsHandler.d.ts +1 -1
  19. package/models/trace/handlers/LayoutShiftsHandler.js +1 -1
  20. package/models/trace/handlers/LayoutShiftsHandler.js.map +1 -1
  21. package/models/trace/handlers/ModelHandlers.d.ts +1 -0
  22. package/models/trace/handlers/ModelHandlers.js +1 -0
  23. package/models/trace/handlers/ModelHandlers.js.map +1 -1
  24. package/models/trace/handlers/UserInteractionsHandler.d.ts +6 -0
  25. package/models/trace/handlers/UserInteractionsHandler.js +15 -0
  26. package/models/trace/handlers/UserInteractionsHandler.js.map +1 -1
  27. package/models/trace/handlers/devtools_entrypoint-bundle-typescript-tsconfig.json +0 -1
  28. package/models/trace/handlers/handlers-tsconfig.json +1 -1
  29. package/models/trace/helpers/Timing.d.ts +0 -6
  30. package/models/trace/helpers/Timing.js +0 -76
  31. package/models/trace/helpers/Timing.js.map +1 -1
  32. package/models/trace/helpers/devtools_entrypoint-bundle-typescript-tsconfig.json +0 -1
  33. package/models/trace/helpers/helpers-tsconfig.json +0 -1
  34. package/models/trace/insights/devtools_entrypoint-bundle-typescript-tsconfig.json +0 -1
  35. package/models/trace/insights/insights-tsconfig.json +0 -1
  36. package/models/trace/lantern/BaseNode.d.ts +91 -0
  37. package/models/trace/lantern/BaseNode.js +268 -0
  38. package/models/trace/lantern/BaseNode.js.map +1 -0
  39. package/models/trace/lantern/CPUNode.d.ts +24 -0
  40. package/models/trace/lantern/CPUNode.js +64 -0
  41. package/models/trace/lantern/CPUNode.js.map +1 -0
  42. package/models/trace/lantern/LanternError.d.ts +3 -0
  43. package/models/trace/lantern/LanternError.js +7 -0
  44. package/models/trace/lantern/LanternError.js.map +1 -0
  45. package/models/trace/lantern/MetricsModule.d.ts +11 -0
  46. package/models/trace/lantern/MetricsModule.js +14 -0
  47. package/models/trace/lantern/MetricsModule.js.map +1 -0
  48. package/models/trace/lantern/NetworkNode.d.ts +22 -0
  49. package/models/trace/lantern/NetworkNode.js +83 -0
  50. package/models/trace/lantern/NetworkNode.js.map +1 -0
  51. package/models/trace/lantern/PageDependencyGraph.d.ts +43 -0
  52. package/models/trace/lantern/PageDependencyGraph.js +509 -0
  53. package/models/trace/lantern/PageDependencyGraph.js.map +1 -0
  54. package/models/trace/lantern/SimulationModule.d.ts +17 -0
  55. package/models/trace/lantern/SimulationModule.js +13 -0
  56. package/models/trace/lantern/SimulationModule.js.map +1 -0
  57. package/models/trace/lantern/bundle-tsconfig.json +1 -0
  58. package/models/trace/lantern/core/LanternError.d.ts +3 -0
  59. package/models/trace/lantern/core/LanternError.js +7 -0
  60. package/models/trace/lantern/core/LanternError.js.map +1 -0
  61. package/models/trace/lantern/core/NetworkAnalyzer.d.ts +112 -0
  62. package/models/trace/lantern/core/NetworkAnalyzer.js +486 -0
  63. package/models/trace/lantern/core/NetworkAnalyzer.js.map +1 -0
  64. package/models/trace/lantern/core/bundle-tsconfig.json +1 -0
  65. package/models/trace/lantern/core/core-tsconfig.json +43 -0
  66. package/models/trace/lantern/core/core.d.ts +2 -0
  67. package/models/trace/lantern/core/core.js +6 -0
  68. package/models/trace/lantern/core/core.js.map +1 -0
  69. package/models/trace/lantern/core/devtools_entrypoint-bundle-typescript-tsconfig.json +42 -0
  70. package/models/trace/lantern/devtools_entrypoint-bundle-typescript-tsconfig.json +42 -0
  71. package/models/trace/lantern/graph/BaseNode.d.ts +91 -0
  72. package/models/trace/lantern/graph/BaseNode.js +268 -0
  73. package/models/trace/lantern/graph/BaseNode.js.map +1 -0
  74. package/models/trace/lantern/graph/CPUNode.d.ts +24 -0
  75. package/models/trace/lantern/graph/CPUNode.js +64 -0
  76. package/models/trace/lantern/graph/CPUNode.js.map +1 -0
  77. package/models/trace/lantern/graph/NetworkNode.d.ts +22 -0
  78. package/models/trace/lantern/graph/NetworkNode.js +83 -0
  79. package/models/trace/lantern/graph/NetworkNode.js.map +1 -0
  80. package/models/trace/lantern/graph/PageDependencyGraph.d.ts +43 -0
  81. package/models/trace/lantern/graph/PageDependencyGraph.js +509 -0
  82. package/models/trace/lantern/graph/PageDependencyGraph.js.map +1 -0
  83. package/models/trace/lantern/graph/bundle-tsconfig.json +1 -0
  84. package/models/trace/lantern/graph/devtools_entrypoint-bundle-typescript-tsconfig.json +42 -0
  85. package/models/trace/lantern/graph/graph-tsconfig.json +48 -0
  86. package/models/trace/lantern/graph/graph.d.ts +4 -0
  87. package/models/trace/lantern/graph/graph.js +8 -0
  88. package/models/trace/lantern/graph/graph.js.map +1 -0
  89. package/models/trace/lantern/lantern-tsconfig.json +53 -0
  90. package/models/trace/lantern/lantern.d.ts +6 -0
  91. package/models/trace/lantern/lantern.js +10 -0
  92. package/models/trace/lantern/lantern.js.map +1 -0
  93. package/models/trace/lantern/metrics/FirstContentfulPaint.d.ts +40 -0
  94. package/models/trace/lantern/metrics/FirstContentfulPaint.js +137 -0
  95. package/models/trace/lantern/metrics/FirstContentfulPaint.js.map +1 -0
  96. package/models/trace/lantern/metrics/Interactive.d.ts +12 -0
  97. package/models/trace/lantern/metrics/Interactive.js +67 -0
  98. package/models/trace/lantern/metrics/Interactive.js.map +1 -0
  99. package/models/trace/lantern/metrics/LargestContentfulPaint.d.ts +17 -0
  100. package/models/trace/lantern/metrics/LargestContentfulPaint.js +69 -0
  101. package/models/trace/lantern/metrics/LargestContentfulPaint.js.map +1 -0
  102. package/models/trace/lantern/metrics/MaxPotentialFID.d.ts +14 -0
  103. package/models/trace/lantern/metrics/MaxPotentialFID.js +48 -0
  104. package/models/trace/lantern/metrics/MaxPotentialFID.js.map +1 -0
  105. package/models/trace/lantern/metrics/Metric.d.ts +44 -0
  106. package/models/trace/lantern/metrics/Metric.js +70 -0
  107. package/models/trace/lantern/metrics/Metric.js.map +1 -0
  108. package/models/trace/lantern/metrics/SpeedIndex.d.ts +25 -0
  109. package/models/trace/lantern/metrics/SpeedIndex.js +101 -0
  110. package/models/trace/lantern/metrics/SpeedIndex.js.map +1 -0
  111. package/models/trace/lantern/metrics/TBTUtils.d.ts +31 -0
  112. package/models/trace/lantern/metrics/TBTUtils.js +65 -0
  113. package/models/trace/lantern/metrics/TBTUtils.js.map +1 -0
  114. package/models/trace/lantern/metrics/TotalBlockingTime.d.ts +16 -0
  115. package/models/trace/lantern/metrics/TotalBlockingTime.js +78 -0
  116. package/models/trace/lantern/metrics/TotalBlockingTime.js.map +1 -0
  117. package/models/trace/lantern/metrics/bundle-tsconfig.json +1 -0
  118. package/models/trace/lantern/metrics/devtools_entrypoint-bundle-typescript-tsconfig.json +42 -0
  119. package/models/trace/lantern/metrics/metrics-tsconfig.json +58 -0
  120. package/models/trace/lantern/metrics/metrics.d.ts +8 -0
  121. package/models/trace/lantern/metrics/metrics.js +12 -0
  122. package/models/trace/lantern/metrics/metrics.js.map +1 -0
  123. package/models/trace/lantern/simulation/ConnectionPool.d.ts +26 -0
  124. package/models/trace/lantern/simulation/ConnectionPool.js +116 -0
  125. package/models/trace/lantern/simulation/ConnectionPool.js.map +1 -0
  126. package/models/trace/lantern/simulation/Constants.d.ts +31 -0
  127. package/models/trace/lantern/simulation/Constants.js +43 -0
  128. package/models/trace/lantern/simulation/Constants.js.map +1 -0
  129. package/models/trace/lantern/simulation/DNSCache.d.ts +22 -0
  130. package/models/trace/lantern/simulation/DNSCache.js +48 -0
  131. package/models/trace/lantern/simulation/DNSCache.js.map +1 -0
  132. package/models/trace/lantern/simulation/NetworkAnalyzer.d.ts +112 -0
  133. package/models/trace/lantern/simulation/NetworkAnalyzer.js +486 -0
  134. package/models/trace/lantern/simulation/NetworkAnalyzer.js.map +1 -0
  135. package/models/trace/lantern/simulation/SimulationTimingMap.d.ts +69 -0
  136. package/models/trace/lantern/simulation/SimulationTimingMap.js +134 -0
  137. package/models/trace/lantern/simulation/SimulationTimingMap.js.map +1 -0
  138. package/models/trace/lantern/simulation/Simulator.d.ts +84 -0
  139. package/models/trace/lantern/simulation/Simulator.js +449 -0
  140. package/models/trace/lantern/simulation/Simulator.js.map +1 -0
  141. package/models/trace/lantern/simulation/TCPConnection.d.ts +48 -0
  142. package/models/trace/lantern/simulation/TCPConnection.js +158 -0
  143. package/models/trace/lantern/simulation/TCPConnection.js.map +1 -0
  144. package/models/trace/lantern/simulation/bundle-tsconfig.json +1 -0
  145. package/models/trace/lantern/simulation/devtools_entrypoint-bundle-typescript-tsconfig.json +42 -0
  146. package/models/trace/lantern/simulation/simulation-tsconfig.json +50 -0
  147. package/models/trace/lantern/simulation/simulation.d.ts +6 -0
  148. package/models/trace/lantern/simulation/simulation.js +10 -0
  149. package/models/trace/lantern/simulation/simulation.js.map +1 -0
  150. package/models/trace/lantern/types/bundle-tsconfig.json +1 -0
  151. package/models/trace/lantern/types/devtools_entrypoint-bundle-typescript-tsconfig.json +42 -0
  152. package/models/trace/lantern/types/lantern.d.ts +199 -0
  153. package/models/trace/lantern/types/lantern.js +24 -0
  154. package/models/trace/lantern/types/lantern.js.map +1 -0
  155. package/models/trace/lantern/types/types-tsconfig.json +42 -0
  156. package/models/trace/lantern/types/types.d.ts +1 -0
  157. package/models/trace/lantern/types/types.js +5 -0
  158. package/models/trace/lantern/types/types.js.map +1 -0
  159. package/models/trace/root-causes/devtools_entrypoint-bundle-typescript-tsconfig.json +0 -1
  160. package/models/trace/root-causes/root-causes-tsconfig.json +0 -1
  161. package/models/trace/trace-tsconfig.json +4 -1
  162. package/models/trace/trace.d.ts +3 -1
  163. package/models/trace/trace.js +3 -1
  164. package/models/trace/trace.js.map +1 -1
  165. package/models/trace/types/Extensions.d.ts +2 -3
  166. package/models/trace/types/Extensions.js +2 -11
  167. package/models/trace/types/Extensions.js.map +1 -1
  168. package/models/trace/types/File.d.ts +1 -0
  169. package/models/trace/types/File.js.map +1 -1
  170. package/models/trace/types/TraceEvents.d.ts +49 -0
  171. package/models/trace/types/TraceEvents.js +33 -0
  172. package/models/trace/types/TraceEvents.js.map +1 -1
  173. package/models/trace/types/devtools_entrypoint-bundle-typescript-tsconfig.json +0 -1
  174. package/models/trace/types/types-tsconfig.json +0 -1
  175. package/package.json +1 -1
  176. package/PAUL.readme.md +0 -5
@@ -0,0 +1,449 @@
1
+ // Copyright 2024 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 Graph from '../graph/graph.js';
5
+ import { ConnectionPool } from './ConnectionPool.js';
6
+ import { Constants } from './Constants.js';
7
+ import { DNSCache } from './DNSCache.js';
8
+ import { SimulatorTimingMap } from './SimulationTimingMap.js';
9
+ import { TCPConnection } from './TCPConnection.js';
10
+ const defaultThrottling = Constants.throttling.mobileSlow4G;
11
+ // see https://cs.chromium.org/search/?q=kDefaultMaxNumDelayableRequestsPerClient&sq=package:chromium&type=cs
12
+ const DEFAULT_MAXIMUM_CONCURRENT_REQUESTS = 10;
13
+ // layout tasks tend to be less CPU-bound and do not experience the same increase in duration
14
+ const DEFAULT_LAYOUT_TASK_MULTIPLIER = 0.5;
15
+ // if a task takes more than 10 seconds it's usually a sign it isn't actually CPU bound and we're overestimating
16
+ const DEFAULT_MAXIMUM_CPU_TASK_DURATION = 10000;
17
+ const NodeState = {
18
+ NotReadyToStart: 0,
19
+ ReadyToStart: 1,
20
+ InProgress: 2,
21
+ Complete: 3,
22
+ };
23
+ const PriorityStartTimePenalty = {
24
+ VeryHigh: 0,
25
+ High: 0.25,
26
+ Medium: 0.5,
27
+ Low: 1,
28
+ VeryLow: 2,
29
+ };
30
+ const ALL_SIMULATION_NODE_TIMINGS = new Map();
31
+ class Simulator {
32
+ static createSimulator(settings) {
33
+ const { throttlingMethod, throttling, precomputedLanternData, networkAnalysis } = settings;
34
+ const options = {
35
+ additionalRttByOrigin: networkAnalysis.additionalRttByOrigin,
36
+ serverResponseTimeByOrigin: networkAnalysis.serverResponseTimeByOrigin,
37
+ observedThroughput: networkAnalysis.throughput,
38
+ };
39
+ // If we have precomputed lantern data, overwrite our observed estimates and use precomputed instead
40
+ // for increased stability.
41
+ if (precomputedLanternData) {
42
+ options.additionalRttByOrigin = new Map(Object.entries(precomputedLanternData.additionalRttByOrigin));
43
+ options.serverResponseTimeByOrigin = new Map(Object.entries(precomputedLanternData.serverResponseTimeByOrigin));
44
+ }
45
+ switch (throttlingMethod) {
46
+ case 'provided':
47
+ options.rtt = networkAnalysis.rtt;
48
+ options.throughput = networkAnalysis.throughput;
49
+ options.cpuSlowdownMultiplier = 1;
50
+ options.layoutTaskMultiplier = 1;
51
+ break;
52
+ case 'devtools':
53
+ if (throttling) {
54
+ options.rtt = throttling.requestLatencyMs / Constants.throttling.DEVTOOLS_RTT_ADJUSTMENT_FACTOR;
55
+ options.throughput =
56
+ throttling.downloadThroughputKbps * 1024 / Constants.throttling.DEVTOOLS_THROUGHPUT_ADJUSTMENT_FACTOR;
57
+ }
58
+ options.cpuSlowdownMultiplier = 1;
59
+ options.layoutTaskMultiplier = 1;
60
+ break;
61
+ case 'simulate':
62
+ if (throttling) {
63
+ options.rtt = throttling.rttMs;
64
+ options.throughput = throttling.throughputKbps * 1024;
65
+ options.cpuSlowdownMultiplier = throttling.cpuSlowdownMultiplier;
66
+ }
67
+ break;
68
+ default:
69
+ // intentionally fallback to simulator defaults
70
+ break;
71
+ }
72
+ return new Simulator(options);
73
+ }
74
+ _options;
75
+ _rtt;
76
+ _throughput;
77
+ _maximumConcurrentRequests;
78
+ _cpuSlowdownMultiplier;
79
+ _layoutTaskMultiplier;
80
+ _cachedNodeListByStartPosition;
81
+ _nodeTimings;
82
+ _numberInProgressByType;
83
+ _nodes;
84
+ _dns;
85
+ _connectionPool;
86
+ constructor(options) {
87
+ this._options = Object.assign({
88
+ rtt: defaultThrottling.rttMs,
89
+ throughput: defaultThrottling.throughputKbps * 1024,
90
+ maximumConcurrentRequests: DEFAULT_MAXIMUM_CONCURRENT_REQUESTS,
91
+ cpuSlowdownMultiplier: defaultThrottling.cpuSlowdownMultiplier,
92
+ layoutTaskMultiplier: DEFAULT_LAYOUT_TASK_MULTIPLIER,
93
+ additionalRttByOrigin: new Map(),
94
+ serverResponseTimeByOrigin: new Map(),
95
+ }, options);
96
+ this._rtt = this._options.rtt;
97
+ this._throughput = this._options.throughput;
98
+ this._maximumConcurrentRequests = Math.max(Math.min(TCPConnection.maximumSaturatedConnections(this._rtt, this._throughput), this._options.maximumConcurrentRequests), 1);
99
+ this._cpuSlowdownMultiplier = this._options.cpuSlowdownMultiplier;
100
+ this._layoutTaskMultiplier = this._cpuSlowdownMultiplier * this._options.layoutTaskMultiplier;
101
+ this._cachedNodeListByStartPosition = [];
102
+ // Properties reset on every `.simulate` call but duplicated here for type checking
103
+ this._nodeTimings = new SimulatorTimingMap();
104
+ this._numberInProgressByType = new Map();
105
+ this._nodes = {};
106
+ this._dns = new DNSCache({ rtt: this._rtt });
107
+ // @ts-expect-error
108
+ this._connectionPool = null;
109
+ if (!Number.isFinite(this._rtt)) {
110
+ throw new Error(`Invalid rtt ${this._rtt}`);
111
+ }
112
+ if (!Number.isFinite(this._throughput)) {
113
+ throw new Error(`Invalid rtt ${this._throughput}`);
114
+ }
115
+ }
116
+ get rtt() {
117
+ return this._rtt;
118
+ }
119
+ _initializeConnectionPool(graph) {
120
+ const records = [];
121
+ graph.getRootNode().traverse(node => {
122
+ if (node.type === Graph.BaseNode.types.NETWORK) {
123
+ records.push(node.request);
124
+ }
125
+ });
126
+ this._connectionPool = new ConnectionPool(records, this._options);
127
+ }
128
+ /**
129
+ * Initializes the various state data structures such _nodeTimings and the _node Sets by state.
130
+ */
131
+ _initializeAuxiliaryData() {
132
+ this._nodeTimings = new SimulatorTimingMap();
133
+ this._numberInProgressByType = new Map();
134
+ this._nodes = {};
135
+ this._cachedNodeListByStartPosition = [];
136
+ // NOTE: We don't actually need *all* of these sets, but the clarity that each node progresses
137
+ // through the system is quite nice.
138
+ for (const state of Object.values(NodeState)) {
139
+ this._nodes[state] = new Set();
140
+ }
141
+ }
142
+ _numberInProgress(type) {
143
+ return this._numberInProgressByType.get(type) || 0;
144
+ }
145
+ _markNodeAsReadyToStart(node, queuedTime) {
146
+ const nodeStartPosition = Simulator._computeNodeStartPosition(node);
147
+ const firstNodeIndexWithGreaterStartPosition = this._cachedNodeListByStartPosition.findIndex(candidate => Simulator._computeNodeStartPosition(candidate) > nodeStartPosition);
148
+ const insertionIndex = firstNodeIndexWithGreaterStartPosition === -1 ? this._cachedNodeListByStartPosition.length :
149
+ firstNodeIndexWithGreaterStartPosition;
150
+ this._cachedNodeListByStartPosition.splice(insertionIndex, 0, node);
151
+ this._nodes[NodeState.ReadyToStart].add(node);
152
+ this._nodes[NodeState.NotReadyToStart].delete(node);
153
+ this._nodeTimings.setReadyToStart(node, { queuedTime });
154
+ }
155
+ _markNodeAsInProgress(node, startTime) {
156
+ const indexOfNodeToStart = this._cachedNodeListByStartPosition.indexOf(node);
157
+ this._cachedNodeListByStartPosition.splice(indexOfNodeToStart, 1);
158
+ this._nodes[NodeState.InProgress].add(node);
159
+ this._nodes[NodeState.ReadyToStart].delete(node);
160
+ this._numberInProgressByType.set(node.type, this._numberInProgress(node.type) + 1);
161
+ this._nodeTimings.setInProgress(node, { startTime });
162
+ }
163
+ _markNodeAsComplete(node, endTime, connectionTiming) {
164
+ this._nodes[NodeState.Complete].add(node);
165
+ this._nodes[NodeState.InProgress].delete(node);
166
+ this._numberInProgressByType.set(node.type, this._numberInProgress(node.type) - 1);
167
+ this._nodeTimings.setCompleted(node, { endTime, connectionTiming });
168
+ // Try to add all its dependents to the queue
169
+ for (const dependent of node.getDependents()) {
170
+ // Skip dependent node if one of its dependencies hasn't finished yet
171
+ const dependencies = dependent.getDependencies();
172
+ if (dependencies.some(dep => !this._nodes[NodeState.Complete].has(dep))) {
173
+ continue;
174
+ }
175
+ // Otherwise add it to the queue
176
+ this._markNodeAsReadyToStart(dependent, endTime);
177
+ }
178
+ }
179
+ _acquireConnection(request) {
180
+ return this._connectionPool.acquire(request);
181
+ }
182
+ _getNodesSortedByStartPosition() {
183
+ // Make a copy so we don't skip nodes due to concurrent modification
184
+ return Array.from(this._cachedNodeListByStartPosition);
185
+ }
186
+ _startNodeIfPossible(node, totalElapsedTime) {
187
+ if (node.type === Graph.BaseNode.types.CPU) {
188
+ // Start a CPU task if there's no other CPU task in process
189
+ if (this._numberInProgress(node.type) === 0) {
190
+ this._markNodeAsInProgress(node, totalElapsedTime);
191
+ }
192
+ return;
193
+ }
194
+ if (node.type !== Graph.BaseNode.types.NETWORK) {
195
+ throw new Error('Unsupported');
196
+ }
197
+ // If a network request is connectionless, we can always start it, so skip the connection checks
198
+ if (!node.isConnectionless) {
199
+ // Start a network request if we're not at max requests and a connection is available
200
+ const numberOfActiveRequests = this._numberInProgress(node.type);
201
+ if (numberOfActiveRequests >= this._maximumConcurrentRequests) {
202
+ return;
203
+ }
204
+ const connection = this._acquireConnection(node.request);
205
+ if (!connection) {
206
+ return;
207
+ }
208
+ }
209
+ this._markNodeAsInProgress(node, totalElapsedTime);
210
+ }
211
+ /**
212
+ * Updates each connection in use with the available throughput based on the number of network requests
213
+ * currently in flight.
214
+ */
215
+ _updateNetworkCapacity() {
216
+ const inFlight = this._numberInProgress(Graph.BaseNode.types.NETWORK);
217
+ if (inFlight === 0) {
218
+ return;
219
+ }
220
+ for (const connection of this._connectionPool.connectionsInUse()) {
221
+ connection.setThroughput(this._throughput / inFlight);
222
+ }
223
+ }
224
+ /**
225
+ * Estimates the number of milliseconds remaining given current condidtions before the node is complete.
226
+ */
227
+ _estimateTimeRemaining(node) {
228
+ if (node.type === Graph.BaseNode.types.CPU) {
229
+ return this._estimateCPUTimeRemaining(node);
230
+ }
231
+ if (node.type === Graph.BaseNode.types.NETWORK) {
232
+ return this._estimateNetworkTimeRemaining(node);
233
+ }
234
+ throw new Error('Unsupported');
235
+ }
236
+ _estimateCPUTimeRemaining(cpuNode) {
237
+ const timingData = this._nodeTimings.getCpuStarted(cpuNode);
238
+ const multiplier = cpuNode.didPerformLayout() ? this._layoutTaskMultiplier : this._cpuSlowdownMultiplier;
239
+ const totalDuration = Math.min(Math.round(cpuNode.duration / 1000 * multiplier), DEFAULT_MAXIMUM_CPU_TASK_DURATION);
240
+ const estimatedTimeElapsed = totalDuration - timingData.timeElapsed;
241
+ this._nodeTimings.setCpuEstimated(cpuNode, { estimatedTimeElapsed });
242
+ return estimatedTimeElapsed;
243
+ }
244
+ _estimateNetworkTimeRemaining(networkNode) {
245
+ const request = networkNode.request;
246
+ const timingData = this._nodeTimings.getNetworkStarted(networkNode);
247
+ let timeElapsed = 0;
248
+ if (networkNode.fromDiskCache) {
249
+ // Rough access time for seeking to location on disk and reading sequentially.
250
+ // 8ms per seek + 20ms/MB
251
+ // @see http://norvig.com/21-days.html#answers
252
+ const sizeInMb = (request.resourceSize || 0) / 1024 / 1024;
253
+ timeElapsed = 8 + 20 * sizeInMb - timingData.timeElapsed;
254
+ }
255
+ else if (networkNode.isNonNetworkProtocol) {
256
+ // Estimates for the overhead of a data URL in Chromium and the decoding time for base64-encoded data.
257
+ // 2ms per request + 10ms/MB
258
+ // @see traces on https://dopiaza.org/tools/datauri/examples/index.php
259
+ const sizeInMb = (request.resourceSize || 0) / 1024 / 1024;
260
+ timeElapsed = 2 + 10 * sizeInMb - timingData.timeElapsed;
261
+ }
262
+ else {
263
+ const connection = this._connectionPool.acquireActiveConnectionFromRequest(request);
264
+ const dnsResolutionTime = this._dns.getTimeUntilResolution(request, {
265
+ requestedAt: timingData.startTime,
266
+ shouldUpdateCache: true,
267
+ });
268
+ const timeAlreadyElapsed = timingData.timeElapsed;
269
+ const calculation = connection.simulateDownloadUntil(request.transferSize - timingData.bytesDownloaded, { timeAlreadyElapsed, dnsResolutionTime, maximumTimeToElapse: Infinity });
270
+ timeElapsed = calculation.timeElapsed;
271
+ }
272
+ const estimatedTimeElapsed = timeElapsed + timingData.timeElapsedOvershoot;
273
+ this._nodeTimings.setNetworkEstimated(networkNode, { estimatedTimeElapsed });
274
+ return estimatedTimeElapsed;
275
+ }
276
+ /**
277
+ * Computes and returns the minimum estimated completion time of the nodes currently in progress.
278
+ */
279
+ _findNextNodeCompletionTime() {
280
+ let minimumTime = Infinity;
281
+ for (const node of this._nodes[NodeState.InProgress]) {
282
+ minimumTime = Math.min(minimumTime, this._estimateTimeRemaining(node));
283
+ }
284
+ return minimumTime;
285
+ }
286
+ /**
287
+ * Given a time period, computes the progress toward completion that the node made durin that time.
288
+ */
289
+ _updateProgressMadeInTimePeriod(node, timePeriodLength, totalElapsedTime) {
290
+ const timingData = this._nodeTimings.getInProgress(node);
291
+ const isFinished = timingData.estimatedTimeElapsed === timePeriodLength;
292
+ if (node.type === Graph.BaseNode.types.CPU || node.isConnectionless) {
293
+ if (isFinished) {
294
+ this._markNodeAsComplete(node, totalElapsedTime);
295
+ }
296
+ else {
297
+ timingData.timeElapsed += timePeriodLength;
298
+ }
299
+ return;
300
+ }
301
+ if (node.type !== Graph.BaseNode.types.NETWORK) {
302
+ throw new Error('Unsupported');
303
+ }
304
+ if (!('bytesDownloaded' in timingData)) {
305
+ throw new Error('Invalid timing data');
306
+ }
307
+ const request = node.request;
308
+ const connection = this._connectionPool.acquireActiveConnectionFromRequest(request);
309
+ const dnsResolutionTime = this._dns.getTimeUntilResolution(request, {
310
+ requestedAt: timingData.startTime,
311
+ shouldUpdateCache: true,
312
+ });
313
+ const calculation = connection.simulateDownloadUntil(request.transferSize - timingData.bytesDownloaded, {
314
+ dnsResolutionTime,
315
+ timeAlreadyElapsed: timingData.timeElapsed,
316
+ maximumTimeToElapse: timePeriodLength - timingData.timeElapsedOvershoot,
317
+ });
318
+ connection.setCongestionWindow(calculation.congestionWindow);
319
+ connection.setH2OverflowBytesDownloaded(calculation.extraBytesDownloaded);
320
+ if (isFinished) {
321
+ connection.setWarmed(true);
322
+ this._connectionPool.release(request);
323
+ this._markNodeAsComplete(node, totalElapsedTime, calculation.connectionTiming);
324
+ }
325
+ else {
326
+ timingData.timeElapsed += calculation.timeElapsed;
327
+ timingData.timeElapsedOvershoot += calculation.timeElapsed - timePeriodLength;
328
+ timingData.bytesDownloaded += calculation.bytesDownloaded;
329
+ }
330
+ }
331
+ _computeFinalNodeTimings() {
332
+ const completeNodeTimingEntries = this._nodeTimings.getNodes().map(node => {
333
+ return [node, this._nodeTimings.getCompleted(node)];
334
+ });
335
+ // Most consumers will want the entries sorted by startTime, so insert them in that order
336
+ completeNodeTimingEntries.sort((a, b) => a[1].startTime - b[1].startTime);
337
+ // Trimmed version of type `Lantern.Simulation.NodeTiming`.
338
+ const nodeTimingEntries = completeNodeTimingEntries.map(([node, timing]) => {
339
+ return [
340
+ node,
341
+ {
342
+ startTime: timing.startTime,
343
+ endTime: timing.endTime,
344
+ duration: timing.endTime - timing.startTime,
345
+ },
346
+ ];
347
+ });
348
+ return {
349
+ nodeTimings: new Map(nodeTimingEntries),
350
+ completeNodeTimings: new Map(completeNodeTimingEntries),
351
+ };
352
+ }
353
+ getOptions() {
354
+ return this._options;
355
+ }
356
+ /**
357
+ * Estimates the time taken to process all of the graph's nodes, returns the overall time along with
358
+ * each node annotated by start/end times.
359
+ *
360
+ * Simulator/connection pool are allowed to deviate from what was
361
+ * observed in the trace/devtoolsLog and start requests as soon as they are queued (i.e. do not
362
+ * wait around for a warm connection to be available if the original request was fetched on a warm
363
+ * connection).
364
+ */
365
+ simulate(graph, options) {
366
+ if (Graph.BaseNode.hasCycle(graph)) {
367
+ throw new Error('Cannot simulate graph with cycle');
368
+ }
369
+ options = Object.assign({
370
+ label: undefined,
371
+ }, options);
372
+ // initialize the necessary data containers
373
+ this._dns = new DNSCache({ rtt: this._rtt });
374
+ this._initializeConnectionPool(graph);
375
+ this._initializeAuxiliaryData();
376
+ const nodesNotReadyToStart = this._nodes[NodeState.NotReadyToStart];
377
+ const nodesReadyToStart = this._nodes[NodeState.ReadyToStart];
378
+ const nodesInProgress = this._nodes[NodeState.InProgress];
379
+ const rootNode = graph.getRootNode();
380
+ rootNode.traverse(node => nodesNotReadyToStart.add(node));
381
+ let totalElapsedTime = 0;
382
+ let iteration = 0;
383
+ // root node is always ready to start
384
+ this._markNodeAsReadyToStart(rootNode, totalElapsedTime);
385
+ // loop as long as we have nodes in the queue or currently in progress
386
+ while (nodesReadyToStart.size || nodesInProgress.size) {
387
+ // move all possible queued nodes to in progress
388
+ for (const node of this._getNodesSortedByStartPosition()) {
389
+ this._startNodeIfPossible(node, totalElapsedTime);
390
+ }
391
+ if (!nodesInProgress.size) {
392
+ // Interplay between fromDiskCache and connectionReused can be incorrect,
393
+ // have to give up.
394
+ throw new Error('Failed to start a node');
395
+ }
396
+ // set the available throughput for all connections based on # inflight
397
+ this._updateNetworkCapacity();
398
+ // find the time that the next node will finish
399
+ const minimumTime = this._findNextNodeCompletionTime();
400
+ totalElapsedTime += minimumTime;
401
+ // While this is no longer strictly necessary, it's always better than hanging
402
+ if (!Number.isFinite(minimumTime) || iteration > 100000) {
403
+ throw new Error('Simulation failed, depth exceeded');
404
+ }
405
+ iteration++;
406
+ // update how far each node will progress until that point
407
+ for (const node of nodesInProgress) {
408
+ this._updateProgressMadeInTimePeriod(node, minimumTime, totalElapsedTime);
409
+ }
410
+ }
411
+ // `nodeTimings` are used for simulator consumers, `completeNodeTimings` kept for debugging.
412
+ const { nodeTimings, completeNodeTimings } = this._computeFinalNodeTimings();
413
+ ALL_SIMULATION_NODE_TIMINGS.set(options.label || 'unlabeled', completeNodeTimings);
414
+ return {
415
+ timeInMs: totalElapsedTime,
416
+ nodeTimings,
417
+ };
418
+ }
419
+ computeWastedMsFromWastedBytes(wastedBytes) {
420
+ const { throughput, observedThroughput } = this._options;
421
+ // https://github.com/GoogleChrome/lighthouse/pull/13323#issuecomment-962031709
422
+ // 0 throughput means the no (additional) throttling is expected.
423
+ // This is common for desktop + devtools throttling where throttling is additive and we don't want any additional.
424
+ const bitsPerSecond = throughput === 0 ? observedThroughput : throughput;
425
+ if (bitsPerSecond === 0) {
426
+ return 0;
427
+ }
428
+ const wastedBits = wastedBytes * 8;
429
+ const wastedMs = wastedBits / bitsPerSecond * 1000;
430
+ // This is an estimate of wasted time, so we won't be more precise than 10ms.
431
+ return Math.round(wastedMs / 10) * 10;
432
+ }
433
+ static get allNodeTimings() {
434
+ return ALL_SIMULATION_NODE_TIMINGS;
435
+ }
436
+ /**
437
+ * We attempt to start nodes by their observed start time using the request priority as a tie breaker.
438
+ * When simulating, just because a low priority image started 5ms before a high priority image doesn't mean
439
+ * it would have happened like that when the network was slower.
440
+ */
441
+ static _computeNodeStartPosition(node) {
442
+ if (node.type === 'cpu') {
443
+ return node.startTime;
444
+ }
445
+ return node.startTime + (PriorityStartTimePenalty[node.request.priority] * 1000 * 1000 || 0);
446
+ }
447
+ }
448
+ export { Simulator };
449
+ //# sourceMappingURL=Simulator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Simulator.js","sourceRoot":"","sources":["../../../../../../../../front_end/models/trace/lantern/simulation/Simulator.ts"],"names":[],"m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EvD,+EAA+E;QAC/E,iEAAiE;QACjE,kHAAkH;QAClH,MAAM,aAAa,GAAG,UAAU,KAAK,CAAC,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,UAAU,CAAC;QACzE,IAAI,aAAa,KAAK,CAAC,EAAE,CAAC;YACxB,OAAO,CAAC,CAAC;QACX,CAAC;QAED,MAAM,UAAU,GAAG,WAAW,GAAG,CAAC,CAAC;QACnC,MAAM,QAAQ,GAAG,UAAU,GAAG,aAAa,GAAG,IAAI,CAAC;QAEnD,6EAA6E;QAC7E,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC;IACxC,CAAC;IAED,MAAM,KAAK,cAAc;QACvB,OAAO,2BAA2B,CAAC;IACrC,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,yBAAyB,CAAC,IAAgB;QAC/C,IAAI,IAAI,CAAC,IAAI,KAAK,KAAK,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC,SAAS,CAAC;QACxB,CAAC;QACD,OAAO,IAAI,CAAC,SAAS,GAAG,CAAC,wBAAwB,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC;IAC/F,CAAC;CACF;AAED,OAAO,EAAC,SAAS,EAAC,CAAC","sourcesContent":["// Copyright 2024 The Chromium Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style license that can be\n// found in the LICENSE file.\n\nimport * as Graph from '../graph/graph.js';\nimport type * as Lantern from '../types/types.js';\n\nimport {ConnectionPool} from './ConnectionPool.js';\nimport {Constants} from './Constants.js';\nimport {DNSCache} from './DNSCache.js';\nimport {type CompleteNodeTiming, type ConnectionTiming, SimulatorTimingMap} from './SimulationTimingMap.js';\nimport {TCPConnection} from './TCPConnection.js';\n\nexport interface Result<T = Lantern.AnyNetworkObject> {\n timeInMs: number;\n nodeTimings: Map<Graph.Node<T>, Lantern.Simulation.NodeTiming>;\n}\n\nconst defaultThrottling = Constants.throttling.mobileSlow4G;\n\n// see https://cs.chromium.org/search/?q=kDefaultMaxNumDelayableRequestsPerClient&sq=package:chromium&type=cs\nconst DEFAULT_MAXIMUM_CONCURRENT_REQUESTS = 10;\n// layout tasks tend to be less CPU-bound and do not experience the same increase in duration\nconst DEFAULT_LAYOUT_TASK_MULTIPLIER = 0.5;\n// if a task takes more than 10 seconds it's usually a sign it isn't actually CPU bound and we're overestimating\nconst DEFAULT_MAXIMUM_CPU_TASK_DURATION = 10000;\n\nconst NodeState = {\n NotReadyToStart: 0,\n ReadyToStart: 1,\n InProgress: 2,\n Complete: 3,\n};\n\nconst PriorityStartTimePenalty: Record<Lantern.ResourcePriority, number> = {\n VeryHigh: 0,\n High: 0.25,\n Medium: 0.5,\n Low: 1,\n VeryLow: 2,\n};\n\nconst ALL_SIMULATION_NODE_TIMINGS = new Map<string, Map<Graph.Node, CompleteNodeTiming>>();\n\nclass Simulator<T = Lantern.AnyNetworkObject> {\n static createSimulator(settings: Lantern.Simulation.Settings): Simulator {\n const {throttlingMethod, throttling, precomputedLanternData, networkAnalysis} = settings;\n\n const options: Lantern.Simulation.Options = {\n additionalRttByOrigin: networkAnalysis.additionalRttByOrigin,\n serverResponseTimeByOrigin: networkAnalysis.serverResponseTimeByOrigin,\n observedThroughput: networkAnalysis.throughput,\n };\n\n // If we have precomputed lantern data, overwrite our observed estimates and use precomputed instead\n // for increased stability.\n if (precomputedLanternData) {\n options.additionalRttByOrigin = new Map(Object.entries(precomputedLanternData.additionalRttByOrigin));\n options.serverResponseTimeByOrigin = new Map(Object.entries(precomputedLanternData.serverResponseTimeByOrigin));\n }\n\n switch (throttlingMethod) {\n case 'provided':\n options.rtt = networkAnalysis.rtt;\n options.throughput = networkAnalysis.throughput;\n options.cpuSlowdownMultiplier = 1;\n options.layoutTaskMultiplier = 1;\n break;\n case 'devtools':\n if (throttling) {\n options.rtt = throttling.requestLatencyMs / Constants.throttling.DEVTOOLS_RTT_ADJUSTMENT_FACTOR;\n options.throughput =\n throttling.downloadThroughputKbps * 1024 / Constants.throttling.DEVTOOLS_THROUGHPUT_ADJUSTMENT_FACTOR;\n }\n\n options.cpuSlowdownMultiplier = 1;\n options.layoutTaskMultiplier = 1;\n break;\n case 'simulate':\n if (throttling) {\n options.rtt = throttling.rttMs;\n options.throughput = throttling.throughputKbps * 1024;\n options.cpuSlowdownMultiplier = throttling.cpuSlowdownMultiplier;\n }\n break;\n default:\n // intentionally fallback to simulator defaults\n break;\n }\n\n return new Simulator(options);\n }\n\n _options: Required<Lantern.Simulation.Options>;\n _rtt: number;\n _throughput: number;\n _maximumConcurrentRequests: number;\n _cpuSlowdownMultiplier: number;\n _layoutTaskMultiplier: number;\n _cachedNodeListByStartPosition: Graph.Node[];\n _nodeTimings: SimulatorTimingMap;\n _numberInProgressByType: Map<string, number>;\n _nodes: Record<number, Set<Graph.Node>>;\n _dns: DNSCache;\n _connectionPool: ConnectionPool;\n\n constructor(options?: Lantern.Simulation.Options) {\n this._options = Object.assign(\n {\n rtt: defaultThrottling.rttMs,\n throughput: defaultThrottling.throughputKbps * 1024,\n maximumConcurrentRequests: DEFAULT_MAXIMUM_CONCURRENT_REQUESTS,\n cpuSlowdownMultiplier: defaultThrottling.cpuSlowdownMultiplier,\n layoutTaskMultiplier: DEFAULT_LAYOUT_TASK_MULTIPLIER,\n additionalRttByOrigin: new Map(),\n serverResponseTimeByOrigin: new Map(),\n },\n options,\n );\n\n this._rtt = this._options.rtt;\n this._throughput = this._options.throughput;\n this._maximumConcurrentRequests = Math.max(\n Math.min(\n TCPConnection.maximumSaturatedConnections(this._rtt, this._throughput),\n this._options.maximumConcurrentRequests,\n ),\n 1);\n this._cpuSlowdownMultiplier = this._options.cpuSlowdownMultiplier;\n this._layoutTaskMultiplier = this._cpuSlowdownMultiplier * this._options.layoutTaskMultiplier;\n this._cachedNodeListByStartPosition = [];\n\n // Properties reset on every `.simulate` call but duplicated here for type checking\n this._nodeTimings = new SimulatorTimingMap();\n this._numberInProgressByType = new Map<string, number>();\n this._nodes = {};\n this._dns = new DNSCache({rtt: this._rtt});\n // @ts-expect-error\n this._connectionPool = null;\n\n if (!Number.isFinite(this._rtt)) {\n throw new Error(`Invalid rtt ${this._rtt}`);\n }\n if (!Number.isFinite(this._throughput)) {\n throw new Error(`Invalid rtt ${this._throughput}`);\n }\n }\n\n get rtt(): number {\n return this._rtt;\n }\n\n _initializeConnectionPool(graph: Graph.Node): void {\n const records: Lantern.NetworkRequest[] = [];\n graph.getRootNode().traverse(node => {\n if (node.type === Graph.BaseNode.types.NETWORK) {\n records.push(node.request);\n }\n });\n\n this._connectionPool = new ConnectionPool(records, this._options);\n }\n\n /**\n * Initializes the various state data structures such _nodeTimings and the _node Sets by state.\n */\n _initializeAuxiliaryData(): void {\n this._nodeTimings = new SimulatorTimingMap();\n this._numberInProgressByType = new Map();\n\n this._nodes = {};\n this._cachedNodeListByStartPosition = [];\n // NOTE: We don't actually need *all* of these sets, but the clarity that each node progresses\n // through the system is quite nice.\n for (const state of Object.values(NodeState)) {\n this._nodes[state] = new Set();\n }\n }\n\n _numberInProgress(type: string): number {\n return this._numberInProgressByType.get(type) || 0;\n }\n\n _markNodeAsReadyToStart(node: Graph.Node, queuedTime: number): void {\n const nodeStartPosition = Simulator._computeNodeStartPosition(node);\n const firstNodeIndexWithGreaterStartPosition = this._cachedNodeListByStartPosition.findIndex(\n candidate => Simulator._computeNodeStartPosition(candidate) > nodeStartPosition);\n const insertionIndex = firstNodeIndexWithGreaterStartPosition === -1 ? this._cachedNodeListByStartPosition.length :\n firstNodeIndexWithGreaterStartPosition;\n this._cachedNodeListByStartPosition.splice(insertionIndex, 0, node);\n\n this._nodes[NodeState.ReadyToStart].add(node);\n this._nodes[NodeState.NotReadyToStart].delete(node);\n this._nodeTimings.setReadyToStart(node, {queuedTime});\n }\n\n _markNodeAsInProgress(node: Graph.Node, startTime: number): void {\n const indexOfNodeToStart = this._cachedNodeListByStartPosition.indexOf(node);\n this._cachedNodeListByStartPosition.splice(indexOfNodeToStart, 1);\n\n this._nodes[NodeState.InProgress].add(node);\n this._nodes[NodeState.ReadyToStart].delete(node);\n this._numberInProgressByType.set(node.type, this._numberInProgress(node.type) + 1);\n this._nodeTimings.setInProgress(node, {startTime});\n }\n\n _markNodeAsComplete(node: Graph.Node, endTime: number, connectionTiming?: ConnectionTiming): void {\n this._nodes[NodeState.Complete].add(node);\n this._nodes[NodeState.InProgress].delete(node);\n this._numberInProgressByType.set(node.type, this._numberInProgress(node.type) - 1);\n this._nodeTimings.setCompleted(node, {endTime, connectionTiming});\n\n // Try to add all its dependents to the queue\n for (const dependent of node.getDependents()) {\n // Skip dependent node if one of its dependencies hasn't finished yet\n const dependencies = dependent.getDependencies();\n if (dependencies.some(dep => !this._nodes[NodeState.Complete].has(dep))) {\n continue;\n }\n\n // Otherwise add it to the queue\n this._markNodeAsReadyToStart(dependent, endTime);\n }\n }\n\n _acquireConnection(request: Lantern.NetworkRequest): TCPConnection|null {\n return this._connectionPool.acquire(request);\n }\n\n _getNodesSortedByStartPosition(): Graph.Node[] {\n // Make a copy so we don't skip nodes due to concurrent modification\n return Array.from(this._cachedNodeListByStartPosition);\n }\n\n _startNodeIfPossible(node: Graph.Node, totalElapsedTime: number): void {\n if (node.type === Graph.BaseNode.types.CPU) {\n // Start a CPU task if there's no other CPU task in process\n if (this._numberInProgress(node.type) === 0) {\n this._markNodeAsInProgress(node, totalElapsedTime);\n }\n\n return;\n }\n\n if (node.type !== Graph.BaseNode.types.NETWORK) {\n throw new Error('Unsupported');\n }\n\n // If a network request is connectionless, we can always start it, so skip the connection checks\n if (!node.isConnectionless) {\n // Start a network request if we're not at max requests and a connection is available\n const numberOfActiveRequests = this._numberInProgress(node.type);\n if (numberOfActiveRequests >= this._maximumConcurrentRequests) {\n return;\n }\n const connection = this._acquireConnection(node.request);\n if (!connection) {\n return;\n }\n }\n\n this._markNodeAsInProgress(node, totalElapsedTime);\n }\n\n /**\n * Updates each connection in use with the available throughput based on the number of network requests\n * currently in flight.\n */\n _updateNetworkCapacity(): void {\n const inFlight = this._numberInProgress(Graph.BaseNode.types.NETWORK);\n if (inFlight === 0) {\n return;\n }\n\n for (const connection of this._connectionPool.connectionsInUse()) {\n connection.setThroughput(this._throughput / inFlight);\n }\n }\n\n /**\n * Estimates the number of milliseconds remaining given current condidtions before the node is complete.\n */\n _estimateTimeRemaining(node: Graph.Node): number {\n if (node.type === Graph.BaseNode.types.CPU) {\n return this._estimateCPUTimeRemaining(node);\n }\n if (node.type === Graph.BaseNode.types.NETWORK) {\n return this._estimateNetworkTimeRemaining(node);\n }\n throw new Error('Unsupported');\n }\n\n _estimateCPUTimeRemaining(cpuNode: Graph.CPUNode): number {\n const timingData = this._nodeTimings.getCpuStarted(cpuNode);\n const multiplier = cpuNode.didPerformLayout() ? this._layoutTaskMultiplier : this._cpuSlowdownMultiplier;\n const totalDuration = Math.min(\n Math.round(cpuNode.duration / 1000 * multiplier),\n DEFAULT_MAXIMUM_CPU_TASK_DURATION,\n );\n const estimatedTimeElapsed = totalDuration - timingData.timeElapsed;\n this._nodeTimings.setCpuEstimated(cpuNode, {estimatedTimeElapsed});\n return estimatedTimeElapsed;\n }\n\n _estimateNetworkTimeRemaining(networkNode: Graph.NetworkNode): number {\n const request = networkNode.request;\n const timingData = this._nodeTimings.getNetworkStarted(networkNode);\n\n let timeElapsed = 0;\n if (networkNode.fromDiskCache) {\n // Rough access time for seeking to location on disk and reading sequentially.\n // 8ms per seek + 20ms/MB\n // @see http://norvig.com/21-days.html#answers\n const sizeInMb = (request.resourceSize || 0) / 1024 / 1024;\n timeElapsed = 8 + 20 * sizeInMb - timingData.timeElapsed;\n } else if (networkNode.isNonNetworkProtocol) {\n // Estimates for the overhead of a data URL in Chromium and the decoding time for base64-encoded data.\n // 2ms per request + 10ms/MB\n // @see traces on https://dopiaza.org/tools/datauri/examples/index.php\n const sizeInMb = (request.resourceSize || 0) / 1024 / 1024;\n timeElapsed = 2 + 10 * sizeInMb - timingData.timeElapsed;\n } else {\n const connection = this._connectionPool.acquireActiveConnectionFromRequest(request);\n const dnsResolutionTime = this._dns.getTimeUntilResolution(request, {\n requestedAt: timingData.startTime,\n shouldUpdateCache: true,\n });\n const timeAlreadyElapsed = timingData.timeElapsed;\n const calculation = connection.simulateDownloadUntil(\n request.transferSize - timingData.bytesDownloaded,\n {timeAlreadyElapsed, dnsResolutionTime, maximumTimeToElapse: Infinity},\n );\n\n timeElapsed = calculation.timeElapsed;\n }\n\n const estimatedTimeElapsed = timeElapsed + timingData.timeElapsedOvershoot;\n this._nodeTimings.setNetworkEstimated(networkNode, {estimatedTimeElapsed});\n return estimatedTimeElapsed;\n }\n\n /**\n * Computes and returns the minimum estimated completion time of the nodes currently in progress.\n */\n _findNextNodeCompletionTime(): number {\n let minimumTime = Infinity;\n for (const node of this._nodes[NodeState.InProgress]) {\n minimumTime = Math.min(minimumTime, this._estimateTimeRemaining(node));\n }\n\n return minimumTime;\n }\n\n /**\n * Given a time period, computes the progress toward completion that the node made durin that time.\n */\n _updateProgressMadeInTimePeriod(node: Graph.Node, timePeriodLength: number, totalElapsedTime: number): void {\n const timingData = this._nodeTimings.getInProgress(node);\n const isFinished = timingData.estimatedTimeElapsed === timePeriodLength;\n\n if (node.type === Graph.BaseNode.types.CPU || node.isConnectionless) {\n if (isFinished) {\n this._markNodeAsComplete(node, totalElapsedTime);\n } else {\n timingData.timeElapsed += timePeriodLength;\n }\n return;\n }\n\n if (node.type !== Graph.BaseNode.types.NETWORK) {\n throw new Error('Unsupported');\n }\n if (!('bytesDownloaded' in timingData)) {\n throw new Error('Invalid timing data');\n }\n\n const request = node.request;\n const connection = this._connectionPool.acquireActiveConnectionFromRequest(request);\n const dnsResolutionTime = this._dns.getTimeUntilResolution(request, {\n requestedAt: timingData.startTime,\n shouldUpdateCache: true,\n });\n const calculation = connection.simulateDownloadUntil(\n request.transferSize - timingData.bytesDownloaded,\n {\n dnsResolutionTime,\n timeAlreadyElapsed: timingData.timeElapsed,\n maximumTimeToElapse: timePeriodLength - timingData.timeElapsedOvershoot,\n },\n );\n\n connection.setCongestionWindow(calculation.congestionWindow);\n connection.setH2OverflowBytesDownloaded(calculation.extraBytesDownloaded);\n\n if (isFinished) {\n connection.setWarmed(true);\n this._connectionPool.release(request);\n this._markNodeAsComplete(node, totalElapsedTime, calculation.connectionTiming);\n } else {\n timingData.timeElapsed += calculation.timeElapsed;\n timingData.timeElapsedOvershoot += calculation.timeElapsed - timePeriodLength;\n timingData.bytesDownloaded += calculation.bytesDownloaded;\n }\n }\n\n _computeFinalNodeTimings(): {\n nodeTimings: Map<Graph.Node, Lantern.Simulation.NodeTiming>,\n completeNodeTimings: Map<Graph.Node, CompleteNodeTiming>,\n } {\n const completeNodeTimingEntries: Array<[Graph.Node, CompleteNodeTiming]> =\n this._nodeTimings.getNodes().map(node => {\n return [node, this._nodeTimings.getCompleted(node)];\n });\n\n // Most consumers will want the entries sorted by startTime, so insert them in that order\n completeNodeTimingEntries.sort((a, b) => a[1].startTime - b[1].startTime);\n\n // Trimmed version of type `Lantern.Simulation.NodeTiming`.\n const nodeTimingEntries: Array<[Graph.Node, Lantern.Simulation.NodeTiming]> =\n completeNodeTimingEntries.map(([node, timing]) => {\n return [\n node,\n {\n startTime: timing.startTime,\n endTime: timing.endTime,\n duration: timing.endTime - timing.startTime,\n },\n ];\n });\n\n return {\n nodeTimings: new Map(nodeTimingEntries),\n completeNodeTimings: new Map(completeNodeTimingEntries),\n };\n }\n\n getOptions(): Required<Lantern.Simulation.Options> {\n return this._options;\n }\n\n /**\n * Estimates the time taken to process all of the graph's nodes, returns the overall time along with\n * each node annotated by start/end times.\n *\n * Simulator/connection pool are allowed to deviate from what was\n * observed in the trace/devtoolsLog and start requests as soon as they are queued (i.e. do not\n * wait around for a warm connection to be available if the original request was fetched on a warm\n * connection).\n */\n simulate(graph: Graph.Node, options?: {label?: string}): Result<T> {\n if (Graph.BaseNode.hasCycle(graph)) {\n throw new Error('Cannot simulate graph with cycle');\n }\n\n options = Object.assign(\n {\n label: undefined,\n },\n options);\n\n // initialize the necessary data containers\n this._dns = new DNSCache({rtt: this._rtt});\n this._initializeConnectionPool(graph);\n this._initializeAuxiliaryData();\n\n const nodesNotReadyToStart = this._nodes[NodeState.NotReadyToStart];\n const nodesReadyToStart = this._nodes[NodeState.ReadyToStart];\n const nodesInProgress = this._nodes[NodeState.InProgress];\n\n const rootNode = graph.getRootNode();\n rootNode.traverse(node => nodesNotReadyToStart.add(node));\n let totalElapsedTime = 0;\n let iteration = 0;\n\n // root node is always ready to start\n this._markNodeAsReadyToStart(rootNode, totalElapsedTime);\n\n // loop as long as we have nodes in the queue or currently in progress\n while (nodesReadyToStart.size || nodesInProgress.size) {\n // move all possible queued nodes to in progress\n for (const node of this._getNodesSortedByStartPosition()) {\n this._startNodeIfPossible(node, totalElapsedTime);\n }\n\n if (!nodesInProgress.size) {\n // Interplay between fromDiskCache and connectionReused can be incorrect,\n // have to give up.\n throw new Error('Failed to start a node');\n }\n\n // set the available throughput for all connections based on # inflight\n this._updateNetworkCapacity();\n\n // find the time that the next node will finish\n const minimumTime = this._findNextNodeCompletionTime();\n totalElapsedTime += minimumTime;\n\n // While this is no longer strictly necessary, it's always better than hanging\n if (!Number.isFinite(minimumTime) || iteration > 100000) {\n throw new Error('Simulation failed, depth exceeded');\n }\n\n iteration++;\n // update how far each node will progress until that point\n for (const node of nodesInProgress) {\n this._updateProgressMadeInTimePeriod(node, minimumTime, totalElapsedTime);\n }\n }\n\n // `nodeTimings` are used for simulator consumers, `completeNodeTimings` kept for debugging.\n const {nodeTimings, completeNodeTimings} = this._computeFinalNodeTimings();\n ALL_SIMULATION_NODE_TIMINGS.set(options.label || 'unlabeled', completeNodeTimings);\n\n return {\n timeInMs: totalElapsedTime,\n nodeTimings,\n };\n }\n\n computeWastedMsFromWastedBytes(wastedBytes: number): number {\n const {throughput, observedThroughput} = this._options;\n\n // https://github.com/GoogleChrome/lighthouse/pull/13323#issuecomment-962031709\n // 0 throughput means the no (additional) throttling is expected.\n // This is common for desktop + devtools throttling where throttling is additive and we don't want any additional.\n const bitsPerSecond = throughput === 0 ? observedThroughput : throughput;\n if (bitsPerSecond === 0) {\n return 0;\n }\n\n const wastedBits = wastedBytes * 8;\n const wastedMs = wastedBits / bitsPerSecond * 1000;\n\n // This is an estimate of wasted time, so we won't be more precise than 10ms.\n return Math.round(wastedMs / 10) * 10;\n }\n\n static get allNodeTimings(): Map<string, Map<Graph.Node, CompleteNodeTiming>> {\n return ALL_SIMULATION_NODE_TIMINGS;\n }\n\n /**\n * We attempt to start nodes by their observed start time using the request priority as a tie breaker.\n * When simulating, just because a low priority image started 5ms before a high priority image doesn't mean\n * it would have happened like that when the network was slower.\n */\n static _computeNodeStartPosition(node: Graph.Node): number {\n if (node.type === 'cpu') {\n return node.startTime;\n }\n return node.startTime + (PriorityStartTimePenalty[node.request.priority] * 1000 * 1000 || 0);\n }\n}\n\nexport {Simulator};\n"]}
@@ -0,0 +1,48 @@
1
+ import { type ConnectionTiming } from './SimulationTimingMap.js';
2
+ interface DownloadOptions {
3
+ dnsResolutionTime?: number;
4
+ timeAlreadyElapsed?: number;
5
+ maximumTimeToElapse?: number;
6
+ }
7
+ interface DownloadResults {
8
+ roundTrips: number;
9
+ timeElapsed: number;
10
+ bytesDownloaded: number;
11
+ extraBytesDownloaded: number;
12
+ congestionWindow: number;
13
+ connectionTiming: ConnectionTiming;
14
+ }
15
+ declare class TCPConnection {
16
+ _warmed: boolean;
17
+ _ssl: boolean;
18
+ _h2: boolean;
19
+ _rtt: number;
20
+ _throughput: number;
21
+ _serverLatency: number;
22
+ _congestionWindow: number;
23
+ _h2OverflowBytesDownloaded: number;
24
+ constructor(rtt: number, throughput: number, serverLatency?: number, ssl?: boolean, h2?: boolean);
25
+ static maximumSaturatedConnections(rtt: number, availableThroughput: number): number;
26
+ _computeMaximumCongestionWindowInSegments(): number;
27
+ setThroughput(throughput: number): void;
28
+ setCongestionWindow(congestion: number): void;
29
+ setWarmed(warmed: boolean): void;
30
+ isWarm(): boolean;
31
+ isH2(): boolean;
32
+ get congestionWindow(): number;
33
+ /**
34
+ * Sets the number of excess bytes that are available to this connection on future downloads, only
35
+ * applies to H2 connections.
36
+ */
37
+ setH2OverflowBytesDownloaded(bytes: number): void;
38
+ clone(): TCPConnection;
39
+ /**
40
+ * Simulates a network download of a particular number of bytes over an optional maximum amount of time
41
+ * and returns information about the ending state.
42
+ *
43
+ * See https://hpbn.co/building-blocks-of-tcp/#three-way-handshake and
44
+ * https://hpbn.co/transport-layer-security-tls/#tls-handshake for details.
45
+ */
46
+ simulateDownloadUntil(bytesToDownload: number, options?: DownloadOptions): DownloadResults;
47
+ }
48
+ export { TCPConnection };