Skip to content

Commit e67dcd9

Browse files
committed
Log to track
1 parent 93b6f22 commit e67dcd9

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
logDedupedComponentRender,
8080
logComponentErrored,
8181
logIOInfo,
82+
logComponentAwait,
8283
} from './ReactFlightPerformanceTrack';
8384

8485
import {
@@ -680,7 +681,7 @@ function getIOInfoTaskName(ioInfo: ReactIOInfo): string {
680681
}
681682

682683
function getAsyncInfoTaskName(asyncInfo: ReactAsyncInfo): string {
683-
return 'await'; // We could be smarter about this and give it a name like `then` or `Promise.all`.
684+
return 'await ' + getIOInfoTaskName(asyncInfo.awaited);
684685
}
685686

686687
function getServerComponentTaskName(componentInfo: ReactComponentInfo): string {
@@ -2971,9 +2972,12 @@ function flushComponentPerformance(
29712972
for (let i = debugInfo.length - 1; i >= 0; i--) {
29722973
const info = debugInfo[i];
29732974
if (typeof info.time === 'number') {
2974-
endTime = info.time;
2975-
if (endTime > childrenEndTime) {
2976-
childrenEndTime = endTime;
2975+
if (info.time > childrenEndTime) {
2976+
childrenEndTime = info.time;
2977+
}
2978+
if (endTime === 0) {
2979+
// Last timestamp is the end of the last component.
2980+
endTime = info.time;
29772981
}
29782982
}
29792983
if (typeof info.name === 'string' && i > 0) {
@@ -3011,8 +3015,29 @@ function flushComponentPerformance(
30113015
}
30123016
// Track the root most component of the result for deduping logging.
30133017
result.component = componentInfo;
3018+
// Set the end time of the previous component to the start of the previous.
3019+
endTime = startTime;
30143020
}
30153021
isLastComponent = false;
3022+
} else if (info.awaited && i > 0 && i < debugInfo.length - 2) {
3023+
// $FlowFixMe: Refined.
3024+
const asyncInfo: ReactAsyncInfo = info;
3025+
const startTimeInfo = debugInfo[i - 1];
3026+
const endTimeInfo = debugInfo[i + 1];
3027+
if (
3028+
typeof startTimeInfo.time === 'number' &&
3029+
typeof endTimeInfo.time === 'number'
3030+
) {
3031+
const awaitStartTime = startTimeInfo.time;
3032+
const awaitEndTime = endTimeInfo.time;
3033+
logComponentAwait(
3034+
asyncInfo,
3035+
trackIdx,
3036+
awaitStartTime,
3037+
awaitEndTime,
3038+
response._rootEnvironmentName,
3039+
);
3040+
}
30163041
}
30173042
}
30183043
}

packages/react-client/src/ReactFlightPerformanceTrack.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
/* eslint-disable react-internal/no-production-logging */
1111

12-
import type {ReactComponentInfo, ReactIOInfo} from 'shared/ReactTypes';
12+
import type {
13+
ReactComponentInfo,
14+
ReactIOInfo,
15+
ReactAsyncInfo,
16+
} from 'shared/ReactTypes';
1317

1418
import {enableProfilerTimer} from 'shared/ReactFeatureFlags';
1519

@@ -224,6 +228,48 @@ function getIOColor(
224228
}
225229
}
226230

231+
export function logComponentAwait(
232+
asyncInfo: ReactAsyncInfo,
233+
trackIdx: number,
234+
startTime: number,
235+
endTime: number,
236+
rootEnv: string,
237+
): void {
238+
if (supportsUserTiming && endTime > 0) {
239+
const env = asyncInfo.env;
240+
const name = asyncInfo.awaited.name;
241+
const isPrimaryEnv = env === rootEnv;
242+
const color = getIOColor(name);
243+
const entryName =
244+
'await ' +
245+
(isPrimaryEnv || env === undefined ? name : name + ' [' + env + ']');
246+
const debugTask = asyncInfo.debugTask;
247+
if (__DEV__ && debugTask) {
248+
debugTask.run(
249+
// $FlowFixMe[method-unbinding]
250+
console.timeStamp.bind(
251+
console,
252+
entryName,
253+
startTime < 0 ? 0 : startTime,
254+
endTime,
255+
trackNames[trackIdx],
256+
COMPONENTS_TRACK,
257+
color,
258+
),
259+
);
260+
} else {
261+
console.timeStamp(
262+
entryName,
263+
startTime < 0 ? 0 : startTime,
264+
endTime,
265+
trackNames[trackIdx],
266+
COMPONENTS_TRACK,
267+
color,
268+
);
269+
}
270+
}
271+
}
272+
227273
export function logIOInfo(ioInfo: ReactIOInfo, rootEnv: string): void {
228274
const startTime = ioInfo.start;
229275
const endTime = ioInfo.end;

0 commit comments

Comments
 (0)