|
| 1 | +import uuid |
| 2 | + |
| 3 | +from locust import HttpUser, between, task |
| 4 | + |
| 5 | + |
| 6 | +class TelemetryApiUser(HttpUser): |
| 7 | + wait_time = between(0.5, 3.0) |
| 8 | + |
| 9 | + def on_start(self): |
| 10 | + self.user_id = self._create_user() |
| 11 | + self.device_id = self._create_device(self.user_id) |
| 12 | + self._add_measurement(self.device_id) |
| 13 | + |
| 14 | + def _create_user(self) -> int: |
| 15 | + unique_suffix = uuid.uuid4().hex[:8]\ |
| 16 | + payload = { |
| 17 | + "username": f"user-{unique_suffix}", |
| 18 | + "email": f"{unique_suffix}@example.com", |
| 19 | + } |
| 20 | + |
| 21 | + with self.client.post( |
| 22 | + "/users/", |
| 23 | + json=payload, |
| 24 | + name="POST /users/", |
| 25 | + catch_response=True |
| 26 | + ) as response: |
| 27 | + if response.status_code != 201: |
| 28 | + response.failure(f"Unexpected status: {response.status_code}, body={response.text}") |
| 29 | + return -1 |
| 30 | + |
| 31 | + data = response.json() |
| 32 | + return int(data["id"]) |
| 33 | + |
| 34 | + def _create_device(self, user_id: int) -> int: |
| 35 | + payload = { |
| 36 | + "name": f"device-{uuid.uuid4().hex[:6]}", |
| 37 | + "user_id": user_id, |
| 38 | + } |
| 39 | + |
| 40 | + with self.client.post( |
| 41 | + "/devices/", |
| 42 | + json=payload, |
| 43 | + name="POST /devices/", |
| 44 | + catch_response=True |
| 45 | + ) as response: |
| 46 | + if response.status_code != 201: |
| 47 | + response.failure(f"Unexpected status: {response.status_code}, body={response.text}") |
| 48 | + return -1 |
| 49 | + |
| 50 | + data = response.json() |
| 51 | + return int(data["id"]) |
| 52 | + |
| 53 | + def _add_measurement(self, device_id: int): |
| 54 | + payload = { |
| 55 | + "x": 1.0, |
| 56 | + "y": 2.0, |
| 57 | + "z": 3.0, |
| 58 | + } |
| 59 | + |
| 60 | + with self.client.post( |
| 61 | + f"/analytics/{device_id}/data", |
| 62 | + json=payload, |
| 63 | + name="POST /analytics/{device_id}/data", |
| 64 | + catch_response=True, |
| 65 | + ) as response: |
| 66 | + if response.status_code != 201: |
| 67 | + response.failure(f"Unexpected status: {response.status_code}, body={response.text}") |
| 68 | + |
| 69 | + @task(5) |
| 70 | + def add_measurement(self): |
| 71 | + self._add_measurement(self.device_id) |
| 72 | + |
| 73 | + @task(3) |
| 74 | + def get_device_analytics(self): |
| 75 | + params = { |
| 76 | + "device_id": self.device_id, |
| 77 | + "limit": 25, |
| 78 | + "offset": 0, |
| 79 | + } |
| 80 | + |
| 81 | + with self.client.get("/analytics/", params=params, name="GET /analytics/?device_id", catch_response=True) as response: |
| 82 | + if response.status_code != 200: |
| 83 | + response.failure(f"Unexpected status: {response.status_code}, body={response.text}") |
| 84 | + return |
| 85 | + |
| 86 | + payload = response.json() |
| 87 | + if "data" not in payload: |
| 88 | + response.failure("Response without data field") |
| 89 | + |
| 90 | + @task(2) |
| 91 | + def get_users(self): |
| 92 | + with self.client.get( |
| 93 | + "/users/", |
| 94 | + params={"limit": 10, "offset": 0}, |
| 95 | + name="GET /users/", |
| 96 | + catch_response=True, |
| 97 | + ) as response: |
| 98 | + if response.status_code != 200: |
| 99 | + response.failure(f"Unexpected status: {response.status_code}, body={response.text}") |
| 100 | + |
| 101 | + @task(2) |
| 102 | + def get_devices(self): |
| 103 | + with self.client.get( |
| 104 | + "/devices/", |
| 105 | + params={"limit": 10, "offset": 0}, |
| 106 | + name="GET /devices/", |
| 107 | + catch_response=True, |
| 108 | + ) as response: |
| 109 | + if response.status_code != 200: |
| 110 | + response.failure(f"Unexpected status: {response.status_code}, body={response.text}") |
0 commit comments