forked from mathialm/secfit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.") |