Skip to content

Commit

Permalink
Merge branch '335-add-performance-tests-for-front-and-backend-to-eval…
Browse files Browse the repository at this point in the history
…uate-330' into 335-add-performance-tests-for-front-and-backend-to-evaluate-330-2
  • Loading branch information
Wolkenfarmer committed Oct 16, 2024
2 parents 6656c81 + cf7ed33 commit bcb0a70
Show file tree
Hide file tree
Showing 12 changed files with 1,032 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ backend/.vscode/settings.json
.idea/
.vscode/launch.json
backend/dps_training_k/restart_backend.sh
**/node_modules/
2 changes: 1 addition & 1 deletion backend/dps_training_k/game/models/patient_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def apply_pretreatments(self):
def schedule_state_change(self, time_offset=0):
from game.models import ScheduledEvent

state_change_time = 600
state_change_time = 30

if self.patient_state.is_dead:
return False
Expand Down
175 changes: 175 additions & 0 deletions testing/backend-stress/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions testing/backend-stress/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "backend-stress",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"axios": "^1.7.7",
"performance-now": "^2.1.0",
"ws": "^8.18.0"
},
"devDependencies": {
"@types/node": "^22.7.4",
"@types/ws": "^8.5.12",
"typescript": "^5.6.2"
}
}
88 changes: 88 additions & 0 deletions testing/backend-stress/src/assignMaterial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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, materialId

async function simulate(userIndex) {
const trainerName = `testuser${crypto.randomUUID()}`

try {
await connectTrainer(socketTrainer, trainerName)
await prepareExercise()
await connectPatient(socketPatient, exerciseId, patientId)

const startTime = now();
for (let i = 0; i < 100; i++) {
await assignMaterial()
}
const endTime = now();

socketPatient.close()
socketTrainer.close()

parentPort.postMessage({
userIndex,
responseTime: (endTime - startTime) / 200,
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, "", 1005, exercise => {
patientId = exercise.areas[0].patients[0].patientId
resolve()
})
})

await new Promise(resolve => {
socketTrainer.materialAdd(areaId, "BZ-Messgerät", exercise => {
materialId = exercise.areas[0].material[0].materialId
resolve()
})
})

await new Promise(resolve => {
socketTrainer.exerciseStart(() => resolve())
})
}

async function assignMaterial() {
await new Promise(resolve => {
socketPatient.assignMaterial(materialId, () => resolve())
})
await new Promise(resolve => {
socketPatient.releaseMaterial(materialId, () => resolve())
})
}

simulate(workerData.userIndex);
93 changes: 93 additions & 0 deletions testing/backend-stress/src/finishAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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 actionName = "Turniquet"
const actionTime = 15000 / 10

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) - actionTime, // 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, "", 1005, 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(actionName, confirmed => {
if (!confirmed) throw Error("action declined")
}, () => resolve())
})
}

simulate(workerData.userIndex);
Loading

0 comments on commit bcb0a70

Please sign in to comment.