diff --git a/testing/backend-stress/src/assignMaterial.js b/testing/backend-stress/src/assignMaterial.js index 567b5f7f..e40f1542 100644 --- a/testing/backend-stress/src/assignMaterial.js +++ b/testing/backend-stress/src/assignMaterial.js @@ -1,8 +1,8 @@ import { parentPort, workerData } from "worker_threads"; -import axios from "axios"; import now from "performance-now"; import {SocketTrainer} from "./sockets/SocketTrainer.js" import {SocketPatient} from "./sockets/SocketPatient.js"; +import {connectPatient, connectTrainer} from "./setupHelper.js"; const socketTrainer = new SocketTrainer('http://localhost/ws/trainer/?token=') const socketPatient = new SocketPatient('http://localhost/ws/patient/?token=') @@ -12,9 +12,9 @@ async function simulate(userIndex) { const trainerName = `testuser${crypto.randomUUID()}` try { - await connectTrainer(trainerName) + await connectTrainer(socketTrainer, trainerName) await prepareExercise() - await connectPatient() + await connectPatient(socketPatient, exerciseId, patientId) const startTime = now(); for (let i = 0; i < 100; i++) { @@ -22,7 +22,6 @@ async function simulate(userIndex) { } const endTime = now(); - socketPatient.close() socketTrainer.close() @@ -43,30 +42,6 @@ async function simulate(userIndex) { parentPort.close() } -async function connectTrainer(trainerName) { - const response = await axios.post(`http://localhost/api/trainer/login`, { - username: trainerName, - password: 'password123' - }) - const token = response.data.token; - - await new Promise(resolve => { - socketTrainer.connect(token, () => resolve()) - }) -} - -async function connectPatient() { - const response = await axios.post(`http://localhost/api/patient/access`, { - exerciseId: exerciseId, - patientId: patientId, - }) - const token = response.data.token; - - await new Promise(resolve => { - socketPatient.connect(token, () => resolve()) - }) -} - async function prepareExercise() { await new Promise(resolve => { socketTrainer.exerciseCreate(exercise => { @@ -83,7 +58,7 @@ async function prepareExercise() { }) await new Promise(resolve => { - socketTrainer.patientAdd(areaId, "W", 1001, exercise => { + socketTrainer.patientAdd(areaId, "W", 1005, exercise => { patientId = exercise.areas[0].patients[0].patientId resolve() }) @@ -91,7 +66,6 @@ async function prepareExercise() { await new Promise(resolve => { socketTrainer.materialAdd(areaId, "BZ-Messgerät", exercise => { - //console.log(exercise.areas[0]) materialId = exercise.areas[0].material[0].materialId resolve() }) diff --git a/testing/backend-stress/src/finishAction.js b/testing/backend-stress/src/finishAction.js new file mode 100644 index 00000000..d6a7dfbe --- /dev/null +++ b/testing/backend-stress/src/finishAction.js @@ -0,0 +1,97 @@ +import {parentPort, workerData} from "worker_threads"; +import now from "performance-now"; +import {SocketTrainer} from "./sockets/SocketTrainer.js" +import {SocketPatient} from "./sockets/SocketPatient.js"; +import {connectPatient, connectTrainer} from "./setupHelper.js"; + +const socketTrainer = new SocketTrainer('http://localhost/ws/trainer/?token=') +const socketPatient = new SocketPatient('http://localhost/ws/patient/?token=') +let exerciseId, areaId, patientId, personnelId + +async function simulate(userIndex) { + const trainerName = `testuser${crypto.randomUUID()}` + + try { + await connectTrainer(socketTrainer, trainerName) + await prepareExercise() + await connectPatient(socketPatient, exerciseId, patientId) + + await new Promise(resolve => { + socketPatient.assignPersonnel(personnelId, () => resolve()) + }) + + const startTime = now(); + await doAction() + const endTime = now(); + + + socketPatient.close() + socketTrainer.close() + + parentPort.postMessage({ + userIndex, + responseTime: (endTime - startTime) - (15000/10), // subtract execution time of action + success: true + }); + parentPort.close() + } catch (error) { + parentPort.postMessage({ + userIndex, + responseTime: now() - startTime, + success: false, + error: error.message + }); + } + parentPort.close() +} + +async function prepareExercise() { + await new Promise(resolve => { + socketTrainer.exerciseCreate(exercise => { + exerciseId = exercise.exerciseId + resolve() + }) + }) + + await new Promise(resolve => { + socketTrainer.areaAdd(exercise => { + areaId = exercise.areas[0].areaId + resolve(true) + }) + }) + + await new Promise(resolve => { + socketTrainer.patientAdd(areaId, "W", 1001, exercise => { + patientId = exercise.areas[0].patients[0].patientId + resolve() + }) + }) + + await new Promise(resolve => { + socketTrainer.patientAdd(areaId, "W", 1001, exercise => { + patientId = exercise.areas[0].patients[0].patientId + resolve() + }) + }) + + await new Promise(resolve => { + socketTrainer.personnelAdd(areaId, "", exercise => { + personnelId = exercise.areas[0].personnel[0].personnelId + resolve() + }) + }) + + await new Promise(resolve => { + socketTrainer.exerciseStart(() => resolve()) + }) +} + +async function doAction() { + await new Promise(resolve => { + socketPatient.actionAdd("Turniquet", confirmed => { + if (!confirmed) throw Error("action declined") + }, () => resolve()) + }) +} + +simulate(workerData.userIndex); \ No newline at end of file diff --git a/testing/backend-stress/src/main.js b/testing/backend-stress/src/main.js index 04d27410..e65e6dd4 100644 --- a/testing/backend-stress/src/main.js +++ b/testing/backend-stress/src/main.js @@ -1,13 +1,13 @@ import { Worker } from 'worker_threads'; -const NUM_EXERCISES = 100 +const NUM_EXERCISES = 2 let results = []; let workerCount = 0; function startWorker(userIndex) { return new Promise((resolve, reject) => { - const worker = new Worker('./assignMaterial.js', { + const worker = new Worker('./finishAction.js', { workerData: { userIndex } }); @@ -36,8 +36,10 @@ async function runStressTest() { const avgResponseTime = results.reduce((acc, curr) => acc + curr.responseTime, 0) / results.length; const variance = results.reduce((acc, curr) => acc + Math.pow(curr.responseTime - avgResponseTime, 2), 0) / results.length; + const stdDeviation = Math.sqrt(variance); console.log(`Average response time: ${avgResponseTime.toFixed(4)} ms`); console.log(`Variance: ${variance.toFixed(4)} ms^2`); + console.log(`Standard deviation: ${stdDeviation.toFixed(4)} ms`); } runStressTest(); diff --git a/testing/backend-stress/src/setupHelper.js b/testing/backend-stress/src/setupHelper.js new file mode 100644 index 00000000..498258bc --- /dev/null +++ b/testing/backend-stress/src/setupHelper.js @@ -0,0 +1,25 @@ +import axios from "axios"; + +export async function connectTrainer(socketTrainer, trainerName) { + const response = await axios.post(`http://localhost/api/trainer/login`, { + username: trainerName, + password: 'password123' + }) + const token = response.data.token; + + await new Promise(resolve => { + socketTrainer.connect(token, () => resolve()) + }) +} + +export async function connectPatient(socketPatient, exerciseId, patientId) { + const response = await axios.post(`http://localhost/api/patient/access`, { + exerciseId: exerciseId, + patientId: patientId, + }) + const token = response.data.token; + + await new Promise(resolve => { + socketPatient.connect(token, () => resolve()) + }) +} \ No newline at end of file diff --git a/testing/backend-stress/src/sockets/SocketPatient.js b/testing/backend-stress/src/sockets/SocketPatient.js index 98519b3f..a0edc95f 100644 --- a/testing/backend-stress/src/sockets/SocketPatient.js +++ b/testing/backend-stress/src/sockets/SocketPatient.js @@ -4,6 +4,8 @@ class EventCallbacks { constructor() { this.testPassthroughs = [] this.resourceAssignments = [] + this.actionConfirmationsDeclinations = [] + this.actionLists = [] } } @@ -19,8 +21,10 @@ export class SocketPatient { this.socket = new WebSocket(this.url + token); this.socket.onopen = () => { - cb() + this.callbacks.actionLists.push(() => {}) + this.callbacks.resourceAssignments.push(() => {}) this.connected = true; + cb() }; this.socket.onclose = () => { @@ -72,10 +76,10 @@ export class SocketPatient { case 'information': break; case 'action-confirmation': + (this.callbacks.actionConfirmationsDeclinations.shift())(true) break; case 'action-declination': - break; - case 'action-result': + (this.callbacks.actionConfirmationsDeclinations.shift())(false) break; case 'resource-assignments': try { @@ -85,6 +89,11 @@ export class SocketPatient { } break; case 'action-list': + try { + (this.callbacks.actionLists.shift())(data.actions) + } catch (e) { + console.error("PatientSocket action-list cb not created"); + } break; case 'visible-injuries': break; @@ -112,7 +121,11 @@ export class SocketPatient { } } - actionAdd(actionName) { + actionAdd(actionName, cb_conf, cb_list) { + this.callbacks.actionConfirmationsDeclinations.push(cb_conf); + this.callbacks.actionLists.push(() => {}); // IP + this.callbacks.actionLists.push(() => {}); // IP #2??? + this.callbacks.actionLists.push(cb_list); // FI this.sendMessage(JSON.stringify({ 'messageType': 'action-add', 'actionName': actionName, @@ -138,7 +151,8 @@ export class SocketPatient { })); } - assignPersonnel(personnelId) { + assignPersonnel(personnelId, cb) { + this.callbacks.resourceAssignments.push(cb); this.sendMessage(JSON.stringify({ 'messageType': 'personnel-assign', 'personnelId': personnelId, diff --git a/testing/backend-stress/src/sockets/SocketTrainer.js b/testing/backend-stress/src/sockets/SocketTrainer.js index 1280fe61..de4eb889 100644 --- a/testing/backend-stress/src/sockets/SocketTrainer.js +++ b/testing/backend-stress/src/sockets/SocketTrainer.js @@ -60,7 +60,7 @@ export class SocketTrainer { try { (this.callbacks.exercises.shift())(data.exercise); } catch (e) { - console.error("TrainerSocket exercise cb not created"); + // console.error("TrainerSocket exercise cb not created"); } break; case 'exercise-start': @@ -174,7 +174,8 @@ export class SocketTrainer { })); } - personnelAdd(areaId, personnelName) { + personnelAdd(areaId, personnelName, cb) { + this.callbacks.exercises.push(cb) this.sendMessage(JSON.stringify({ 'messageType': 'personnel-add', 'areaId': areaId,