File size: 1,082 Bytes
fe2866c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from __future__ import annotations

from locust import HttpUser, between, task


class EcommerceUser(HttpUser):
    wait_time = between(0.5, 1.5)

    @task(3)
    def search_products(self) -> None:
        payload = {"query": "red running shoes", "top_k": "10"}
        headers = {"Content-Type": "application/x-www-form-urlencoded"}
        with self.client.post("/search", data=payload, headers=headers, catch_response=True) as response:
            if response.status_code != 200:
                response.failure(f"Unexpected status: {response.status_code} - {response.text}")
            elif len(response.json().get("results", [])) == 0:
                response.failure("Empty search results")

    @task(1)
    def list_products(self) -> None:
        with self.client.get("/products?limit=20&offset=0", catch_response=True) as response:
            if response.status_code != 200:
                response.failure(f"Unexpected status: {response.status_code}")


# Usage:
# locust -f scripts/performance/locustfile.py --host=http://localhost:8000 --users 50 --spawn-rate 5