Skip to content

Commit

Permalink
locust
Browse files Browse the repository at this point in the history
  • Loading branch information
Julie Lundberg Suter committed Mar 27, 2025
1 parent e1e938e commit 970f416
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions locust.py
Original file line number Diff line number Diff line change
@@ -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.")

0 comments on commit 970f416

Please sign in to comment.