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