Skip to content

Commit ff9f943

Browse files
authored
[DevTools] add perf regression test page in shell (#25078)
## Summary This PR adds a "perf regression tests" page to react-devtools-shell. This page is meant to be used as a performance sanity check we will run whenever we release a new version or finish a major refactor. Similar to other pages in the shell, this page can load the inline version of devtools and a test react app on the same page. But this page does not load devtools automatically like other pages. Instead, it provides a button that allows us to load devtools on-demand, so that we can easily compare perf numbers without devtools against the numbers with devtools. <img width="561" alt="image" src="https://user-images.githubusercontent.com/1001890/184059633-e4f0852c-8464-4d94-8064-1684eee626f4.png"> As a first step, this page currently only contain one test: mount/unmount a large subtree. This is to catch perf issues that devtools can cause on the react applications it's running on, which was once a bug fixed in #24863. In the future, we plan to add: - more test apps covering different scenarios - perf numbers within devtools (e.g. initial load) ## How did you test this change? In order to show this test app can actually catch the perf regression it's aiming at, I reverted #24863 locally. Here is the result: https://user-images.githubusercontent.com/1001890/184059214-9c9b308c-173b-4dd7-b815-46fbd7067073.mov As shown in the video, the time it takes to unmount the large subtree significantly increased after DevTools is loaded. For comparison, here is how it looks like before the fix was reverted: <img width="452" alt="image" src="https://user-images.githubusercontent.com/1001890/184059743-0968bc7d-4ce4-42cd-b04a-f6cbc078d4f4.png"> ## about the `requestAnimationFrame` method For this test, I used `requestAnimationFrame` to catch the time when render and commit are done. It aligns very well with the numbers reported by Chrome DevTools performance profiling. For example, in one run, the numbers reported by my method are <img width="464" alt="image" src="https://user-images.githubusercontent.com/1001890/184060228-990a4c75-f594-411a-9f85-fa5532ec8c37.png"> They are very close to the numbers reported by Chrome profiling: <img width="456" alt="image" src="https://user-images.githubusercontent.com/1001890/184060355-a15d1ec5-c296-4016-9c83-03e761f387e3.png"> <img width="354" alt="image" src="https://user-images.githubusercontent.com/1001890/184060375-19029010-3aed-4a23-890e-397cdba86d9e.png"> `<Profiler>` is not able to catch this issue here. If you are aware of a better way to do this, please kindly share with me.
1 parent c2d6552 commit ff9f943

File tree

8 files changed

+191
-1
lines changed

8 files changed

+191
-1
lines changed

packages/react-devtools-shell/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@
5151
<a href="/multi.html">multi DevTools</a>
5252
|
5353
<a href="/e2e.html">e2e tests</a>
54+
|
5455
<a href="/e2e-regression.html">e2e regression tests</a>
56+
|
57+
<a href="/perf-regression.html">perf regression tests</a>
5558
</span>
5659
</div>
5760

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf8">
5+
<title>React DevTools</title>
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<style>
8+
* {
9+
box-sizing: border-box;
10+
}
11+
body {
12+
margin: 0;
13+
padding: 0;
14+
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial,
15+
sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol;
16+
font-size: 12px;
17+
line-height: 1.5;
18+
}
19+
#iframe {
20+
position: absolute;
21+
top: 0;
22+
left: 0;
23+
width: 100vw;
24+
height: 60vh;
25+
}
26+
#devtools {
27+
position: absolute;
28+
bottom: 0;
29+
left: 0;
30+
width: 100vw;
31+
height: 40vh;
32+
}
33+
#load-devtools {
34+
margin: 20px;
35+
}
36+
</style>
37+
</head>
38+
<body>
39+
<iframe id="iframe"></iframe>
40+
<div id="devtools">
41+
<button id="load-devtools">Load DevTools</button>
42+
</div>
43+
<script src="dist/perf-regression-devtools.js"></script>
44+
</body>
45+
</html>

packages/react-devtools-shell/src/e2e-regression/devtools.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {initialize as createDevTools} from 'react-devtools-inline/frontend';
99

1010
// This is a pretty gross hack to make the runtime loaded named-hooks-code work.
1111
// TODO (Webpack 5) Hoepfully we can remove this once we upgrade to Webpack 5.
12-
// $FlowFixMer
12+
// $FlowFixMe
1313
__webpack_public_path__ = '/dist/'; // eslint-disable-line no-undef
1414

