/**
* 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.
*
* @flow strict-local
* @format
*/
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
import type Performance from 'react-native/src/private/webapis/performance/Performance';
import RNTesterText from '../../components/RNTesterText';
import * as React from 'react';
import {useEffect} from 'react';
import {Button, StyleSheet, View} from 'react-native';
declare var performance: Performance;
const {useState, useCallback} = React;
function MemoryExample(): React.Node {
// Memory API testing
const [memoryInfo, setMemoryInfo] =
useState(null);
const onGetMemoryInfo = useCallback(() => {
// performance.memory is not included in bom.js yet.
// Once we release the change in flow this can be removed.
setMemoryInfo(performance.memory);
}, []);
return (
{`jsHeapSizeLimit: ${String(memoryInfo?.jsHeapSizeLimit)} bytes`}
{`totalJSHeapSize: ${String(memoryInfo?.totalJSHeapSize)} bytes`}
{`usedJSHeapSize: ${String(memoryInfo?.usedJSHeapSize)} bytes`}
);
}
function StartupTimingExample(): React.Node {
// React Startup Timing API testing
const [startUpTiming, setStartUpTiming] =
useState(null);
const onGetStartupTiming = useCallback(() => {
// performance.reactNativeStartupTiming is not included in bom.js yet.
// Once we release the change in flow this can be removed.
setStartUpTiming(performance.rnStartupTiming);
}, []);
return (
{`startTime: ${String(startUpTiming?.startTime)} ms`}
{`initializeRuntimeStart: ${String(
startUpTiming?.initializeRuntimeStart,
)} ms`}
{`executeJavaScriptBundleEntryPointStart: ${String(
startUpTiming?.executeJavaScriptBundleEntryPointStart,
)} ms`}
{`endTime: ${String(startUpTiming?.endTime)} ms`}
);
}
function PerformanceObserverUserTimingExample(): React.Node {
const [entries, setEntries] = useState<$ReadOnlyArray>([]);
useEffect(() => {
const observer = new PerformanceObserver(list => {
const newEntries = list
.getEntries()
.filter(entry => entry.name.startsWith('rntester-'));
if (newEntries.length > 0) {
setEntries(newEntries);
}
});
observer.observe({entryTypes: ['mark', 'measure']});
return () => observer.disconnect();
}, []);
const onPress = useCallback(() => {
performance.mark('rntester-mark1');
performance.mark('rntester-mark2');
performance.measure(
'rntester-measure1',
'rntester-mark1',
'rntester-mark2',
);
}, []);
return (
{entries.map((entry, index) =>
entry.entryType === 'mark' ? (
Mark {entry.name}: {entry.startTime.toFixed(2)}
) : (
Measure {entry.name}: {entry.startTime.toFixed(2)} -{' '}
{(entry.startTime + entry.duration).toFixed(2)} (
{entry.duration.toFixed(2)}ms)
),
)}
);
}
function PerformanceObserverEventTimingExample(): React.Node {
const [count, setCount] = useState(0);
const [entries, setEntries] = useState<
$ReadOnlyArray,
>([]);
useEffect(() => {
const observer = new PerformanceObserver(list => {
const newEntries: $ReadOnlyArray =
// $FlowExpectedError[incompatible-type] This is guaranteed because we're only observing `event` entry types.
list.getEntries();
setEntries(newEntries);
});
observer.observe({type: 'event'});
return () => observer.disconnect();
}, []);
const onPress = useCallback(() => {
busyWait(500);
// Force a state update to show how/if we're reporting paint times as well.
setCount(currentCount => currentCount + 1);
}, []);
return (
{entries.map((entry, index) => (
Event: {entry.name}
{'\n'}
Start: {entry.startTime.toFixed(2)}
{'\n'}
End: {(entry.startTime + entry.duration).toFixed(2)}
{'\n'}
Duration: {entry.duration.toFixed(2)}ms{'\n'}
Processing start: {entry.processingStart.toFixed(2)} (delay:{' '}
{(entry.processingStart - entry.startTime).toFixed(2)}ms){'\n'}
Processing end: {entry.processingEnd.toFixed(2)} (duration:{' '}
{(entry.processingEnd - entry.processingStart).toFixed(2)}ms){'\n'}
))}
);
}
function PerformanceObserverLongtaskExample(): React.Node {
const [entries, setEntries] = useState<$ReadOnlyArray>([]);
useEffect(() => {
const observer = new PerformanceObserver(list => {
setEntries(list.getEntries());
});
observer.observe({entryTypes: ['longtask']});
return () => observer.disconnect();
}, []);
const onPress = useCallback(() => {
// Wait 1s to force a long task
busyWait(1000);
}, []);
return (
{entries.map((entry, index) => (
Long task {entry.name}: {entry.startTime} -{' '}
{entry.startTime + entry.duration} ({entry.duration}ms)
))}
);
}
function busyWait(ms: number): void {
const end = performance.now() + ms;
while (performance.now() < end) {}
}
const styles = StyleSheet.create({
container: {
padding: 10,
},
});
export const title = 'Performance API Examples';
export const category = 'Basic';
export const description = 'Shows the performance API provided in React Native';
export const examples: Array = ([
{
title: 'performance.memory',
render: (): React.Node => {
return ;
},
},
{
title: 'performance.reactNativeStartupTiming',
render: (): React.Node => {
return ;
},
},
{
title: 'PerformanceObserver (marks and measures)',
render: (): React.Node => {
return ;
},
},
{
title: 'PerformanceObserver (events)',
render: (): React.Node => {
return ;
},
},
PerformanceObserver.supportedEntryTypes.includes('longtask')
? {
title: 'PerformanceObserver (long tasks)',
render: (): React.Node => {
return ;
},
}
: null,
]: Array).filter(Boolean);