-
Notifications
You must be signed in to change notification settings - Fork 1
/
assembl_load_testing.py
executable file
·153 lines (134 loc) · 4.59 KB
/
assembl_load_testing.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
#!/usr/bin/env python3
import asyncio
import argparse
import pdb
from molotov import scenario, global_setup, setup_session
import simplejson as json
_SERVER = None
_HARS = []
_USER = None
_PASSWORD = None
def is_write(request):
if request['method'] in ('GET', 'OPTIONS', 'HEAD'):
return False
if request['method'] != 'POST':
return True
if not request['url'].endswith('/graphql'):
return True
data = json.loads(request['postData']['text'])
query = data['query']
return 'mutation' in query
def as_dict(headers, lower=False):
def maybe_lower(h):
return h.lower() if lower else h
return dict(zip([maybe_lower(x['name']) for x in headers],
[x['value'] for x in headers]))
@scenario(weight=100)
async def from_har(session):
global _SERVER
har = session.har
requests = []
responses = []
for entry in har['log']['entries']:
request, response = entry['request'], entry['response']
if not request['url'].startswith(_SERVER):
continue
if response['status'] != 200:
continue
resp_headers = as_dict(response['headers'], True)
if 'cache-control' in resp_headers:
# skip cached entries
continue
postDatum = request.get('postData', (None))
if postDatum:
postDatum = postDatum['text']
if responses and is_write(request):
# make it sequential
# print("waiting before", request)
await asyncio.wait(
responses, return_when=asyncio.FIRST_EXCEPTION)
response = session.request(
method=request['method'],
url=request['url'],
params=as_dict(request['queryString']), # assuming no multi-value
data=postDatum,
headers=as_dict(request['headers']),
verify_ssl=False,
# TODO: cookies
)
requests.append(request)
responses.append(response)
assert responses, "No request found"
done, pending = await asyncio.wait(
responses, return_when=asyncio.FIRST_EXCEPTION)
for task in done:
resp = await task
request = requests[responses.index(task._coro)]
print(resp.status, request['url'])
print()
if resp.status != 200:
print(resp.status, request)
# pdb.set_trace()
assert resp.status == 200
@setup_session()
async def do_login(worker_id, session):
global _USER
global _PASSWORD
global _SERVER
global _HARS
session.har = _HARS[worker_id % len(_HARS)]
resp = await session.post(_SERVER+'/legacy/login', data={
'referrer': 'v2',
'identifier': _USER,
'password': _PASSWORD
})
if resp.status != 200:
print("Could not login:", resp)
# pdb.set_trace()
assert False, 'could not login, check credentials'
def config(user=None, password=None, server=None, har_files=None):
global _USER
global _PASSWORD
global _SERVER
global _HARS
if not (user and password and server and har_files):
from configparser import ConfigParser
parser = ConfigParser()
with open('molotov.ini') as f:
parser.read_file(f)
_USER = user or parser.get('molotov', 'user')
_PASSWORD = password or parser.get('molotov', 'password')
_SERVER = server or parser.get('molotov', 'server')
har_files = har_files or parser.get('molotov', 'har_files')
assert _USER and _PASSWORD and _SERVER and har_files
else:
(_USER, _PASSWORD, _SERVER) = (user, password, server)
for filename in har_files.split():
with open(filename) as f:
har = json.load(f)
_HARS.append(har)
@global_setup()
def molotov_config(*args):
# global _HARS
config()
# weight = 100/len(_HARS)
# for har in _HARS:
# async def test(session):
# await from_har(session, har)
# scenario(weight=weight)(test)
async def main():
import aiohttp
async with aiohttp.ClientSession() as client:
await do_login(0, client)
await from_har(client)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("configuration", help="configuration file")
parser.add_argument("-u", "--username")
parser.add_argument("-p", "--password")
parser.add_argument("-s", "--server")
parser.add_argument('har', nargs='*', help='har files')
args = parser.parse_args()
config(args.username, args.password, args.server, args.har)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())