Skip to content

Commit b67a1d3

Browse files
authored
feat: implement benchmark app (#13)
* feat: initial commit * chore: remove % * chore: add accumulator into the table
1 parent e4f3693 commit b67a1d3

File tree

9 files changed

+480
-224
lines changed

9 files changed

+480
-224
lines changed

example/App.tsx

-190
This file was deleted.

example/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import '@bacons/text-decoder/install'
22

33
import { AppRegistry } from 'react-native'
44

5-
import { App } from './App'
5+
import { BenchmarkUI } from './tests/benchmark'
66

7-
AppRegistry.registerComponent('NitroPlayground', () => App)
7+
AppRegistry.registerComponent('NitroPlayground', () => BenchmarkUI)

example/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"android": "react-native run-android",
66
"ios": "react-native run-ios",
77
"start": "react-native start",
8-
"server": "bun server.ts"
8+
"server": "bun server/index.ts"
99
},
1010
"dependencies": {
1111
"@bacons/text-decoder": "^0.0.0",

example/server.ts

-31
This file was deleted.

example/server/createServer.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// <reference types="bun-types" />
2+
3+
/**
4+
* Creates a WebSocket server that sends configurable responses
5+
*/
6+
export function createServer(payload: string | Bun.BufferSource, port: number) {
7+
const server = Bun.serve({
8+
fetch(req, server) {
9+
if (server.upgrade(req)) {
10+
return
11+
}
12+
return new Response('Upgrade failed', { status: 500 })
13+
},
14+
websocket: {
15+
message(ws, message: string) {
16+
if (typeof message !== 'string') {
17+
return
18+
}
19+
const count = parseInt(message)
20+
if (isNaN(count)) {
21+
return
22+
}
23+
try {
24+
for (let i = 0; i < count; i++) {
25+
ws.send(payload)
26+
}
27+
} catch (error) {
28+
console.error('Failed to parse message:', error)
29+
}
30+
},
31+
},
32+
port,
33+
})
34+
35+
console.log(`WebSocket server listening on ws://localhost:${port}`)
36+
37+
// Return cleanup function
38+
return () => server.stop()
39+
}

example/server/img.jpg

229 KB
Loading

example/server/index.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { createServer } from './createServer'
2+
import {
3+
CHAT_PAYLOAD,
4+
CHAT_PAYLOAD_BINARY,
5+
CHAT_SERVER_BINARY_PORT,
6+
CHAT_SERVER_PORT,
7+
COORDINATE_PAYLOAD,
8+
COORDINATE_PAYLOAD_BINARY,
9+
COORDINATE_SERVER_BINARY_PORT,
10+
COORDINATE_SERVER_PORT,
11+
IMAGE_SERVER_PORT,
12+
} from './payloads'
13+
14+
async function run() {
15+
const stopChatServer = createServer(CHAT_PAYLOAD, CHAT_SERVER_PORT)
16+
const stopChatBinaryServer = createServer(CHAT_PAYLOAD_BINARY, CHAT_SERVER_BINARY_PORT)
17+
const stopCoordinateServer = createServer(COORDINATE_PAYLOAD, COORDINATE_SERVER_PORT)
18+
const stopCoordinateBinaryServer = createServer(
19+
COORDINATE_PAYLOAD_BINARY,
20+
COORDINATE_SERVER_BINARY_PORT
21+
)
22+
const stopImageServer = createServer(
23+
await Bun.file(__dirname + '/img.jpg').arrayBuffer(),
24+
IMAGE_SERVER_PORT
25+
)
26+
27+
const stopAllServers = () => {
28+
stopChatServer()
29+
stopChatBinaryServer()
30+
stopCoordinateServer()
31+
stopCoordinateBinaryServer()
32+
stopImageServer()
33+
}
34+
35+
process.on('SIGINT', () => {
36+
stopAllServers()
37+
process.exit(0)
38+
})
39+
}
40+
41+
run()

example/server/payloads.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* This file contains the payloads that are used in the test.
3+
*/
4+
5+
const encoder = new TextEncoder()
6+
7+
export const CHAT_PAYLOAD = JSON.stringify({
8+
id: '123e4567-e89b-12d3-a456-426614174000',
9+
type: 'message',
10+
content: {
11+
text: 'Hello! How are you doing today? 👋',
12+
mentions: ['@mike'],
13+
attachments: [],
14+
},
15+
room: {
16+
id: 'room_456',
17+
type: 'direct',
18+
},
19+
metadata: {
20+
client: 'react-native-fast-ws/1.0.0',
21+
},
22+
})
23+
24+
export const CHAT_PAYLOAD_BINARY = encoder.encode(JSON.stringify(CHAT_PAYLOAD))
25+
26+
export const COORDINATE_PAYLOAD = JSON.stringify({
27+
coords: [456, 750],
28+
})
29+
export const COORDINATE_PAYLOAD_BINARY = encoder.encode(JSON.stringify(COORDINATE_PAYLOAD))
30+
31+
export const CHAT_SERVER_PORT = 3000
32+
export const CHAT_SERVER_BINARY_PORT = 3001
33+
34+
export const COORDINATE_SERVER_PORT = 3002
35+
export const COORDINATE_SERVER_BINARY_PORT = 3003
36+
37+
export const IMAGE_SERVER_PORT = 3004

0 commit comments

Comments
 (0)