1515
// TODO (Webpack 5) Hopefully we can remove this prop after the Webpack 5 migration.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/** @flow */
2+
3+
// This test harness mounts each test app as a separate root to test multi-root applications.
4+
5+
import * as React from 'react';
6+
import {createRoot} from 'react-dom/client';
7+
import App from './apps/index';
8+
9+
function mountApp() {
10+
const container = document.createElement('div');
11+
12+
((document.body: any): HTMLBodyElement).appendChild(container);
13+
14+
const root = createRoot(container);
15+
root.render(
16+
<React.StrictMode>
17+
<App />
18+
</React.StrictMode>,
19+
);
20+
}
21+
22+
mountApp();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import * as React from 'react';
11+
12+
function generateArray(size) {
13+
return Array.from({length: size}, () => Math.floor(Math.random() * size));
14+
}
15+
16+
const arr = generateArray(50000);
17+
18+
export default function LargeSubtree() {
19+
const [showList, setShowList] = React.useState(false);
20+
const toggleList = () => {
21+
const startTime = performance.now();
22+
setShowList(!showList);
23+
// requestAnimationFrame should happen after render+commit is done
24+
window.requestAnimationFrame(() => {
25+
const afterRenderTime = performance.now();
26+
console.log(
27+
`Time spent on ${
28+
showList ? 'unmounting' : 'mounting'
29+
} the subtree: ${afterRenderTime - startTime}ms`,
30+
);
31+
});
32+
};
33+
return (
34+
<div>
35+
<h2>Mount/Unmount a large subtree</h2>
36+
<p>Click the button to toggle the state. Open console for results.</p>
37+
<button onClick={toggleList}>toggle</button>
38+
<ul>
39+
<li key="dummy">dummy item</li>
40+
{showList && arr.map((num, idx) => <li key={idx}>{num}</li>)}
41+
</ul>
42+
</div>
43+
);
44+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import * as React from 'react';
11+
import LargeSubtree from './LargeSubtree';
12+
13+
export default function Home() {
14+
return (
15+
<div>
16+
<LargeSubtree />
17+
</div>
18+
);
19+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as React from 'react';
2+
import {createRoot} from 'react-dom/client';
3+
import {
4+
activate as activateBackend,
5+
initialize as initializeBackend,
6+
} from 'react-devtools-inline/backend';
7+
import {initialize as createDevTools} from 'react-devtools-inline/frontend';
8+
9+
// This is a pretty gross hack to make the runtime loaded named-hooks-code work.
10+
// TODO (Webpack 5) Hoepfully we can remove this once we upgrade to Webpack 5.
11+
// $FlowFixMe
12+
__webpack_public_path__ = '/dist/'; // eslint-disable-line no-undef
13+
14+
// TODO (Webpack 5) Hopefully we can remove this prop after the Webpack 5 migration.
15+
function hookNamesModuleLoaderFunction() {
16+
return import('react-devtools-inline/hookNames');
17+
}
18+
19+
function inject(contentDocument, sourcePath) {
20+
const script = contentDocument.createElement('script');
21+
script.src = sourcePath;
22+
23+
((contentDocument.body: any): HTMLBodyElement).appendChild(script);
24+
}
25+
26+
function init(
27+
appSource: string,
28+
appIframe: HTMLIFrameElement,
29+
devtoolsContainer: HTMLElement,
30+
loadDevToolsButton: HTMLButtonElement,
31+
) {
32+
const {contentDocument, contentWindow} = appIframe;
33+
34+
initializeBackend(contentWindow);
35+
36+
inject(contentDocument, appSource);
37+
38+
loadDevToolsButton.addEventListener('click', () => {
39+
const DevTools = createDevTools(contentWindow);
40+
createRoot(devtoolsContainer).render(
41+
<DevTools
42+
hookNamesModuleLoaderFunction={hookNamesModuleLoaderFunction}
43+
showTabBar={true}
44+
/>,
45+
);
46+
activateBackend(contentWindow);
47+
});
48+
}
49+
50+
init(
51+
'dist/perf-regression-app.js',
52+
document.getElementById('iframe'),
53+
document.getElementById('devtools'),
54+
document.getElementById('load-devtools'),
55+
);

packages/react-devtools-shell/webpack.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ const app = makeConfig(
157157
'multi-devtools': './src/multi/devtools.js',
158158
'multi-right': './src/multi/right.js',
159159
'e2e-regression': './src/e2e-regression/app.js',
160+
'perf-regression-app': './src/perf-regression/app.js',
161+
'perf-regression-devtools': './src/perf-regression/devtools.js',
160162
},
161163
{
162164
react: resolve(builtModulesDir, 'react'),

0 commit comments

Comments
 (0)