Skip to content

Commit 8dfbd16

Browse files
authored
[Fiber] Color Performance Track Entries by Self Time (#30984)
Stacked on #30983. This colors each component entry by its self time from light to dark depending on how long it took. If it took longer than a cut off we color it red (the error color). <img width="435" alt="Screenshot 2024-09-16 at 11 48 15 PM" src="https://github.com/user-attachments/assets/5d0bda83-6205-40e9-bec1-b81db2d48b2d">
1 parent e1c2090 commit 8dfbd16

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

packages/react-reconciler/src/ReactFiberCommitWork.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ import {
109109
popComponentEffectStart,
110110
componentEffectStartTime,
111111
componentEffectEndTime,
112+
componentEffectDuration,
112113
} from './ReactProfilerTimer';
113114
import {
114115
logComponentRender,
@@ -608,6 +609,7 @@ function commitLayoutEffectOnFiber(
608609
finishedWork,
609610
componentEffectStartTime,
610611
componentEffectEndTime,
612+
componentEffectDuration,
611613
);
612614
}
613615

@@ -2105,6 +2107,7 @@ function commitMutationEffectsOnFiber(
21052107
finishedWork,
21062108
componentEffectStartTime,
21072109
componentEffectEndTime,
2110+
componentEffectDuration,
21082111
);
21092112
}
21102113

@@ -2926,6 +2929,7 @@ function commitPassiveMountOnFiber(
29262929
finishedWork,
29272930
componentEffectStartTime,
29282931
componentEffectEndTime,
2932+
componentEffectDuration,
29292933
);
29302934
}
29312935

@@ -3444,6 +3448,7 @@ function commitPassiveUnmountOnFiber(finishedWork: Fiber): void {
34443448
finishedWork,
34453449
componentEffectStartTime,
34463450
componentEffectEndTime,
3451+
componentEffectDuration,
34473452
);
34483453
}
34493454

packages/react-reconciler/src/ReactFiberPerformanceTrack.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,9 @@ const reusableComponentOptions = {
3838
},
3939
};
4040

41-
const reusableComponentEffectDevToolDetails = {
42-
dataType: 'track-entry',
43-
color: 'secondary',
44-
track: 'Blocking', // Lane
45-
trackGroup: TRACK_GROUP,
46-
};
47-
const reusableComponentEffectOptions = {
48-
start: -0,
49-
end: -0,
50-
detail: {
51-
devtools: reusableComponentEffectDevToolDetails,
52-
},
53-
};
54-
5541
export function setCurrentTrackFromLanes(lanes: number): void {
56-
reusableComponentEffectDevToolDetails.track =
57-
reusableComponentDevToolDetails.track =
58-
getGroupNameOfHighestPriorityLane(lanes);
42+
reusableComponentDevToolDetails.track =
43+
getGroupNameOfHighestPriorityLane(lanes);
5944
}
6045

6146
export function logComponentRender(
@@ -69,6 +54,20 @@ export function logComponentRender(
6954
return;
7055
}
7156
if (supportsUserTiming) {
57+
let selfTime: number = (fiber.actualDuration: any);
58+
if (fiber.alternate === null || fiber.alternate.child !== fiber.child) {
59+
for (let child = fiber.child; child !== null; child = child.sibling) {
60+
selfTime -= (child.actualDuration: any);
61+
}
62+
}
63+
reusableComponentDevToolDetails.color =
64+
selfTime < 0.5
65+
? 'primary-light'
66+
: selfTime < 10
67+
? 'primary'
68+
: selfTime < 100
69+
? 'primary-dark'
70+
: 'error';
7271
reusableComponentOptions.start = startTime;
7372
reusableComponentOptions.end = endTime;
7473
performance.measure(name, reusableComponentOptions);
@@ -79,15 +78,24 @@ export function logComponentEffect(
7978
fiber: Fiber,
8079
startTime: number,
8180
endTime: number,
81+
selfTime: number,
8282
): void {
8383
const name = getComponentNameFromFiber(fiber);
8484
if (name === null) {
8585
// Skip
8686
return;
8787
}
8888
if (supportsUserTiming) {
89-
reusableComponentEffectOptions.start = startTime;
90-
reusableComponentEffectOptions.end = endTime;
91-
performance.measure(name, reusableComponentEffectOptions);
89+
reusableComponentDevToolDetails.color =
90+
selfTime < 1
91+
? 'secondary-light'
92+
: selfTime < 100
93+
? 'secondary'
94+
: selfTime < 500
95+
? 'secondary-dark'
96+
: 'error';
97+
reusableComponentOptions.start = startTime;
98+
reusableComponentOptions.end = endTime;
99+
performance.measure(name, reusableComponentOptions);
92100
}
93101
}

packages/react-reconciler/src/ReactProfilerTimer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export let completeTime: number = -0;
2525
export let commitTime: number = -0;
2626
export let profilerStartTime: number = -1.1;
2727
export let profilerEffectDuration: number = -0;
28+
export let componentEffectDuration: number = -0;
2829
export let componentEffectStartTime: number = -1.1;
2930
export let componentEffectEndTime: number = -1.1;
3031

@@ -72,6 +73,7 @@ export function pushComponentEffectStart(): number {
7273
}
7374
const prevEffectStart = componentEffectStartTime;
7475
componentEffectStartTime = -1.1; // Track the next start.
76+
componentEffectDuration = -0; // Reset component level duration.
7577
return prevEffectStart;
7678
}
7779

@@ -211,6 +213,7 @@ export function recordEffectDuration(fiber: Fiber): void {
211213
// Store duration on the next nearest Profiler ancestor
212214
// Or the root (for the DevTools Profiler to read)
213215
profilerEffectDuration += elapsedTime;
216+
componentEffectDuration += elapsedTime;
214217

215218
// Keep track of the last end time of the effects.
216219
componentEffectEndTime = endTime;

0 commit comments

Comments
 (0)