EvalueTonSavoir/test/stressTest/main.js

105 lines
3.7 KiB
JavaScript
Raw Normal View History

2024-11-15 17:46:01 -05:00
import { attemptLoginOrRegister, createRoomContainer } from './utility/apiServices.js';
import { Student } from './class/student.js';
import { Teacher } from './class/teacher.js';
const BASE_URL = 'http://localhost';
2024-11-15 19:35:41 -05:00
const user = { username: 'admin@example.com', password: 'adminPassword' };
const numberRooms = 5;
const studentPerRoom = 59; // Max is 60; 1 slot reserved for the teacher
2024-11-15 17:46:01 -05:00
const roomAssociations = {};
const allSockets = []; // Track all active WebSocket connections
2024-11-15 19:35:41 -05:00
async function createRoomContainers(token) {
const roomCreationPromises = Array.from({ length: numberRooms }, async () => {
const room = await createRoomContainer(BASE_URL, token);
if (room?.id) {
roomAssociations[room.id] = { teacher: null, students: [] };
console.log(`Created room with ID: ${room.id}`);
} else {
console.warn('Failed to create a room.');
}
});
2024-11-15 17:46:01 -05:00
await Promise.allSettled(roomCreationPromises);
2024-11-15 19:35:41 -05:00
console.log(`Created ${Object.keys(roomAssociations).length} room containers.`);
2024-11-15 17:46:01 -05:00
}
2024-11-15 19:35:41 -05:00
async function addAndConnectTeachers() {
const teacherCreationPromises = Object.keys(roomAssociations).map(async (roomId, index) => {
const teacher = new Teacher(`teacher_${index}`, roomId);
const socket = await teacher.connectToRoom(BASE_URL);
if (socket.connected) {
allSockets.push(socket);
roomAssociations[roomId].teacher = teacher;
console.log(`Teacher ${teacher.username} connected to room ${roomId}.`);
} else {
console.warn(`Failed to connect teacher_${index} to room ${roomId}`);
2024-11-15 17:46:01 -05:00
}
});
2024-11-15 19:35:41 -05:00
await Promise.allSettled(teacherCreationPromises);
console.log('All teachers added and connected to their respective rooms.');
2024-11-15 17:46:01 -05:00
}
async function addAndConnectStudents() {
2024-11-15 19:35:41 -05:00
const studentCreationPromises = Object.entries(roomAssociations).flatMap(([roomId, association], roomIndex) =>
Array.from({ length: studentPerRoom }, async (_, i) => {
2024-11-15 17:46:01 -05:00
const student = new Student(`student_${roomIndex}_${i}`, roomId);
2024-11-15 19:35:41 -05:00
const socket = await student.connectToRoom(BASE_URL);
if (socket.connected) {
allSockets.push(socket);
association.students.push(student);
console.log(`Student ${student.username} connected to room ${roomId}.`);
} else {
console.warn(`Failed to connect student_${roomIndex}_${i} to room ${roomId}`);
}
})
);
2024-11-15 17:46:01 -05:00
await Promise.allSettled(studentCreationPromises);
2024-11-15 19:35:41 -05:00
console.log('All students added and connected to their respective rooms.');
2024-11-15 17:46:01 -05:00
}
function closeAllSockets() {
2024-11-15 19:35:41 -05:00
console.log('Closing all Socket.IO connections...');
2024-11-15 17:46:01 -05:00
allSockets.forEach((socket) => {
2024-11-15 19:35:41 -05:00
if (socket && socket.connected) {
try {
socket.disconnect();
console.log('Disconnected Socket.IO connection.');
} catch (error) {
console.error('Error disconnecting Socket.IO socket:', error.message);
2024-11-15 17:46:01 -05:00
}
}
});
2024-11-15 19:35:41 -05:00
console.log('All Socket.IO connections disconnected.');
2024-11-15 17:46:01 -05:00
}
async function main() {
try {
const token = await attemptLoginOrRegister(BASE_URL, user.username, user.password);
2024-11-15 19:35:41 -05:00
if (!token) throw new Error('Failed to log in.');
2024-11-15 17:46:01 -05:00
2024-11-15 19:35:41 -05:00
await createRoomContainers(token);
await addAndConnectTeachers();
await addAndConnectStudents();
2024-11-15 17:46:01 -05:00
console.log('All tasks completed.');
} catch (error) {
console.error('An error occurred:', error.message);
}
}
2024-11-15 19:35:41 -05:00
// Handle script termination and exit
2024-11-15 17:46:01 -05:00
process.on('SIGINT', () => {
console.log('Script interrupted (Ctrl+C).');
closeAllSockets();
2024-11-15 19:35:41 -05:00
process.exit(0);
2024-11-15 17:46:01 -05:00
});
2024-11-15 19:35:41 -05:00
process.on('exit', closeAllSockets);
2024-11-15 17:46:01 -05:00
main();