test(performance): Added resources performance tests

This commit is contained in:
sumit_chaturvedi
2025-06-03 10:21:06 +05:30
parent 5c1a47d108
commit 4156033183
2 changed files with 54 additions and 0 deletions
@@ -0,0 +1,33 @@
from locust import HttpUser, task, events
from utils.helpers import get_api_token, get_auth_headers, get_random_resource_id
GLOBAL = {
"token": None,
"resource_ids": []
}
@events.test_start.add_listener
def on_test_start(environment, **kwargs):
GLOBAL["token"] = get_api_token(environment.host)
class ResourceUser(HttpUser):
def on_start(self):
self.token = GLOBAL["token"]
self.headers = get_auth_headers(self.token)
with self.client.get("/resources", headers=self.headers, name="/resources", catch_response=True) as response:
if response.status_code == 200:
json_data = response.json()
GLOBAL["resource_ids"] = [item["id"] for item in json_data.get("data", [])[:10]]
else:
response.failure("Failed to load /resources")
@task(3)
def list_resources(self):
self.client.get("/resources", headers=self.headers, name="/resources")
@task(2)
def get_single_resource(self):
if GLOBAL["resource_ids"]:
resource_id = get_random_resource_id(GLOBAL["resource_ids"])
self.client.get(f"/resources/{resource_id}", headers=self.headers, name="/resources/:id")
+21
View File
@@ -166,3 +166,24 @@ def get_sort_value(sort_values: list) -> str:
str: A formatted sort query string (e.g., "sort=created_at,-severity").
"""
return f"sort={','.join(sort_values)}"
def get_random_resource_id(resource_list):
"""
Selects and returns a random resource ID from the provided list.
Args:
resource_list (list): A list of resource IDs.
Returns:
Any: A randomly selected resource ID from the list.
Raises:
ValueError: If the provided list is empty.
Example:
resource_id = get_random_resource_id(GLOBAL["resource_ids"])
"""
if not resource_list:
raise ValueError("The resource list is empty.")
return random.choice(resource_list)