Commit
·
457c02a
1
Parent(s):
97391a1
chore: Add stress test file with Locust
Browse files- stress_test/dog.jpeg +3 -0
- stress_test/locustfile.py +58 -0
stress_test/dog.jpeg
ADDED
|
Git LFS Details
|
stress_test/locustfile.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
|
| 3 |
+
import requests
|
| 4 |
+
from locust import HttpUser, between, task
|
| 5 |
+
|
| 6 |
+
API_BASE_URL = "http://localhost:8000"
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def login(username: str, password: str) -> Optional[str]:
|
| 10 |
+
"""This function calls the login endpoint of the API to authenticate the user and get a token.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
username (str): email of the user
|
| 14 |
+
password (str): password of the user
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
Optional[str]: token if login is successful, None otherwise
|
| 18 |
+
"""
|
| 19 |
+
url = f"{API_BASE_URL}/login"
|
| 20 |
+
headers = {
|
| 21 |
+
"accept": "application/json",
|
| 22 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
| 23 |
+
}
|
| 24 |
+
data = {
|
| 25 |
+
"grant_type": "",
|
| 26 |
+
"username": username,
|
| 27 |
+
"password": password,
|
| 28 |
+
"scope": "",
|
| 29 |
+
"client_id": "",
|
| 30 |
+
"client_secret": "",
|
| 31 |
+
}
|
| 32 |
+
response = requests.post(url, headers=headers, data=data)
|
| 33 |
+
if response.status_code == 200:
|
| 34 |
+
return response.json()["access_token"]
|
| 35 |
+
else:
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class APIUser(HttpUser):
|
| 40 |
+
"""Represents a user in the API."""
|
| 41 |
+
|
| 42 |
+
wait_time = between(1, 5)
|
| 43 |
+
|
| 44 |
+
@task(1)
|
| 45 |
+
def predict(self):
|
| 46 |
+
"""Predicts an image using the model."""
|
| 47 |
+
token = login("admin@example.com", "admin")
|
| 48 |
+
files = [
|
| 49 |
+
("file", ("dog.jpeg", open("stress_test/dog.jpeg", "rb"), "image/jpeg"))
|
| 50 |
+
]
|
| 51 |
+
headers = {"Authorization": f"Bearer {token}"}
|
| 52 |
+
payload = {}
|
| 53 |
+
self.client.post(
|
| 54 |
+
"http://0.0.0.0:8000/model/predict",
|
| 55 |
+
headers=headers,
|
| 56 |
+
data=payload,
|
| 57 |
+
files=files,
|
| 58 |
+
)
|