From cf7ed33d403badf978318f2c2788b6456d22a70d Mon Sep 17 00:00:00 2001 From: Wolkenfarmer Date: Wed, 16 Oct 2024 16:08:14 +0200 Subject: [PATCH] #335 test: add phaseChange performance test --- testing/backend-stress/src/main.js | 2 +- testing/backend-stress/src/phaseChange.js | 82 +++++++++++++++++++ .../src/sockets/SocketPatient.js | 7 ++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 testing/backend-stress/src/phaseChange.js diff --git a/testing/backend-stress/src/main.js b/testing/backend-stress/src/main.js index e65e6dd4..11b9e138 100644 --- a/testing/backend-stress/src/main.js +++ b/testing/backend-stress/src/main.js @@ -7,7 +7,7 @@ let workerCount = 0; function startWorker(userIndex) { return new Promise((resolve, reject) => { - const worker = new Worker('./finishAction.js', { + const worker = new Worker('./phaseChange.js', { workerData: { userIndex } }); diff --git a/testing/backend-stress/src/phaseChange.js b/testing/backend-stress/src/phaseChange.js new file mode 100644 index 00000000..15f7ae99 --- /dev/null +++ b/testing/backend-stress/src/phaseChange.js @@ -0,0 +1,82 @@ +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 phaseLength = 30000 / 10 + +const socketTrainer = new SocketTrainer('http://localhost/ws/trainer/?token=') +const socketPatient = new SocketPatient('http://localhost/ws/patient/?token=') +let exerciseId, areaId, patientId + +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 => { + socketTrainer.exerciseStart(() => { + resolve() + }) + }) + + let responseTime = 0 + for (let i = 0; i < 5; i++) { + let startTime = now(); + await new Promise(resolve => { + socketPatient.addStateCb(() => { + resolve() + }) + }) + let endTime = now(); + responseTime += (endTime - startTime) - phaseLength + } + + socketPatient.close() + socketTrainer.close() + + parentPort.postMessage({ + userIndex, + responseTime: responseTime / 5, + 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, "", 1013, exercise => { + patientId = exercise.areas[0].patients[0].patientId + resolve() + }) + }) +} + +simulate(workerData.userIndex); \ 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 a0edc95f..f8ab54ca 100644 --- a/testing/backend-stress/src/sockets/SocketPatient.js +++ b/testing/backend-stress/src/sockets/SocketPatient.js @@ -6,6 +6,7 @@ class EventCallbacks { this.resourceAssignments = [] this.actionConfirmationsDeclinations = [] this.actionLists = [] + this.states = [] } } @@ -21,6 +22,7 @@ export class SocketPatient { this.socket = new WebSocket(this.url + token); this.socket.onopen = () => { + this.callbacks.states.push(() => {}) this.callbacks.actionLists.push(() => {}) this.callbacks.resourceAssignments.push(() => {}) this.connected = true; @@ -54,6 +56,7 @@ export class SocketPatient { (this.callbacks.testPassthroughs.shift())(data.message) break; case 'state': + (this.callbacks.states.shift())(data.state) break; case 'continuous-variable': break; @@ -217,4 +220,8 @@ export class SocketPatient { 'messageType': 'action-check-stop', })); } + + addStateCb(cb) { + this.callbacks.states.push(cb); + } } \ No newline at end of file