forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkMarker.tsx
More file actions
382 lines (341 loc) · 10.9 KB
/
NetworkMarker.tsx
File metadata and controls
382 lines (341 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import * as React from 'react';
import classNames from 'classnames';
import { TooltipDetail, type TooltipDetailComponent } from './TooltipDetails';
import {
getColorClassNameForMimeType,
guessMimeTypeFromNetworkMarker,
} from 'firefox-profiler/profile-logic/marker-data';
import {
formatBytes,
formatNumber,
formatMilliseconds,
} from 'firefox-profiler/utils/format-numbers';
import {
getLatestPreconnectPhaseAndValue,
getMatchingPhaseValues,
getHumanReadableDataStatus,
getHumanReadablePriority,
getHumanReadableHttpVersion,
PRECONNECT_PHASES_IN_ORDER,
REQUEST_PHASES_IN_ORDER,
ALL_NETWORK_PHASES_IN_ORDER,
} from 'firefox-profiler/profile-logic/network';
import type {
NetworkPayload,
NetworkPhaseName,
NetworkPhaseAndValue,
Milliseconds,
} from 'firefox-profiler/types';
import './NetworkMarker.css';
/* The labels are for the duration between _this_ label and the next label. */
const HUMAN_LABEL_FOR_PHASE: Record<NetworkPhaseName, string> = {
startTime: 'Waiting for socket thread',
domainLookupStart: 'DNS request',
domainLookupEnd: 'After DNS request',
connectStart: 'TCP connection',
tcpConnectEnd: 'After TCP connection',
secureConnectionStart: 'Establishing TLS session',
connectEnd: 'Waiting for HTTP request',
requestStart: 'HTTP request and waiting for response',
responseStart: 'HTTP response',
responseEnd: 'Waiting for main thread',
endTime: 'End',
};
const OPACITY_FOR_PHASE: Record<NetworkPhaseName, number> = {
startTime: 0,
domainLookupStart: 0.5,
domainLookupEnd: 0.5,
connectStart: 0.5,
tcpConnectEnd: 0.5,
secureConnectionStart: 0.5,
connectEnd: 0.5,
requestStart: 0.75,
responseStart: 1,
responseEnd: 0,
endTime: 0,
};
type NetworkPhaseProps = {
readonly propertyName: NetworkPhaseName;
readonly dur: Milliseconds;
readonly startPosition: Milliseconds;
readonly phaseDuration: Milliseconds;
};
class NetworkPhase extends React.PureComponent<NetworkPhaseProps> {
override render() {
const { startPosition, dur, propertyName, phaseDuration } = this.props;
const startPositionPercent = (startPosition / dur) * 100;
const durationPercent = Math.max(0.3, (phaseDuration / dur) * 100);
const opacity = OPACITY_FOR_PHASE[propertyName];
return (
<React.Fragment>
<div className="tooltipLabel">
{HUMAN_LABEL_FOR_PHASE[propertyName]}:
</div>
<div
aria-label={`Starting at ${formatNumber(
startPosition
)} milliseconds, duration is ${formatNumber(
phaseDuration
)} milliseconds`}
>
{formatMilliseconds(phaseDuration)}
</div>
<div
className={classNames('tooltipNetworkPhase', {
tooltipNetworkPhaseEmpty: opacity === 0,
})}
aria-hidden="true"
style={{
marginLeft: startPositionPercent + '%',
marginRight: 100 - startPositionPercent - durationPercent + '%',
opacity: opacity === 0 ? undefined : opacity,
}}
/>
</React.Fragment>
);
}
}
type Props = {
readonly payload: NetworkPayload;
readonly zeroAt: Milliseconds;
};
export class TooltipNetworkMarkerPhases extends React.PureComponent<Props> {
_renderPhases(
properties: NetworkPhaseAndValue[],
sectionDuration: Milliseconds,
startTime: Milliseconds
): Array<React.ReactElement<typeof NetworkPhase>> | null {
if (properties.length < 2) {
console.error(
'Only 1 preconnect property has been found, this should not happen.'
);
return null;
}
const phases = [];
for (let i = 1; i < properties.length; i++) {
const { phase: previousProperty, value: startValue } = properties[i - 1];
const { value: endValue } = properties[i];
const phaseDuration = endValue - startValue;
const startPosition = startValue - startTime;
phases.push(
<NetworkPhase
key={previousProperty}
propertyName={previousProperty}
startPosition={startPosition}
phaseDuration={phaseDuration}
dur={sectionDuration}
/>
);
}
return phases;
}
_renderPreconnectPhases(): React.ReactNode {
const { payload, zeroAt } = this.props;
const preconnectStart = payload.domainLookupStart;
if (typeof preconnectStart !== 'number') {
// All preconnect operations include a domain lookup part.
return null;
}
// The preconnect bar goes from the start to the end of the whole preconnect
// operation, that includes both the domain lookup and the connection
// process. Therefore we want the value that represents the latest phase.
const preconnectEndPhase = getLatestPreconnectPhaseAndValue(
this.props.payload
);
if (preconnectEndPhase === null) {
return null;
}
const preconnectEnd = preconnectEndPhase.value;
// If the latest phase ends before the start of the marker, we'll display a
// separate preconnect section.
// It could theorically happen that a preconnect session starts before
// `startTime` but ends after `startTime`; in that case we'll still draw
// only one diagram.
const hasPreconnect = preconnectEnd < payload.startTime;
if (!hasPreconnect) {
return null;
}
const preconnectValues = getMatchingPhaseValues(
payload,
PRECONNECT_PHASES_IN_ORDER
);
const dur = preconnectEnd - preconnectStart;
const phases = this._renderPhases(preconnectValues, dur, preconnectStart);
return (
<>
<h3 className="tooltipNetworkTitle3">
Preconnect (starting at {formatMilliseconds(preconnectStart - zeroAt)}
)
</h3>
{phases}
</>
);
}
override render() {
const { payload } = this.props;
const mimeType =
payload.contentType || guessMimeTypeFromNetworkMarker(payload);
const markerColorClass = getColorClassNameForMimeType(mimeType);
if (payload.status === 'STATUS_START') {
return null;
}
const preconnectPhases = this._renderPreconnectPhases();
const availablePhases = getMatchingPhaseValues(
payload,
preconnectPhases ? REQUEST_PHASES_IN_ORDER : ALL_NETWORK_PHASES_IN_ORDER
);
if (availablePhases.length === 0 || availablePhases.length === 1) {
// This shouldn't happen as we should always have both startTime and endTime.
return null;
}
const dur = payload.endTime - payload.startTime;
if (availablePhases.length === 2) {
// We only have startTime and endTime.
return (
<div className={`tooltipNetworkPhases ${markerColorClass}`}>
<NetworkPhase
propertyName="responseStart"
startPosition={0}
phaseDuration={dur}
dur={dur}
/>
</div>
);
}
// Looks like availablePhases.length >= 3.
const renderedPhases = this._renderPhases(
availablePhases,
dur,
payload.startTime
);
return (
// We render both phase sections in the same grid so that they're aligned
// and the bar widths have the same reference.
<div className={`tooltipNetworkPhases ${markerColorClass}`}>
{preconnectPhases ? (
<>
{/* Note: preconnectPhases contains its own title */}
{preconnectPhases}
<h3 className="tooltipNetworkTitle3">Actual request</h3>
</>
) : null}
{renderedPhases}
</div>
);
}
}
/**
* This function bypasses the Marker schema, and uses its own formatting to display
* the Network details.
*/
export function getNetworkMarkerDetails(
payload: NetworkPayload
): TooltipDetailComponent[] {
let mimeType = payload.contentType;
let mimeTypeLabel = 'MIME type';
if (mimeType === undefined || mimeType === null) {
mimeType = guessMimeTypeFromNetworkMarker(payload);
mimeTypeLabel = 'Guessed MIME type';
}
const markerColorClass = getColorClassNameForMimeType(mimeType);
const details = [];
details.push(
<TooltipDetail label="Status" key="Network-Status">
{getHumanReadableDataStatus(payload.status)}
</TooltipDetail>
);
if (payload.redirectType !== undefined) {
details.push(
<TooltipDetail label="Redirection type" key="Redirection-Type">
{payload.redirectType +
(payload.isHttpToHttpsRedirect ? ' (HTTP to HTTPS)' : '')}
</TooltipDetail>
);
}
details.push(
<TooltipDetail label="Cache" key="Network-Cache">
{payload.cache}
</TooltipDetail>,
<TooltipDetail label="URL" key="Network-URL">
<span className="tooltipDetailsUrl">{payload.URI}</span>
</TooltipDetail>
);
if (payload.RedirectURI) {
details.push(
<TooltipDetail label="Redirect URL" key="Network-Redirect URL">
<span className="tooltipDetailsUrl">{payload.RedirectURI}</span>
</TooltipDetail>
);
}
details.push(
<TooltipDetail label="Priority" key="Network-Priority">
{getHumanReadablePriority(payload.pri)}
</TooltipDetail>
);
if (payload.priorityHeader) {
details.push(
<TooltipDetail label="Priority Header" key="Network-Priority-Header">
{payload.priorityHeader}
</TooltipDetail>
);
}
if (mimeType) {
details.push(
<TooltipDetail label={mimeTypeLabel} key={'Network-' + mimeTypeLabel}>
<div className="tooltipNetworkMimeType">
<span
className={`tooltipNetworkMimeTypeSwatch colored-square ${markerColorClass}`}
title={mimeType}
/>
{mimeType}
</div>
</TooltipDetail>
);
}
if (payload.isPrivateBrowsing) {
details.push(
<TooltipDetail label="Private Browsing" key="Network-Private Browsing">
Yes
</TooltipDetail>
);
}
if (typeof payload.count === 'number') {
details.push(
<TooltipDetail label="Requested bytes" key="Network-Requested Bytes">
{formatBytes(payload.count)}
</TooltipDetail>
);
}
if (payload.httpVersion) {
details.push(
<TooltipDetail label="HTTP Version" key="Network-HTTP Version">
{getHumanReadableHttpVersion(payload.httpVersion)}
</TooltipDetail>
);
}
if (payload.classOfService) {
details.push(
<TooltipDetail label="Class of Service" key="Network-Class of Service">
{payload.classOfService}
</TooltipDetail>
);
}
if (payload.requestStatus) {
details.push(
<TooltipDetail label="Request Status" key="Network-Request Status">
{payload.requestStatus}
</TooltipDetail>
);
}
if (payload.responseStatus) {
details.push(
<TooltipDetail label="Response Status Code" key="Network-Response Status">
{payload.responseStatus}
</TooltipDetail>
);
}
return details;
}