-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadtester.py
68 lines (56 loc) · 2.07 KB
/
loadtester.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
import glob
import os
from pprint import pprint
import yaml
from locust import HttpLocust, TaskSet, between
def load_rules():
dir_rules = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'rules')
if os.path.exists(dir_rules):
pattern = f'{dir_rules}/*.yaml'
for each in glob.glob(pattern):
if not os.path.exists(each):
continue
try:
result = None
with open(each) as tmpf:
result = yaml.safe_load(tmpf)
yield result
except Exception as exc:
print(exc)
else:
print('No rules directory found.')
def generate_task(payload):
endpoint = payload.get('endpoint')
http_method = payload.get('method').lower()
headers= payload.get('headers')
data = payload.get('data')
return lambda l: getattr(l.client, http_method)(endpoint,
headers=headers,
data=data)
def create_taskset():
"""Create Taskset class.
NOTE: currently, single host is only supported.
Returns:
HttpLocust -- Locust class
"""
rules = load_rules()
# taskset_classes = []
for e, each_rule in enumerate(rules):
# pprint(each_rule)
class_name = each_rule.get('name') or f'UserClass{e}'
host = '{proto}://{host}:{port}'.format(**each_rule)
# print(host)
tasks = each_rule.get('tasks')
fn_tasks = [generate_task(each_task) for each_task in tasks]
new_class = type(class_name, (TaskSet, ), dict(tasks=fn_tasks))
# help(new_class)
# taskset_classes.append(new_class)
locust_runner = type('LoadTester',
(HttpLocust,),
dict(
host=host,
task_set=new_class,
wait_time=between(2, 5)))
# help(locust_runner)
return locust_runner
load_tester = create_taskset()