forked from chapkovski/locust-otree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocustfile.py
69 lines (55 loc) · 2.36 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
from locust import TaskSet, task, between, HttpUser
from locust.exception import StopUser
# this is not the best - in case oTree decides to change the bot complete message later.
########### BLOCK: FOR OLD (<2.6) otree version ##############################################################
BOT_COMPLETE_HTML_MESSAGE = b'''
<html>
<head>
<title>Bot completed</title>
</head>
<body>Bot completed</body>
</html>
'''
############ END OF: FOR OLD (<2.6) otree version #############################################################
class OtreeApplication:
def __init__(self, client, host=None):
self.client = client
self.start_url = host
def first_page(self):
with self.client.get(self.start_url, name=self.start_url, catch_response=True) as response:
newlink = response.url
if newlink != self.start_url and response.ok:
response.success()
status = True
while status:
name = ': '.join(newlink.strip('/').split('/')[-3:])
with self.client.post(newlink, name=name, catch_response=True) as response:
oldlink = newlink
newlink = response.url
# trying to catch OutOfRangeNotification for older otree versions
if response.content == BOT_COMPLETE_HTML_MESSAGE and oldlink == newlink:
print('IM DONE')
status = False
response.success()
elif newlink.split('/')[3] == 'OutOfRangeNotification':
with self.client.get(newlink, name='OutOfRangeNotification', catch_response=True) as final_response:
if final_response.ok:
final_response.success()
status = False
elif response.ok:
status = response.ok
response.success()
else:
response.failure('oTree-Locust error')
status = False
class OtreeTaskSet(TaskSet):
def on_start(self):
self.otree_client = OtreeApplication(self.client, host=self.parent.host)
@task(1)
def start_bot(self):
self.otree_client.first_page()
raise StopUser()
class WebsiteUser(HttpUser):
wait_time = between(5, 9)
host = 'http://localhost:8000'
tasks = [OtreeTaskSet]