Skip to content

feat: implement benchmark app #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 0 additions & 190 deletions example/App.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import '@bacons/text-decoder/install'

import { AppRegistry } from 'react-native'

import { App } from './App'
import { BenchmarkUI } from './tests/benchmark'

AppRegistry.registerComponent('NitroPlayground', () => App)
AppRegistry.registerComponent('NitroPlayground', () => BenchmarkUI)
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"server": "bun server.ts"
"server": "bun server/index.ts"
},
"dependencies": {
"@bacons/text-decoder": "^0.0.0",
Expand Down
31 changes: 0 additions & 31 deletions example/server.ts

This file was deleted.

39 changes: 39 additions & 0 deletions example/server/createServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// <reference types="bun-types" />

/**
* Creates a WebSocket server that sends configurable responses
*/
export function createServer(payload: string | Bun.BufferSource, port: number) {
const server = Bun.serve({
fetch(req, server) {
if (server.upgrade(req)) {
return
}
return new Response('Upgrade failed', { status: 500 })
},
websocket: {
message(ws, message: string) {
if (typeof message !== 'string') {
return
}
const count = parseInt(message)
if (isNaN(count)) {
return
}
try {
for (let i = 0; i < count; i++) {
ws.send(payload)
}
} catch (error) {
console.error('Failed to parse message:', error)
}
},
},
port,
})

console.log(`WebSocket server listening on ws://localhost:${port}`)

// Return cleanup function
return () => server.stop()
}
Binary file added example/server/img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions example/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { createServer } from './createServer'
import {
CHAT_PAYLOAD,
CHAT_PAYLOAD_BINARY,
CHAT_SERVER_BINARY_PORT,
CHAT_SERVER_PORT,
COORDINATE_PAYLOAD,
COORDINATE_PAYLOAD_BINARY,
COORDINATE_SERVER_BINARY_PORT,
COORDINATE_SERVER_PORT,
IMAGE_SERVER_PORT,
} from './payloads'

async function run() {
const stopChatServer = createServer(CHAT_PAYLOAD, CHAT_SERVER_PORT)
const stopChatBinaryServer = createServer(CHAT_PAYLOAD_BINARY, CHAT_SERVER_BINARY_PORT)
const stopCoordinateServer = createServer(COORDINATE_PAYLOAD, COORDINATE_SERVER_PORT)
const stopCoordinateBinaryServer = createServer(
COORDINATE_PAYLOAD_BINARY,
COORDINATE_SERVER_BINARY_PORT
)
const stopImageServer = createServer(
await Bun.file(__dirname + '/img.jpg').arrayBuffer(),
IMAGE_SERVER_PORT
)

const stopAllServers = () => {
stopChatServer()
stopChatBinaryServer()
stopCoordinateServer()
stopCoordinateBinaryServer()
stopImageServer()
}

process.on('SIGINT', () => {
stopAllServers()
process.exit(0)
})
}

run()
37 changes: 37 additions & 0 deletions example/server/payloads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* This file contains the payloads that are used in the test.
*/

const encoder = new TextEncoder()

export const CHAT_PAYLOAD = JSON.stringify({
id: '123e4567-e89b-12d3-a456-426614174000',
type: 'message',
content: {
text: 'Hello! How are you doing today? 👋',
mentions: ['@mike'],
attachments: [],
},
room: {
id: 'room_456',
type: 'direct',
},
metadata: {
client: 'react-native-fast-ws/1.0.0',
},
})

export const CHAT_PAYLOAD_BINARY = encoder.encode(JSON.stringify(CHAT_PAYLOAD))

export const COORDINATE_PAYLOAD = JSON.stringify({
coords: [456, 750],
})
export const COORDINATE_PAYLOAD_BINARY = encoder.encode(JSON.stringify(COORDINATE_PAYLOAD))

export const CHAT_SERVER_PORT = 3000
export const CHAT_SERVER_BINARY_PORT = 3001

export const COORDINATE_SERVER_PORT = 3002
export const COORDINATE_SERVER_BINARY_PORT = 3003

export const IMAGE_SERVER_PORT = 3004
Loading