-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocustfile.py
76 lines (61 loc) · 2.21 KB
/
locustfile.py
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
import numpy as np
import base64
from locust import HttpUser, task, between
URL = "/api"
class User(HttpUser):
categories = ['bird', 'horse', 'truck', 'airplane', 'cat', 'automobile', 'dog', 'ship', 'deer', 'frog']
wait_time = between(0.1, 1)
@staticmethod
def _render_image(resp):
category, base_str = resp["result"]
bimg = base64.decodebytes(base_str.encode('utf-8'))
img = np.frombuffer(bimg, dtype=np.uint8)
img.resize((32, 32, 3))
return category
@task(5)
def image_load_random(self):
payload = {
"method": "download",
"jsonrpc": "2.0",
"id": 0,
}
with self.client.post(URL, json=payload, catch_response=True) as resp:
try:
self._render_image(resp.json())
except KeyError:
resp.failure("failed to get image result!")
except Exception:
resp.failure("failed to convert to image!")
@task(5)
def image_load_selected(self):
c = np.random.choice(self.categories)
payload = {
"method": "download",
"params": {"category_id": c},
"jsonrpc": "2.0",
"id": 0,
}
with self.client.post(URL, json=payload, catch_response=True) as resp:
category = None
try:
category = self._render_image(resp.json())
except KeyError:
resp.failure("failed to get image result!")
except Exception as e:
print(repr(e))
resp.failure(f"failed to convert to image! {repr(e)}")
if category != c:
resp.failure(f"invalid category! got {category} expected {c}")
@task
def list_categories(self):
payload = {
"method": "listCategories",
"jsonrpc": "2.0",
"id": 0,
}
with self.client.post(URL, json=payload, catch_response=True) as resp:
try:
if set(resp.json()["result"]) != set(self.categories):
resp.failure("invalid category list!")
except KeyError:
resp.failure("failed to get categories result!")