Skip to content

Commit

Permalink
Locust
Browse files Browse the repository at this point in the history
  • Loading branch information
Sondre Malerud committed Apr 3, 2025
1 parent daa5e99 commit 39bf907
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ DJANGO_SUPERUSER_USERNAME=admin
DJANGO_SUPERUSER_EMAIL=admin@mail.com


LOCUST_USER_PASSWORD=locustpass
LOCUST_USER_USERNAME=locustuser
LOCUST_USER_PASSWORD=xalami123
LOCUST_USER_USERNAME=koko
LOCUST_USER_EMAIL=locustuser@mail.com
2 changes: 2 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ urllib3==2.2.2
validators==0.33.0
win32-setctime==1.1.0
djangorestframework-simplejwt==5.3.1
locust==2.33.2
python-dotenv==1.0.0
45 changes: 45 additions & 0 deletions backend/tests/locustfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
from locust import HttpUser, task, between
from datetime import datetime, timezone
from dotenv import load_dotenv
load_dotenv() # load variables from .env file when running locust command locally

class SecFitUser(HttpUser):
host = "http://tdt4242-23.idi.ntnu.no" # Match Django backend
wait_time = between(10, 10.5)
token = None # Store authentication token
exercise_url = None # Store existing exercise URL


workout_num = "1"

def on_start(self):

response = self.client.post("/api/token/", json={"username": os.environ.get("LOCUST_USER_USERNAME") , "password": os.environ.get("LOCUST_USER_PASSWORD")})
if response.status_code == 200:
self.token = response.json().get("access")
if self.token:
self.client.headers.update({"Authorization": f"Bearer {self.token}"})
else:
print("\nFailed to retrieve token from response.")
else:
print("\nFailed to authenticate. Response:", response.json())

@task
def update_workout(self):
"""Updates the first workout"""
new_date = datetime.now(timezone.utc).isoformat()
workout = {
"url": self.host + "/api/workouts/" + self.workout_num + "/",
"name": "My Great Workout",
"date": new_date,
"notes": "Great workout!",
"exercise_instances": []
}

headers = {"Authorization": f"Bearer {self.token}"} if hasattr(self, "token") else {}
workout_response = self.client.put("/api/workouts/" + self.workout_num + "/", json=workout, headers=headers)
if workout_response.status_code == 200:
print("\nUpdate OK")
else:
print("\nError when updating workout")

0 comments on commit 39bf907

Please sign in to comment.