Skip to content

Commit 53c0178

Browse files
authored
feat: render benchmark results in app (#1)
1 parent f162565 commit 53c0178

File tree

1 file changed

+132
-78
lines changed

1 file changed

+132
-78
lines changed

example/index.tsx

+132-78
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,123 @@
11
import '@bacons/text-decoder/install'
22

3-
import { AppRegistry, View } from 'react-native'
3+
import { useCallback, useState } from 'react'
4+
import { AppRegistry } from 'react-native'
5+
import { ActivityIndicator, Button, StyleSheet, Text, View } from 'react-native'
46
import { WebSocket as FastWebSocket } from 'react-native-fast-ws'
57

8+
type Result = {
9+
outgoingTime: number
10+
incomingTime: number
11+
incomingFirstMessageTime: number
12+
}
13+
614
function App() {
7-
return <View />
15+
return (
16+
<View style={styles.container}>
17+
<TestCase payload="Hello World" title="Test String" />
18+
<TestCase payload={new TextEncoder().encode('Hello World')} title="Test Binary" />
19+
</View>
20+
)
821
}
922

10-
AppRegistry.registerComponent('NitroPlayground', () => App)
23+
function Results({
24+
fastResults,
25+
wsResults,
26+
}: {
27+
title: string
28+
fastResults: Result
29+
wsResults: Result
30+
}) {
31+
return (
32+
<View>
33+
<View style={styles.resultRowContainer}>
34+
<Text style={styles.resultItem}> </Text>
35+
<Text style={styles.resultItem}>{`FastWS`}</Text>
36+
<Text style={styles.resultItem}>{`WebSocket`}</Text>
37+
</View>
38+
<ResultsRow
39+
title="Sending (ms)"
40+
fastResult={fastResults.outgoingTime}
41+
wsResult={wsResults.outgoingTime}
42+
/>
43+
<ResultsRow
44+
title="Received (ms)"
45+
fastResult={fastResults.incomingTime}
46+
wsResult={wsResults.incomingTime}
47+
/>
48+
<ResultsRow
49+
title="TFM (ms)"
50+
fastResult={fastResults.incomingFirstMessageTime}
51+
wsResult={wsResults.incomingFirstMessageTime}
52+
/>
53+
</View>
54+
)
55+
}
56+
57+
function ResultsRow({
58+
title,
59+
fastResult,
60+
wsResult,
61+
}: {
62+
title: string
63+
fastResult: number
64+
wsResult: number
65+
}) {
66+
const isFastBetter = fastResult < wsResult
67+
return (
68+
<View style={styles.resultRowContainer}>
69+
<Text style={styles.resultItem}>{title}</Text>
70+
<ResultItem result={fastResult} isBetter={isFastBetter} />
71+
<ResultItem result={wsResult} isBetter={!isFastBetter} />
72+
</View>
73+
)
74+
}
75+
76+
function ResultItem({ result, isBetter }: { result: number; isBetter: boolean }) {
77+
return (
78+
<Text
79+
style={[styles.resultItem, isBetter ? styles.betterResult : styles.worseResult]}
80+
>{`${result.toFixed(2)}`}</Text>
81+
)
82+
}
83+
84+
function TestCase({ payload, title }: { payload: string | ArrayBuffer; title: string }) {
85+
const [fastResult, setFastResult] = useState<Result | null>(null)
86+
const [wsResult, setWsResult] = useState<Result | null>(null)
87+
const [loading, setLoading] = useState(false)
88+
const runTest = useCallback(async () => {
89+
if (loading) {
90+
return
91+
}
92+
setLoading(true)
93+
const fastRes = await testWebsocketMessages({
94+
// @ts-ignore
95+
Ws: FastWebSocket,
96+
outgoing: OUTGOING,
97+
incoming: INCOMING,
98+
payload,
99+
})
100+
setFastResult(fastRes)
101+
const wsRes = await testWebsocketMessages({
102+
Ws: WebSocket,
103+
outgoing: OUTGOING,
104+
incoming: INCOMING,
105+
payload,
106+
})
107+
setWsResult(wsRes)
108+
setLoading(false)
109+
}, [loading])
110+
111+
return (
112+
<View>
113+
<Button onPress={runTest} title={title} />
114+
{fastResult && wsResult && !loading && (
115+
<Results fastResults={fastResult} wsResults={wsResult} title={title} />
116+
)}
117+
{loading && <ActivityIndicator />}
118+
</View>
119+
)
120+
}
11121

12122
const testWebsocketMessages = async (opts: {
13123
Ws: new (uri: string) => WebSocket
@@ -66,79 +176,23 @@ const testWebsocketMessages = async (opts: {
66176
const INCOMING = 10000
67177
const OUTGOING = 10000
68178

69-
setTimeout(async () => {
70-
const results: [string, string, number, number, number][] = []
71-
72-
const resFastBinary = await testWebsocketMessages({
73-
// @ts-ignore
74-
Ws: FastWebSocket,
75-
outgoing: OUTGOING,
76-
incoming: INCOMING,
77-
payload: new TextEncoder().encode('Hello World'),
78-
})
79-
80-
results.push([
81-
'FastWS',
82-
'Binary',
83-
resFastBinary.outgoingTime,
84-
resFastBinary.incomingTime,
85-
resFastBinary.incomingFirstMessageTime,
86-
])
87-
88-
const resWebSocketBinary = await testWebsocketMessages({
89-
Ws: WebSocket,
90-
outgoing: OUTGOING,
91-
incoming: INCOMING,
92-
payload: new TextEncoder().encode('Hello World'),
93-
})
94-
95-
results.push([
96-
'WebSocket',
97-
'Binary',
98-
resWebSocketBinary.outgoingTime,
99-
resWebSocketBinary.incomingTime,
100-
resWebSocketBinary.incomingFirstMessageTime,
101-
])
102-
103-
const resFastString = await testWebsocketMessages({
104-
// @ts-ignore
105-
Ws: FastWebSocket,
106-
outgoing: OUTGOING,
107-
incoming: INCOMING,
108-
payload: 'Hello World',
109-
})
110-
111-
results.push([
112-
'FastWS',
113-
'String',
114-
resFastString.outgoingTime,
115-
resFastString.incomingTime,
116-
resFastString.incomingFirstMessageTime,
117-
])
118-
119-
const resWebSocketString = await testWebsocketMessages({
120-
Ws: WebSocket,
121-
outgoing: OUTGOING,
122-
incoming: INCOMING,
123-
payload: 'Hello World',
124-
})
179+
const styles = StyleSheet.create({
180+
container: {
181+
marginTop: 100,
182+
},
183+
resultRowContainer: {
184+
flexDirection: 'row',
185+
padding: 10,
186+
},
187+
resultItem: {
188+
flex: 1,
189+
},
190+
betterResult: {
191+
backgroundColor: 'green',
192+
},
193+
worseResult: {
194+
backgroundColor: 'red',
195+
},
196+
})
125197

126-
results.push([
127-
'WebSocket',
128-
'String',
129-
resWebSocketString.outgoingTime,
130-
resWebSocketString.incomingTime,
131-
resWebSocketString.incomingFirstMessageTime,
132-
])
133-
134-
// Print the table header
135-
console.log('Type | Payload | Sending (ms) | Received (ms) | TFM (ms)')
136-
console.log('-----------------------------------------------------------------')
137-
138-
// Print each row of results
139-
results.forEach((result) => {
140-
console.log(
141-
`${result[0].padEnd(10)} | ${result[1].padEnd(7)} | ${result[2].toFixed(2).padEnd(12)} | ${result[3].toFixed(2).padEnd(13)} | ${result[4].toFixed(2).padEnd(8)}`
142-
)
143-
})
144-
}, 1000)
198+
AppRegistry.registerComponent('NitroPlayground', () => App)

0 commit comments

Comments
 (0)