|
| 1 | +import { Profiler, useCallback, useEffect, useRef, useState } from 'react'; |
| 2 | +import { StyleSheet, Text, View } from 'react-native'; |
| 3 | +import { Clickable, ScrollView } from 'react-native-gesture-handler'; |
| 4 | + |
| 5 | +const CLICK_COUNT = 2000; |
| 6 | +const N = 25; |
| 7 | +const DROPOUT = 3; |
| 8 | + |
| 9 | +const STRESS_DATA = Array.from( |
| 10 | + { length: CLICK_COUNT }, |
| 11 | + (_, i) => `stress-${i}` |
| 12 | +); |
| 13 | + |
| 14 | +type BenchmarkState = |
| 15 | + | { phase: 'idle' } |
| 16 | + | { phase: 'running'; run: number } |
| 17 | + | { phase: 'done'; results: number[] }; |
| 18 | + |
| 19 | +function getTrimmedAverage(results: number[], dropout: number): number { |
| 20 | + const sorted = [...results].sort((a, b) => a - b); |
| 21 | + const trimCount = Math.min( |
| 22 | + dropout, |
| 23 | + Math.max(0, Math.floor((sorted.length - 1) / 2)) |
| 24 | + ); |
| 25 | + const trimmed = |
| 26 | + trimCount > 0 ? sorted.slice(trimCount, sorted.length - trimCount) : sorted; |
| 27 | + return trimmed.reduce((sum, v) => sum + v, 0) / trimmed.length; |
| 28 | +} |
| 29 | + |
| 30 | +type ClickableListProps = { |
| 31 | + run: number; |
| 32 | + onMountDuration: (duration: number) => void; |
| 33 | +}; |
| 34 | + |
| 35 | +function ClickableList({ run, onMountDuration }: ClickableListProps) { |
| 36 | + const reportedRef = useRef(-1); |
| 37 | + |
| 38 | + const handleRender = useCallback( |
| 39 | + (_id: string, phase: string, actualDuration: number) => { |
| 40 | + if (phase === 'mount' && reportedRef.current !== run) { |
| 41 | + reportedRef.current = run; |
| 42 | + onMountDuration(actualDuration); |
| 43 | + } |
| 44 | + }, |
| 45 | + [run, onMountDuration] |
| 46 | + ); |
| 47 | + |
| 48 | + return ( |
| 49 | + <Profiler id="ClickableList" onRender={handleRender}> |
| 50 | + <ScrollView style={{ flex: 1 }}> |
| 51 | + {STRESS_DATA.map((id) => ( |
| 52 | + // <BaseButton key={id} style={styles.button} /> |
| 53 | + <Clickable key={id} style={styles.button} /> |
| 54 | + |
| 55 | + // <RectButton key={id} style={styles.button} /> |
| 56 | + // <Clickable |
| 57 | + // key={id} |
| 58 | + // style={styles.button} |
| 59 | + // activeUnderlayOpacity={0.105} |
| 60 | + // /> |
| 61 | + |
| 62 | + // <BorderlessButton key={id} style={styles.button} /> |
| 63 | + // <Clickable key={id} style={styles.button} activeOpacity={0.3} /> |
| 64 | + ))} |
| 65 | + </ScrollView> |
| 66 | + </Profiler> |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +export default function ClickableStress() { |
| 71 | + const [state, setState] = useState<BenchmarkState>({ phase: 'idle' }); |
| 72 | + const resultsRef = useRef<number[]>([]); |
| 73 | + const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 74 | + |
| 75 | + const start = useCallback(() => { |
| 76 | + resultsRef.current = []; |
| 77 | + setState({ phase: 'running', run: 1 }); |
| 78 | + }, []); |
| 79 | + |
| 80 | + const handleMountDuration = useCallback((duration: number) => { |
| 81 | + resultsRef.current = [...resultsRef.current, duration]; |
| 82 | + const currentRun = resultsRef.current.length; |
| 83 | + |
| 84 | + if (currentRun >= N) { |
| 85 | + setState({ phase: 'done', results: resultsRef.current }); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // Unmount then remount for next run |
| 90 | + setState({ phase: 'idle' }); |
| 91 | + if (timeoutRef.current !== null) { |
| 92 | + clearTimeout(timeoutRef.current); |
| 93 | + } |
| 94 | + timeoutRef.current = setTimeout(() => { |
| 95 | + setState({ phase: 'running', run: currentRun + 1 }); |
| 96 | + }, 50); |
| 97 | + }, []); |
| 98 | + |
| 99 | + useEffect(() => { |
| 100 | + return () => { |
| 101 | + if (timeoutRef.current !== null) { |
| 102 | + clearTimeout(timeoutRef.current); |
| 103 | + timeoutRef.current = null; |
| 104 | + } |
| 105 | + }; |
| 106 | + }, []); |
| 107 | + |
| 108 | + const isRunning = state.phase === 'running'; |
| 109 | + const currentRun = state.phase === 'running' ? state.run : 0; |
| 110 | + const results = state.phase === 'done' ? state.results : null; |
| 111 | + const trimmedAverage = results ? getTrimmedAverage(results, DROPOUT) : null; |
| 112 | + |
| 113 | + return ( |
| 114 | + <View style={styles.container}> |
| 115 | + <Clickable |
| 116 | + activeUnderlayOpacity={0.105} |
| 117 | + style={[styles.startButton, isRunning && styles.startButtonBusy]} |
| 118 | + onPress={start} |
| 119 | + enabled={!isRunning}> |
| 120 | + <Text style={styles.startButtonText}> |
| 121 | + {isRunning ? `Running ${currentRun}/${N}...` : 'Start test'} |
| 122 | + </Text> |
| 123 | + </Clickable> |
| 124 | + |
| 125 | + {results && ( |
| 126 | + <View style={styles.results}> |
| 127 | + <Text style={styles.resultText}> |
| 128 | + Runs: {results.length} (trimmed ±{DROPOUT}) |
| 129 | + </Text> |
| 130 | + <Text style={styles.resultText}> |
| 131 | + Trimmed avg: {trimmedAverage?.toFixed(2)} ms |
| 132 | + </Text> |
| 133 | + <Text style={styles.resultText}> |
| 134 | + Min: {Math.min(...results).toFixed(2)} ms |
| 135 | + </Text> |
| 136 | + <Text style={styles.resultText}> |
| 137 | + Max: {Math.max(...results).toFixed(2)} ms |
| 138 | + </Text> |
| 139 | + <Text style={styles.resultText}> |
| 140 | + All: {results.map((r) => r.toFixed(1)).join(', ')} ms |
| 141 | + </Text> |
| 142 | + </View> |
| 143 | + )} |
| 144 | + |
| 145 | + {isRunning && ( |
| 146 | + <ClickableList run={currentRun} onMountDuration={handleMountDuration} /> |
| 147 | + )} |
| 148 | + </View> |
| 149 | + ); |
| 150 | +} |
| 151 | + |
| 152 | +const styles = StyleSheet.create({ |
| 153 | + container: { |
| 154 | + flex: 1, |
| 155 | + padding: 20, |
| 156 | + alignItems: 'center', |
| 157 | + }, |
| 158 | + startButton: { |
| 159 | + width: 200, |
| 160 | + height: 50, |
| 161 | + backgroundColor: '#167a5f', |
| 162 | + borderRadius: 10, |
| 163 | + alignItems: 'center', |
| 164 | + justifyContent: 'center', |
| 165 | + }, |
| 166 | + startButtonBusy: { |
| 167 | + backgroundColor: '#7f879b', |
| 168 | + }, |
| 169 | + startButtonText: { |
| 170 | + color: 'white', |
| 171 | + fontWeight: '700', |
| 172 | + }, |
| 173 | + button: { |
| 174 | + width: 200, |
| 175 | + height: 50, |
| 176 | + backgroundColor: 'lightblue', |
| 177 | + borderRadius: 10, |
| 178 | + alignItems: 'center', |
| 179 | + justifyContent: 'center', |
| 180 | + }, |
| 181 | + results: { |
| 182 | + marginTop: 20, |
| 183 | + padding: 16, |
| 184 | + borderRadius: 12, |
| 185 | + backgroundColor: '#eef3fb', |
| 186 | + width: '100%', |
| 187 | + gap: 6, |
| 188 | + }, |
| 189 | + resultText: { |
| 190 | + color: '#33415c', |
| 191 | + fontSize: 13, |
| 192 | + }, |
| 193 | +}); |
0 commit comments