Skip to content

Commit

Permalink
added the locust file
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingGirlGargi committed Apr 3, 2025
1 parent dac7c38 commit 8e31fbc
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions backend/locust_athlete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from locust import HttpUser, task, between

class SecFitUser(HttpUser):
host = "http://tdt4242-18.idi.ntnu.no:21817" # URL of the Django backend
wait_time = between(1, 3) # Users will wait 1 to 3 seconds between tasks
token = None # To store the authentication token
exercise_url = None # To store the URL of an existing exercise

def on_start(self):
"""Authenticate and prepare before sending requests."""
# Authentication request
response = self.client.post("/api/token/", json={
"username": "test-athlete", # Replace with actual username
"password": "test-athlete-password" # Replace with actual password
})

# If authentication succeeds
if response.status_code == 200:
self.token = response.json().get("access") # Store the token
print("\nAuthenticated! Token:", self.token)

# Fetch an existing exercise using the token
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 the first exercise URL
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 add_exercise(self):
"""Send a POST request to create a new exercise."""
if self.token:
headers = {"Authorization": f"Bearer {self.token}"}
exercise_data = {
"name": "Push-ups", # Replace with the desired exercise name
"description": "Upper body strength exercise", # Replace with a meaningful description
"unit": "repetitions" # Replace with the measurement unit (e.g., repetitions, minutes, etc.)
}
response = self.client.post("/api/exercises/", headers=headers, json=exercise_data)

if response.status_code == 201:
print("\nSuccessfully added exercise:", exercise_data["name"])
else:
print("\nFailed to add exercise. Response:", response.status_code, response.json())
else:
print("\nSkipping exercise creation. No valid token available.")


0 comments on commit 8e31fbc

Please sign in to comment.