From 28b69fb35774d1389885abd3cfcafb0b0a32fc7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauritz=20Skog=C3=B8y?= Date: Thu, 27 Mar 2025 15:48:50 +0100 Subject: [PATCH] Test: stresstesting file. --- backend/locustTest.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 backend/locustTest.py diff --git a/backend/locustTest.py b/backend/locustTest.py new file mode 100644 index 0000000..15b52c6 --- /dev/null +++ b/backend/locustTest.py @@ -0,0 +1,43 @@ +from locust import HttpUser, task, between + +class SecFitUser(HttpUser): + host = "http://tdt4242-05.idi.ntnu.no:20027" # Replace with your actual backend URL + wait_time = between(1, 3) + + token = None # Store authentication token + exercise_url = None # Store an existing exercise URL + + def on_start(self): + """ Log in and fetch a valid exercise URL when a Locust user starts """ + response = self.client.post("/api/token/", json={ + "username": "locustuser", + "password": "locustpass" + }) + + if response.status_code == 200: + self.token = response.json().get("access") # Store JWT token + print("\nAuthenticated! Token:", self.token) + + # Fetch an existing exercise + headers = {"Authorization": f"Bearer {self.token}"} + exercise_response = self.client.get("/api/exercises/", headers=headers) + if exercise_response.status_code == 200 and len(exercise_response.json()) > 0: + self.exercise_url = exercise_response.json()[0]["url"] # Get first available exercise + print("\nUsing existing exercise:", self.exercise_url) + else: + print("\nNo exercises found! Ensure you have exercises in the database.") + else: + print("\nFailed to authenticate. Response:", response.json()) + + @task + def fetch_exercise_data(self): + """ Simulate fetching an exercise """ + if self.exercise_url: + headers = {"Authorization": f"Bearer {self.token}"} + self.client.get(self.exercise_url, headers=headers) + + @task + def fetch_user_profile(self): + """ Simulate fetching user profile data """ + headers = {"Authorization": f"Bearer {self.token}"} + self.client.get("/api/profile/", headers=headers)