-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocustfile.py
More file actions
119 lines (100 loc) · 3.64 KB
/
locustfile.py
File metadata and controls
119 lines (100 loc) · 3.64 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import uuid
from locust import HttpUser, between, task
API_PREFIX = "/api"
API_VERSION = "v1"
API_ROOT = f"{API_PREFIX}/{API_VERSION}"
class TelemetryApiUser(HttpUser):
wait_time = between(0.5, 3.0)
def on_start(self):
self.user_id = self._create_user()
self.device_id = self._create_device(self.user_id)
self._add_measurement(self.device_id)
def _create_user(self) -> int:
unique_suffix = uuid.uuid4().hex[:8]
payload = {
"username": f"user-{unique_suffix}",
"email": f"{unique_suffix}@example.com",
}
with self.client.post(
f"{API_ROOT}/users/",
json=payload,
name=f"POST {API_ROOT}/users/",
catch_response=True
) as response:
if response.status_code != 201:
response.failure(f"Unexpected status: {response.status_code}, body={response.text}")
return -1
data = response.json()
return int(data["id"])
def _create_device(self, user_id: int) -> int:
payload = {
"name": f"device-{uuid.uuid4().hex[:6]}",
"user_id": user_id,
}
with self.client.post(
f"{API_ROOT}/devices/",
json=payload,
name=f"POST {API_ROOT}/devices/",
catch_response=True
) as response:
if response.status_code != 201:
response.failure(f"Unexpected status: {response.status_code}, body={response.text}")
return -1
data = response.json()
return int(data["id"])
def _add_measurement(self, device_id: int):
payload = {
"x": 1.0,
"y": 2.0,
"z": 3.0,
}
with self.client.post(
f"{API_ROOT}/analytics/{device_id}/data",
json=payload,
name=f"POST {API_ROOT}/analytics/{device_id}/data",
catch_response=True,
) as response:
if response.status_code != 201:
response.failure(f"Unexpected status: {response.status_code}, body={response.text}")
@task(5)
def add_measurement(self):
self._add_measurement(self.device_id)
@task(3)
def get_device_analytics(self):
params = {
"device_id": self.device_id,
"limit": 25,
"offset": 0,
}
with self.client.get(
f"{API_ROOT}/analytics/",
params=params,
name=f"GET {API_ROOT}/analytics/?device_id",
catch_response=True
) as response:
if response.status_code != 200:
response.failure(f"Unexpected status: {response.status_code}, body={response.text}")
return
payload = response.json()
if "data" not in payload:
response.failure("Response without data field")
@task(2)
def get_users(self):
with self.client.get(
f"{API_ROOT}/users/",
params={"limit": 10, "offset": 0},
name=f"GET {API_ROOT}/users/",
catch_response=True,
) as response:
if response.status_code != 200:
response.failure(f"Unexpected status: {response.status_code}, body={response.text}")
@task(2)
def get_devices(self):
with self.client.get(
f"{API_ROOT}/devices/",
params={"limit": 10, "offset": 0},
name=f"GET {API_ROOT}/devices/",
catch_response=True,
) as response:
if response.status_code != 200:
response.failure(f"Unexpected status: {response.status_code}, body={response.text}")