-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsimulate.py
More file actions
96 lines (82 loc) · 3.33 KB
/
simulate.py
File metadata and controls
96 lines (82 loc) · 3.33 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import random
from uuid import uuid4
from locust import HttpUser, between, task
class APIUser(HttpUser):
"""Simulates user behavior interacting with the API endpoints."""
# Wait between 1 to 5 seconds between tasks
wait_time = between(1, 5)
def on_start(self):
"""Initialize user session data."""
# Store some test product IDs for reuse
self.test_products = [
"15ca8c18-43d4-4da3-ad14-2dc127365b04", # Normal case
"05ca8c18-43d4-4da3-ad14-2dc127365b04", # Not found case
"55ca8c18-43d4-4da3-ad14-2dc127365b04", # Timeout case
]
@task(5) # Higher weight for common read operations
def list_products(self):
"""Simulate users browsing product listings."""
page = random.randint(1, 15)
limit = random.choice([10, 20, 50, 100])
self.client.get(f"/api/v1/products?page={page}&limit={limit}")
@task(3)
def get_product_details(self):
"""Simulate users viewing individual product details."""
product_id = random.choice(self.test_products)
self.client.get(f"/api/v1/products/{product_id}")
@task(1) # Lower weight for write operations
def create_product(self):
"""Simulate users creating new products."""
payload = {
"name": f"Test Product {random.randint(1, 1000)}",
"description": "Automated test product",
"price": random.uniform(10.0, 2000.0),
"category": random.choice(["electronics", "books", "clothing"]),
}
self.client.post("/api/v1/products", json=payload)
@task(1)
def update_product(self):
"""Simulate users updating existing products."""
product_id = random.choice(self.test_products)
payload = {
"name": f"Updated Product {random.randint(1, 1000)}",
"description": "Updated test product",
"price": random.uniform(10.0, 2000.0),
"category": random.choice(["electronics", "books", "clothing"]),
}
self.client.put(f"/api/v1/products/{product_id}", json=payload)
@task(2)
def process_product(self):
"""Simulate heavy processing operations on products."""
product_id = random.choice(self.test_products)
self.client.post(f"/api/v1/products/{product_id}/process")
@task(3)
def ai_predict(self):
"""Simulate AI prediction requests."""
texts = [
"Analyze this customer feedback",
"Process this long document",
"Classify this text sample",
"Summarize this article",
]
payload = {
"text": random.choice(texts),
"model": "gpt-3.5-turbo",
"parameters": {"temperature": 0.7},
}
# Randomly decide whether to simulate errors (20% chance)
simulate_error = random.random() < 0.2
self.client.post(
f"/api/v1/ai/predict?simulate_error={simulate_error}",
json=payload,
)
@task(2)
def get_ai_prediction(self):
"""Simulate retrieving AI prediction results."""
prediction_id = str(uuid4())
# Randomly decide whether to simulate errors (10% chance)
simulate_error = random.random() < 0.1
self.client.get(
f"/api/v1/ai/predict/{prediction_id}?simulate_error={simulate_error}",
)
#