Skip to content

Commit

Permalink
#335 test: add finishAction performance test
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolkenfarmer committed Oct 14, 2024
1 parent de1f8c5 commit 5fe013e
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 39 deletions.
34 changes: 4 additions & 30 deletions testing/backend-stress/src/assignMaterial.js
Original file line number Diff line number Diff line change
@@ -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=')
Expand All @@ -12,17 +12,16 @@ 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++) {
await assignMaterial()
}
const endTime = now();


socketPatient.close()
socketTrainer.close()

Expand All @@ -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 => {
Expand All @@ -83,15 +58,14 @@ 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()
})
})

await new Promise(resolve => {
socketTrainer.materialAdd(areaId, "BZ-Messgerät", exercise => {
//console.log(exercise.areas[0])
materialId = exercise.areas[0].material[0].materialId
resolve()
})
Expand Down
97 changes: 97 additions & 0 deletions testing/backend-stress/src/finishAction.js
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 4 additions & 2 deletions testing/backend-stress/src/main.js
Original file line number Diff line number Diff line change
@@ -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 }
});

Expand Down Expand Up @@ -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();
25 changes: 25 additions & 0 deletions testing/backend-stress/src/setupHelper.js
Original file line number Diff line number Diff line change
@@ -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())
})
}
24 changes: 19 additions & 5 deletions testing/backend-stress/src/sockets/SocketPatient.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class EventCallbacks {
constructor() {
this.testPassthroughs = []
this.resourceAssignments = []
this.actionConfirmationsDeclinations = []
this.actionLists = []
}
}

Expand All @@ -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 = () => {
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions testing/backend-stress/src/sockets/SocketTrainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5fe013e

Please sign in to comment.