Files
react-native/packages/rn-tester/js/examples/Performance/PerformanceApiExample.js
T
Xin Chen c1023c73b0 Add performance.reactNativeStartupTiming API
Summary:
This diff adds the `performance.reactNativeStartupTiming` API to the performance global object for RN. This property does not exist in web, and we are free to make up our own list of properties in the startup metrics to track RN app startup process. In our case, we have the following six properties to begin with (we may extend and add more to this list in the future):

```
- `(start|end)Time`: The time ‘zero’ for the startup timing and the end of app startup. RN has no knowledge of app start time, which will be provided by the platform. The `endTime` will be the time when the first JS bundle finishes executing (Note that RN supports multiple JS bundles, which can be loaded async)
  - `executeJavaScriptBundleEntryPoint(Start|End)`: The time for RN to execute the JS entry point (and finish all sync job)

```

Changelog:
[General][Added] - Add new JS performance API to support getting RN app startup timings

Reviewed By: rshest

Differential Revision: D43326564

fbshipit-source-id: 7b4c7cae70ff64ba1714a1630cd5e183df6c06b0
2023-03-02 20:04:42 -08:00

138 lines
4.1 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
* @oncall react_native
*/
'use strict';
import type ReactNativeStartupTiming from '../../../../../Libraries/WebPerformance/ReactNativeStartupTiming';
import * as React from 'react';
import {StyleSheet, View, Text, Button} from 'react-native';
import RNTesterPage from '../../components/RNTesterPage';
import Performance from '../../../../../Libraries/WebPerformance/Performance';
const {useState, useCallback} = React;
const performance = new Performance();
function MemoryExample(): React.Node {
// Memory API testing
const [memoryInfo, setMemoryInfo] = useState<{
jsHeapSizeLimit?: ?number,
totalJSHeapSize?: ?number,
usedJSHeapSize?: ?number,
}>({});
const onGetMemoryInfo = useCallback(() => {
// performance.memory is not included in bom.js yet.
// Once we release the change in flow this can be removed.
// $FlowFixMe[prop-missing]
// $FlowFixMe[incompatible-call]
setMemoryInfo(performance.memory);
}, []);
return (
<RNTesterPage noSpacer={true} noScroll={true} title="performance.memory">
<View style={styles.container}>
<Button onPress={onGetMemoryInfo} title="Click to update memory info" />
<View>
<Text>
{`jsHeapSizeLimit: ${
memoryInfo.jsHeapSizeLimit == null
? 'N/A'
: memoryInfo.jsHeapSizeLimit
} bytes`}
</Text>
<Text>
{`totalJSHeapSize: ${
memoryInfo.totalJSHeapSize == null
? 'N/A'
: memoryInfo.totalJSHeapSize
} bytes`}
</Text>
<Text>
{`usedJSHeapSize: ${
memoryInfo.usedJSHeapSize == null
? 'N/A'
: memoryInfo.usedJSHeapSize
} bytes`}
</Text>
</View>
</View>
</RNTesterPage>
);
}
function StartupTimingExample(): React.Node {
// React Startup Timing API testing
const [startUpTiming, setStartUpTiming] =
useState<?ReactNativeStartupTiming>(null);
const onGetStartupTiming = useCallback(() => {
// performance.reactNativeStartupTiming is not included in bom.js yet.
// Once we release the change in flow this can be removed.
// $FlowFixMe[prop-missing]
// $FlowFixMe[incompatible-call]
setStartUpTiming(performance.reactNativeStartupTiming);
}, []);
return (
<RNTesterPage
noSpacer={true}
noScroll={true}
title="performance.reactNativeStartupTiming">
<View style={styles.container}>
<Button
onPress={onGetStartupTiming}
title="Click to update React startup timing"
/>
<View>
<Text>{`startTime: ${
startUpTiming == null ? 'N/A' : startUpTiming.startTime
} ms`}</Text>
<Text>
{`executeJavaScriptBundleEntryPointStart: ${
startUpTiming == null
? 'N/A'
: startUpTiming.executeJavaScriptBundleEntryPointStart
} ms`}
</Text>
<Text>{`executeJavaScriptBundleEntryPointEnd: ${
startUpTiming == null
? 'N/A'
: startUpTiming.executeJavaScriptBundleEntryPointEnd
} ms`}</Text>
<Text>{`endTime: ${
startUpTiming == null ? 'N/A' : startUpTiming.endTime
} ms`}</Text>
</View>
</View>
</RNTesterPage>
);
}
const styles = StyleSheet.create({
container: {
padding: 10,
},
});
exports.title = 'Performance API Examples';
exports.category = 'Basic';
exports.description = 'Shows the performance API provided in React Native';
exports.examples = [
{
title: 'performance.memory',
render: function (): React.Element<typeof MemoryExample> {
return <MemoryExample />;
},
},
{
title: 'performance.reactNativeStartupTiming',
render: function (): React.Element<typeof StartupTimingExample> {
return <StartupTimingExample />;
},
},
];