Skip to content

Commit

Permalink
Test: stresstesting file.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauritzs committed Mar 27, 2025
1 parent 20cfd4a commit 28b69fb
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions backend/locustTest.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 28b69fb

Please sign in to comment.