forked from delftdata/wdm-project-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locustfile.py
347 lines (251 loc) · 9.52 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import random
from typing import List
from locust import HttpLocust, TaskSet, TaskSequence, seq_task, between
# replace the example urls and ports with the appropriate ones
ORDER_URL = "http://127.0.0.1:5000"
PAYMENT_URL = "http://127.0.0.1:5001"
STOCK_URL = "http://127.0.0.1:5002"
USER_URL = "http://127.0.0.1:5003"
def create_item(self):
price = random.randint(1, 10)
response = self.client.post(f"{STOCK_URL}/stock/item/create/{price}", name="/stock/item/create/[price]")
self.item_ids.append(response.json()['item_id'])
def add_stock(self, item_idx: int):
stock_to_add = random.randint(100, 1000)
self.client.post(f"{STOCK_URL}/stock/add/{self.item_ids[item_idx]}/{stock_to_add}",
name="/stock/add/[item_id]/[number]")
def create_user(self):
response = self.client.post(f"{USER_URL}/users/create", name="/users/create/")
self.user_id = response.json()['user_id']
def add_balance_to_user(self):
balance_to_add = random.randint(10000, 100000)
self.client.post(f"{USER_URL}/users/credit/add/{self.user_id}/{balance_to_add}",
name="/users/credit/add/[user_id]/[amount]")
def create_order(self):
response = self.client.post(f"{ORDER_URL}/orders/create/{self.user_id}", name="/orders/create/[user_id]")
self.order_id = response.json()['order_id']
def add_item_to_order(self, item_idx: int):
response = self.client.post(f"{ORDER_URL}/orders/addItem/{self.order_id}/{self.item_ids[item_idx]}",
name="/orders/addItem/[order_id]/[item_id]", catch_response=True)
if 400 <= response.status_code < 500:
response.failure(response.text)
else:
response.success()
def remove_item_from_order(self, item_idx: int):
response = self.client.delete(f"{ORDER_URL}/orders/removeItem/{self.order_id}/{self.item_ids[item_idx]}",
name="/orders/removeItem/[order_id]/[item_id]", catch_response=True)
if 400 <= response.status_code < 500:
response.failure(response.text)
else:
response.success()
def checkout_order(self):
response = self.client.post(f"{ORDER_URL}/orders/checkout/{self.order_id}", name="/orders/checkout/[order_id]",
catch_response=True)
if 400 <= response.status_code < 500:
response.failure(response.text)
else:
response.success()
def checkout_order_that_is_supposed_to_fail(self, reason: int):
response = self.client.post(f"{ORDER_URL}/orders/checkout/{self.order_id}", name="/orders/checkout/[order_id]",
catch_response=True)
if 400 <= response.status_code < 500:
response.success()
else:
if reason == 0:
response.failure("This was supposed to fail: Not enough stock")
else:
response.failure("This was supposed to fail: Not enough credit")
def make_items_stock_zero(self, item_idx: int):
stock_to_subtract = self.client.get(f"{STOCK_URL}/stock/find/{self.item_ids[item_idx]}",
name="/stock/find/[item_id]").json()['stock']
self.client.post(f"{STOCK_URL}/stock/subtract/{self.item_ids[item_idx]}/{stock_to_subtract}",
name="/stock/add/[item_id]/[number]")
class LoadTest1(TaskSequence):
"""
Scenario where a stock admin creates an item and adds stock to it
"""
item_ids: List[str]
def on_start(self):
""" on_start is called when a Locust start before any task is scheduled """
self.item_ids = list()
def on_stop(self):
""" on_stop is called when the TaskSet is stopping """
self.item_ids = list()
@seq_task(1)
def admin_creates_item(self): create_item(self)
@seq_task(2)
def admin_adds_stock_to_item(self): add_stock(self, 0)
class LoadTest2(TaskSequence):
"""
Scenario where a user checks out an order with one item inside that an admin has added stock to before
"""
item_ids: List[str]
user_id: str
order_id: str
def on_start(self):
self.item_ids = list()
self.user_id = ""
self.order_id = ""
def on_stop(self):
self.item_ids = list()
self.user_id = ""
self.order_id = ""
@seq_task(1)
def admin_creates_item(self): create_item(self)
@seq_task(2)
def admin_adds_stock_to_item(self): add_stock(self, 0)
@seq_task(3)
def user_creates_account(self): create_user(self)
@seq_task(4)
def user_adds_balance(self): add_balance_to_user(self)
@seq_task(5)
def user_creates_order(self): create_order(self)
@seq_task(6)
def user_adds_item_to_order(self): add_item_to_order(self, 0)
@seq_task(7)
def user_checks_out_order(self): checkout_order(self)
class LoadTest3(TaskSequence):
"""
Scenario where a user checks out an order with two items inside that an admin has added stock to before
"""
item_ids: List[str]
user_id: str
order_id: str
def on_start(self):
self.item_ids = list()
self.user_id = ""
self.order_id = ""
def on_stop(self):
self.item_ids = list()
self.user_id = ""
self.order_id = ""
@seq_task(1)
def admin_creates_item1(self): create_item(self)
@seq_task(2)
def admin_adds_stock_to_item1(self): add_stock(self, 0)
@seq_task(3)
def admin_creates_item2(self): create_item(self)
@seq_task(4)
def admin_adds_stock_to_item2(self): add_stock(self, 1)
@seq_task(5)
def user_creates_account(self): create_user(self)
@seq_task(6)
def user_adds_balance(self): add_balance_to_user(self)
@seq_task(7)
def user_creates_order(self): create_order(self)
@seq_task(8)
def user_adds_item1_to_order(self): add_item_to_order(self, 0)
@seq_task(9)
def user_adds_item2_to_order(self): add_item_to_order(self, 1)
@seq_task(10)
def user_checks_out_order(self): checkout_order(self)
class LoadTest4(TaskSequence):
"""
Scenario where a user adds an item to an order, regrets it and removes it and then adds it back and checks out
"""
item_ids: List[str]
user_id: str
order_id: str
def on_start(self):
self.item_ids = list()
self.user_id = ""
self.order_id = ""
def on_stop(self):
self.item_ids = list()
self.user_id = ""
self.order_id = ""
@seq_task(1)
def admin_creates_item(self): create_item(self)
@seq_task(2)
def admin_adds_stock_to_item(self): add_stock(self, 0)
@seq_task(3)
def user_creates_account(self): create_user(self)
@seq_task(4)
def user_adds_balance(self): add_balance_to_user(self)
@seq_task(5)
def user_creates_order(self): create_order(self)
@seq_task(6)
def user_adds_item_to_order(self): add_item_to_order(self, 0)
@seq_task(7)
def user_removes_item_from_order(self): remove_item_from_order(self, 0)
@seq_task(8)
def user_adds_item_to_order_again(self): add_item_to_order(self, 0)
@seq_task(9)
def user_checks_out_order(self): checkout_order(self)
class LoadTest5(TaskSequence):
"""
Scenario that is supposed to fail because the second item does not have enough stock
"""
item_ids: List[str]
user_id: str
order_id: str
def on_start(self):
self.item_ids = list()
self.user_id = ""
self.order_id = ""
def on_stop(self):
self.item_ids = list()
self.user_id = ""
self.order_id = ""
@seq_task(1)
def admin_creates_item1(self): create_item(self)
@seq_task(2)
def admin_adds_stock_to_item1(self): add_stock(self, 0)
@seq_task(3)
def admin_creates_item2(self): create_item(self)
@seq_task(4)
def admin_adds_stock_to_item2(self): add_stock(self, 1)
@seq_task(5)
def user_creates_account(self): create_user(self)
@seq_task(6)
def user_adds_balance(self): add_balance_to_user(self)
@seq_task(7)
def user_creates_order(self): create_order(self)
@seq_task(8)
def user_adds_item1_to_order(self): add_item_to_order(self, 0)
@seq_task(9)
def user_adds_item2_to_order(self): add_item_to_order(self, 1)
@seq_task(10)
def stock_admin_makes_item2s_stock_zero(self): make_items_stock_zero(self, 1)
@seq_task(11)
def user_checks_out_order(self): checkout_order_that_is_supposed_to_fail(self, 0)
class LoadTest6(TaskSequence):
"""
Scenario that is supposed to fail because the user does not have enough credit
"""
item_ids: List[str]
user_id: str
order_id: str
def on_start(self):
self.item_ids = list()
self.user_id = ""
self.order_id = ""
def on_stop(self):
self.item_ids = list()
self.user_id = ""
self.order_id = ""
@seq_task(1)
def admin_creates_item(self): create_item(self)
@seq_task(2)
def admin_adds_stock_to_item(self): add_stock(self, 0)
@seq_task(3)
def user_creates_account(self): create_user(self)
@seq_task(4)
def user_creates_order(self): create_order(self)
@seq_task(5)
def user_adds_item_to_order(self): add_item_to_order(self, 0)
@seq_task(6)
def user_checks_out_order(self): checkout_order_that_is_supposed_to_fail(self, 1)
class LoadTests(TaskSet):
# [TaskSequence]: [weight of the TaskSequence]
tasks = {
LoadTest1: 5,
LoadTest2: 30,
LoadTest3: 25,
LoadTest4: 20,
LoadTest5: 10,
LoadTest6: 10
}
class MicroservicesUser(HttpLocust):
task_set = LoadTests
wait_time = between(1, 15) # how much time a user waits (seconds) to run another TaskSequence