-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically Profile roots when DevTools is present (#13058)
* react-test-renderer injects itself into DevTools if present * Fibers are always opted into ProfileMode if DevTools is present * Added simple test for DevTools + always profiling behavior
- Loading branch information
Showing
4 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
111 changes: 111 additions & 0 deletions
111
packages/react/src/__tests__/ReactProfilerDevToolsIntegration-test.internal.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('ReactProfiler DevTools integration', () => { | ||
let React; | ||
let ReactFeatureFlags; | ||
let ReactTestRenderer; | ||
let AdvanceTime; | ||
let advanceTimeBy; | ||
let hook; | ||
let mockNow; | ||
|
||
const mockNowForTests = () => { | ||
let currentTime = 0; | ||
|
||
mockNow = jest.fn().mockImplementation(() => currentTime); | ||
|
||
ReactTestRenderer.unstable_setNowImplementation(mockNow); | ||
advanceTimeBy = amount => { | ||
currentTime += amount; | ||
}; | ||
}; | ||
|
||
beforeEach(() => { | ||
global.__REACT_DEVTOOLS_GLOBAL_HOOK__ = hook = { | ||
inject: () => {}, | ||
onCommitFiberRoot: jest.fn((rendererId, root) => {}), | ||
onCommitFiberUnmount: () => {}, | ||
supportsFiber: true, | ||
}; | ||
|
||
jest.resetModules(); | ||
|
||
ReactFeatureFlags = require('shared/ReactFeatureFlags'); | ||
ReactFeatureFlags.enableProfilerTimer = true; | ||
React = require('react'); | ||
ReactTestRenderer = require('react-test-renderer'); | ||
|
||
mockNowForTests(); | ||
|
||
AdvanceTime = class extends React.Component { | ||
static defaultProps = { | ||
byAmount: 10, | ||
shouldComponentUpdate: true, | ||
}; | ||
shouldComponentUpdate(nextProps) { | ||
return nextProps.shouldComponentUpdate; | ||
} | ||
render() { | ||
// Simulate time passing when this component is rendered | ||
advanceTimeBy(this.props.byAmount); | ||
return this.props.children || null; | ||
} | ||
}; | ||
}); | ||
|
||
it('should auto-Profile all fibers if the DevTools hook is detected', () => { | ||
const App = ({multiplier}) => { | ||
advanceTimeBy(2); | ||
return ( | ||
<React.unstable_Profiler id="Profiler" onRender={onRender}> | ||
<AdvanceTime byAmount={3 * multiplier} shouldComponentUpdate={true} /> | ||
<AdvanceTime | ||
byAmount={7 * multiplier} | ||
shouldComponentUpdate={false} | ||
/> | ||
</React.unstable_Profiler> | ||
); | ||
}; | ||
|
||
const onRender = jest.fn(() => {}); | ||
const rendered = ReactTestRenderer.create(<App multiplier={1} />); | ||
|
||
expect(hook.onCommitFiberRoot).toHaveBeenCalledTimes(1); | ||
|
||
// Measure observable timing using the Profiler component. | ||
// The time spent in App (above the Profiler) won't be included in the durations, | ||
// But needs to be accounted for in the offset times. | ||
expect(onRender).toHaveBeenCalledTimes(1); | ||
expect(onRender).toHaveBeenCalledWith('Profiler', 'mount', 10, 10, 2, 12); | ||
onRender.mockClear(); | ||
|
||
// Measure unobservable timing required by the DevTools profiler. | ||
// At this point, the base time should include both: | ||
// The time 2ms in the App component itself, and | ||
// The 10ms spend in the Profiler sub-tree beneath. | ||
expect(rendered.root.findByType(App)._currentFiber().treeBaseTime).toBe(12); | ||
|
||
rendered.update(<App multiplier={2} />); | ||
|
||
// Measure observable timing using the Profiler component. | ||
// The time spent in App (above the Profiler) won't be included in the durations, | ||
// But needs to be accounted for in the offset times. | ||
expect(onRender).toHaveBeenCalledTimes(1); | ||
expect(onRender).toHaveBeenCalledWith('Profiler', 'update', 6, 13, 14, 20); | ||
|
||
// Measure unobservable timing required by the DevTools profiler. | ||
// At this point, the base time should include both: | ||
// The initial 9ms for the components that do not re-render, and | ||
// The updated 6ms for the component that does. | ||
expect(rendered.root.findByType(App)._currentFiber().treeBaseTime).toBe(15); | ||
}); | ||
}); |