UNPKG

open-easyrtc

Version:

Open-EasyRTC enables quick development of WebRTC

154 lines (130 loc) 4.46 kB
const io = require('socket.io-client'); process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; class ConnectionBenchmark { constructor(host, port, maxConnections) { this.serverUrl = `${host}:${port}`; this.connections = new Map(); this.rooms = new Map(); this.roomId = 0; this.maxConnections = parseInt(maxConnections); this.activeConnections = 0; this.failedConnections = 0; this.startTime = null; } async run() { this.startTime = Date.now(); console.log(`Starting benchmark: ${this.maxConnections} connections to ${this.serverUrl}`); this.createConnection(1); } createConnection(id) { const client = io(this.serverUrl, { 'force new connection': true, reconnection: true, rejectUnauthorized: false, transports: ['websocket'], extraHeaders: { origin: this.serverUrl } }); this.connections.set(id, client) var connected = false; client.on('connect', () => { const authMsg = { msgType: 'authenticate', msgData: { apiVersion: "1.0.11", applicationName: "sylaps.com", username: `test-${id}`, roomJoin: {}, setPresence: { show: 'chat', status: '' } } }; let targetRoom = `test${this.roomId}` let room = this.rooms.get(targetRoom) || 1 if (room > 12) { this.roomId = this.roomId + 1; room = 1 targetRoom = `test${this.roomId}` } room++ this.rooms.set(targetRoom, room) console.log(`Connection ${id} to ${targetRoom}...`) client.emit('easyrtcAuth', authMsg, (response) => { if (response.msgType === 'error') { console.error(`Authentication failed for connection ${id}:`, response); this.failedConnections++; client.disconnect(); } else { connected = true this.activeConnections++; console.log(`Connection ${id} established. Active: ${this.activeConnections}`); const dataToShip = { msgType: 'roomJoin', msgData: { roomJoin: { [targetRoom]: { roomName: targetRoom, roomParameters: {}, } } } } for (var i = 0; i < 10; i++) { setTimeout(() => { client.emit('easyrtcCmd', dataToShip, (response) => { if (response.msgType === 'error') { console.error(`Join room ${targetRoom} failed for connection ${id}:`, response); } else { console.error(`Join room ${targetRoom} success for connection ${id}:`); const outgoingMessage = { targetRoom: targetRoom, msgType: 'msg', msgData: { id: Date.now(), type: 'msg', content: "Hello", creation: Date.now(), user: { fields: { username: `test-${id}`, } } } } client.emit('easyrtcMsg', outgoingMessage, (response) => { if (response.msgType === 'error') { console.error(`Send message ${targetRoom} failed for connection ${id}:`, response); } else { console.error(`Send message ${targetRoom} success for connection ${id}:`); } }) } }) }, 10) } } }); }); client.on('connect_error', (error) => { console.error(`Connection ${id} failed:`, error); this.failedConnections++; }); client.on('disconnect', () => { if (connected) { console.log(`Disconnect ${id}`); this.activeConnections--; } else { console.error(`Connection ${id} failed`); } }); } } // Usage const SERVER_HOST = process.argv[2] || 'https://localhost'; const SERVER_PORT = process.argv[3] || '8443'; // Replace with actual port const MAX_CONNECTIONS = process.argv[4] || 100; // Adjust as needed const benchmark = new ConnectionBenchmark(SERVER_HOST, SERVER_PORT, MAX_CONNECTIONS); benchmark.run();