From 970f416c974063714cc30174dd79b982643afefd Mon Sep 17 00:00:00 2001 From: Julie Lundberg Suter Date: Thu, 27 Mar 2025 10:31:19 +0100 Subject: [PATCH] locust --- locust.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 locust.py diff --git a/locust.py b/locust.py new file mode 100644 index 0000000..770dc3c --- /dev/null +++ b/locust.py @@ -0,0 +1,46 @@ +from locust import HttpUser, task, between +import random + +class SecFitUser(HttpUser): + host = "http://0.0.0.0:20027" # Match Django backend + wait_time = between(10, 10) # Wait time of exactly 10 seconds between requests + token = None # Store authentication token + exercise_url = None # Store existing exercise URL + + def on_start(self): + """ Log in and fetch a valid exercise URL when a Locust user starts """ + username = "locust_username" + password = "locust_password" + + response = self.client.post("/api/token/", json={ + "username": username, + "password": password + }) + + 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 perform_task(self): + """ Example of a task """ + if self.exercise_url: + response = self.client.get(self.exercise_url) + if response.status_code == 200: + print(f"\nAccessed exercise: {self.exercise_url}") + else: + print(f"\nFailed to access exercise: {self.exercise_url}") + else: + print("\nNo valid exercise URL available.")