Skip to content

Commit

Permalink
#335 test: add phaseChange performance test
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolkenfarmer committed Oct 16, 2024
1 parent d11e705 commit cf7ed33
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion testing/backend-stress/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
});

Expand Down
82 changes: 82 additions & 0 deletions testing/backend-stress/src/phaseChange.js
Original file line number Diff line number Diff line change
@@ -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);
7 changes: 7 additions & 0 deletions testing/backend-stress/src/sockets/SocketPatient.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class EventCallbacks {
this.resourceAssignments = []
this.actionConfirmationsDeclinations = []
this.actionLists = []
this.states = []
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -217,4 +220,8 @@ export class SocketPatient {
'messageType': 'action-check-stop',
}));
}

addStateCb(cb) {
this.callbacks.states.push(cb);
}
}

0 comments on commit cf7ed33

Please sign in to comment.