-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(react): Track duration of React component updates #5531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d926d7f
Track duration of React component updates
0Calories 3c4d3c0
Use Prettier config from workspace
0Calories 1a1555b
Remove changes to vscode settings
0Calories ac107fc
Move comments
0Calories fb96942
Remove VSCode settings changes
0Calories 0608e16
Remove Prettier changes in test file
0Calories 7cc8984
Add docstring and additional asserts on mockFinish
0Calories File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,10 @@ class Profiler extends React.Component<ProfilerProps> { | |
* Made protected for the React Native SDK to access | ||
*/ | ||
protected _mountSpan: Span | undefined = undefined; | ||
/** | ||
* The span that represents the duration of time between shouldComponentUpdate and componentDidUpdate | ||
*/ | ||
protected _updateSpan: Span | undefined = undefined; | ||
|
||
// eslint-disable-next-line @typescript-eslint/member-ordering | ||
public static defaultProps: Partial<ProfilerProps> = { | ||
|
@@ -66,29 +70,35 @@ class Profiler extends React.Component<ProfilerProps> { | |
} | ||
} | ||
|
||
public componentDidUpdate({ updateProps, includeUpdates = true }: ProfilerProps): void { | ||
// Only generate an update span if hasUpdateSpan is true, if there is a valid mountSpan, | ||
public shouldComponentUpdate({ updateProps, includeUpdates = true }: ProfilerProps): boolean { | ||
// Only generate an update span if includeUpdates is true, if there is a valid mountSpan, | ||
// and if the updateProps have changed. It is ok to not do a deep equality check here as it is expensive. | ||
// We are just trying to give baseline clues for further investigation. | ||
if (includeUpdates && this._mountSpan && updateProps !== this.props.updateProps) { | ||
// See what props haved changed between the previous props, and the current props. This is | ||
// set as data on the span. We just store the prop keys as the values could be potenially very large. | ||
const changedProps = Object.keys(updateProps).filter(k => updateProps[k] !== this.props.updateProps[k]); | ||
if (changedProps.length > 0) { | ||
// The update span is a point in time span with 0 duration, just signifying that the component | ||
// has been updated. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed this comment since it's no longer true |
||
const now = timestampWithMs(); | ||
this._mountSpan.startChild({ | ||
this._updateSpan = this._mountSpan.startChild({ | ||
data: { | ||
changedProps, | ||
}, | ||
description: `<${this.props.name}>`, | ||
endTimestamp: now, | ||
op: REACT_UPDATE_OP, | ||
startTimestamp: now, | ||
}); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public componentDidUpdate(): void { | ||
if (this._updateSpan) { | ||
this._updateSpan.finish(); | ||
this._updateSpan = undefined; | ||
} | ||
} | ||
|
||
// If a component is unmounted, we can say it is no longer on the screen. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ jest.mock('@sentry/browser', () => ({ | |
|
||
beforeEach(() => { | ||
mockStartChild.mockClear(); | ||
mockFinish.mockClear(); | ||
activeTransaction = new MockSpan({ op: 'pageload' }); | ||
}); | ||
|
||
|
@@ -100,7 +101,9 @@ describe('withProfiler', () => { | |
}); | ||
|
||
it('is not created if hasRenderSpan is false', () => { | ||
const ProfiledComponent = withProfiler(() => <h1>Testing</h1>, { includeRender: false }); | ||
const ProfiledComponent = withProfiler(() => <h1>Testing</h1>, { | ||
includeRender: false, | ||
}); | ||
expect(mockStartChild).toHaveBeenCalledTimes(0); | ||
|
||
const component = render(<ProfiledComponent />); | ||
|
@@ -115,32 +118,33 @@ describe('withProfiler', () => { | |
const ProfiledComponent = withProfiler((props: { num: number }) => <div>{props.num}</div>); | ||
const { rerender } = render(<ProfiledComponent num={0} />); | ||
expect(mockStartChild).toHaveBeenCalledTimes(1); | ||
expect(mockFinish).toHaveBeenCalledTimes(1); | ||
|
||
// Dispatch new props | ||
rerender(<ProfiledComponent num={1} />); | ||
expect(mockStartChild).toHaveBeenCalledTimes(2); | ||
expect(mockStartChild).toHaveBeenLastCalledWith({ | ||
data: { changedProps: ['num'] }, | ||
description: `<${UNKNOWN_COMPONENT}>`, | ||
endTimestamp: expect.any(Number), | ||
op: REACT_UPDATE_OP, | ||
startTimestamp: expect.any(Number), | ||
}); | ||
|
||
expect(mockFinish).toHaveBeenCalledTimes(2); | ||
// New props yet again | ||
rerender(<ProfiledComponent num={2} />); | ||
expect(mockStartChild).toHaveBeenCalledTimes(3); | ||
expect(mockStartChild).toHaveBeenLastCalledWith({ | ||
data: { changedProps: ['num'] }, | ||
description: `<${UNKNOWN_COMPONENT}>`, | ||
endTimestamp: expect.any(Number), | ||
op: REACT_UPDATE_OP, | ||
startTimestamp: expect.any(Number), | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we assert on |
||
expect(mockFinish).toHaveBeenCalledTimes(3); | ||
|
||
// Should not create spans if props haven't changed | ||
rerender(<ProfiledComponent num={2} />); | ||
expect(mockStartChild).toHaveBeenCalledTimes(3); | ||
expect(mockFinish).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('does not get created if hasUpdateSpan is false', () => { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a doc string for this?