Skip to content

Commit

Permalink
Track self time and color the track entry by the self time
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Sep 17, 2024
1 parent a3894dd commit d771ad9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
5 changes: 5 additions & 0 deletions packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import {
popComponentEffectStart,
componentEffectStartTime,
componentEffectEndTime,
componentEffectDuration,
} from './ReactProfilerTimer';
import {
logComponentRender,
Expand Down Expand Up @@ -608,6 +609,7 @@ function commitLayoutEffectOnFiber(
finishedWork,
componentEffectStartTime,
componentEffectEndTime,
componentEffectDuration,
);
}

Expand Down Expand Up @@ -2105,6 +2107,7 @@ function commitMutationEffectsOnFiber(
finishedWork,
componentEffectStartTime,
componentEffectEndTime,
componentEffectDuration,
);
}

Expand Down Expand Up @@ -2926,6 +2929,7 @@ function commitPassiveMountOnFiber(
finishedWork,
componentEffectStartTime,
componentEffectEndTime,
componentEffectDuration,
);
}

Expand Down Expand Up @@ -3444,6 +3448,7 @@ function commitPassiveUnmountOnFiber(finishedWork: Fiber): void {
finishedWork,
componentEffectStartTime,
componentEffectEndTime,
componentEffectDuration,
);
}

Expand Down
46 changes: 26 additions & 20 deletions packages/react-reconciler/src/ReactFiberPerformanceTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,9 @@ const reusableComponentOptions = {
},
};

const reusableComponentEffectDevToolDetails = {
dataType: 'track-entry',
color: 'secondary',
track: 'Blocking', // Lane
trackGroup: TRACK_GROUP,
};
const reusableComponentEffectOptions = {
start: -0,
end: -0,
detail: {
devtools: reusableComponentEffectDevToolDetails,
},
};

export function setCurrentTrackFromLanes(lanes: number): void {
reusableComponentEffectDevToolDetails.track =
reusableComponentDevToolDetails.track =
getGroupNameOfHighestPriorityLane(lanes);
reusableComponentDevToolDetails.track =
getGroupNameOfHighestPriorityLane(lanes);
}

export function logComponentRender(
Expand All @@ -69,6 +54,18 @@ export function logComponentRender(
return;
}
if (supportsUserTiming) {
let selfTime: number = (fiber.actualDuration: any);
for (let child = fiber.child; child !== null; child = child.sibling) {
selfTime -= (child.actualDuration: any);
}
reusableComponentDevToolDetails.color =
selfTime < 0.5
? 'primary-light'
: selfTime < 10
? 'primary'
: selfTime < 100
? 'primary-dark'
: 'error';
reusableComponentOptions.start = startTime;
reusableComponentOptions.end = endTime;
performance.measure(name, reusableComponentOptions);
Expand All @@ -79,15 +76,24 @@ export function logComponentEffect(
fiber: Fiber,
startTime: number,
endTime: number,
selfTime: number,
): void {
const name = getComponentNameFromFiber(fiber);
if (name === null) {
// Skip
return;
}
if (supportsUserTiming) {
reusableComponentEffectOptions.start = startTime;
reusableComponentEffectOptions.end = endTime;
performance.measure(name, reusableComponentEffectOptions);
reusableComponentDevToolDetails.color =
selfTime < 1
? 'secondary-light'
: selfTime < 100
? 'secondary'
: selfTime < 500
? 'secondary-dark'
: 'error';
reusableComponentOptions.start = startTime;
reusableComponentOptions.end = endTime;
performance.measure(name, reusableComponentOptions);
}
}
3 changes: 3 additions & 0 deletions packages/react-reconciler/src/ReactProfilerTimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export let completeTime: number = -0;
export let commitTime: number = -0;
export let profilerStartTime: number = -1.1;
export let profilerEffectDuration: number = -0;
export let componentEffectDuration: number = -0;
export let componentEffectStartTime: number = -1.1;
export let componentEffectEndTime: number = -1.1;

Expand Down Expand Up @@ -72,6 +73,7 @@ export function pushComponentEffectStart(): number {
}
const prevEffectStart = componentEffectStartTime;
componentEffectStartTime = -1.1; // Track the next start.
componentEffectDuration = -0; // Reset component level duration.
return prevEffectStart;
}

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

// Keep track of the last end time of the effects.
componentEffectEndTime = endTime;
Expand Down

0 comments on commit d771ad9

Please sign in to comment.