Skip to content

Commit

Permalink
chore: improve performance demo reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfran committed Feb 24, 2025
1 parent 5717b67 commit 0675279
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 47 deletions.
4 changes: 1 addition & 3 deletions apps/api/src/app/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ export class Client {
return;
}

if (validationResult.length) {
this.updateSendAllStateActions(validationResult);
}
this.updateSendAllStateActions(validationResult);
}

unauthorizedClose(disconnect = true) {
Expand Down
79 changes: 35 additions & 44 deletions apps/api/src/simulate-users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { io } from 'socket.io-client';
import { v4 as uuidv4 } from 'uuid';
import customParser from 'socket.io-msgpack-parser';
import readline from 'node:readline';

/*
Tested scenario:
Expand All @@ -9,7 +10,7 @@ pnpm build
pnpm serve-dist
pnpm demo
50+fps
55+fps
const NUM_CONNECTIONS = 100;
const PORT = 8000;
Expand Down Expand Up @@ -37,24 +38,29 @@ const clientData: Record<
}
> = {};

console.time('time');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

function randomRange(min: number, max: number) {
return Math.random() * (max - min) + min;
}

function getRandomColor() {
return '#' + (((1 << 24) * Math.random()) | 0).toString(16).padStart(6, '0');
}

function createClient(index: number) {
return new Promise((resolve) => {
const clientId = `user_${index}`;
let interval: NodeJS.Timeout;
let intervalTimeout: NodeJS.Timeout;
let emits = 0;
let init = false;
const noteId = uuidv4();
const ownerId = `user_${index}_${Date.now()}`;
const randomInitX = Math.random() * 1000;
const randomInitY = Math.random() * 1000;
let lastMessage = Date.now();

clientData[clientId] = {
totalMessages: 0,
Expand All @@ -70,7 +76,6 @@ function createClient(index: number) {

const close = (reason: string) => {
clearInterval(interval);
clearInterval(intervalTimeout);

resolve({
clientId,
Expand Down Expand Up @@ -113,7 +118,7 @@ function createClient(index: number) {
content: {
text: `Client ${index} ${Math.random()}`,
position: { x, y },
color: `#${Math.floor(Math.random() * 16777215).toString(16)}`,
color: getRandomColor(),
},
},
position: randomRange(0, NUM_CONNECTIONS - 1),
Expand All @@ -136,6 +141,7 @@ function createClient(index: number) {

console.log(`Client ${index} finished`);
clearInterval(interval);
close('Finished');
}
}, STEP_DELAY);
}
Expand All @@ -144,18 +150,18 @@ function createClient(index: number) {
console.error(`Error client ${index}:`, err.message);
});

ws.on('connect', () => {
console.log(`Client ${index} connected`);
ws.on('disconnect', () => {
close(`Client ${index} disconnected`);
});

ws.on('connect_error', (err) => {
console.error(`Error client ${index}:`, err.message);
});

ws.emit('board', [
{
boardId: BOARD_ID,
action: 'join',
},
]);
ws.on('connect', async () => {
console.log(`Client ${index} connected`);

ws.on('board', (response) => {
lastMessage = Date.now();
clientData[clientId].totalMessages++;

if (response && response[0].type === '[Board] State action' && !init) {
Expand All @@ -175,7 +181,7 @@ function createClient(index: number) {
height: 300,
ownerId: ownerId,
layer: 0,
color: `#${Math.floor(Math.random() * 16777215).toString(16)}`,
color: getRandomColor(),
position: { x: initialX, y: initialY },
},
},
Expand All @@ -185,45 +191,30 @@ function createClient(index: number) {

setTimeout(() => {
start();
}, 500);
}, 200);
}
});

intervalTimeout = setInterval(() => {
let timeout = 500;

if (!init) {
timeout = 1000;
}

const diff = Date.now() - lastMessage;

if (diff > timeout) {
close(`Client ${index} timeout ${diff}`);
}
// some joins are lost withou setTimeout
setTimeout(() => {
ws.emit('board', [
{
boardId: BOARD_ID,
action: 'join',
},
]);
}, 100);

ws.on('disconnect', () => {
close(`Client ${index} disconnected`);
});
});

ws.on('connect_error', (err) => {
console.error(`Error client ${index}:`, err.message);
});
});
}

const clients = [];
const clients: ReturnType<typeof createClient>[] = [];

for (let i = 0; i < NUM_CONNECTIONS; i++) {
clients.push(createClient(i));
}

Promise.all(clients).then((results) => {
console.log(results);

console.timeEnd('time');

process.exit(0);
rl.question('Print results', () => {
console.log('Results: ' + Object.keys(clientData).length);
console.log(clientData);
});

0 comments on commit 0675279

Please sign in to comment.