Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
256 lines (197 loc) · 7.97 KB
/
Copy pathmain.py
File metadata and controls
256 lines (197 loc) · 7.97 KB
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
import time
import random
import asyncio
from typing import Callable, Union
from rest_client import SyncRestClient, AsyncRestClient, ProtectedRestClient
from requests.models import Response
from aiohttp.client_reqrep import ClientResponse
class BaseSender:
""" TODO fix description, add type definitions
gets bundle of data to send
returns bundle of responses
Can:
send data with strong RPS
send data with random RPS(can change mode by users decision)
send data in synchronous mode
send data in asynchronous mode
send data in mixed mode(async, but sync or by condition)
attach request handler
attach response handler
"""
def __init__(self, host, path, headers, rps, send_mode):
self.host = host
self.path = path
self.headers = headers
self._rps = 1 / rps
self._need_abort = False
self._need_suspend = False
self.request_handler: Callable = lambda: True
self.response_handler: Callable = lambda: True
self.sync_rest_client = SyncRestClient(host, headers)
self.async_rest_client = AsyncRestClient(host, headers)
self.protected_rest_client = ProtectedRestClient(host, headers)
self.response = Union[ClientResponse, Response]
self.results_bundle = []
self.bundle_to_resend = []
self.conditions_for_resend = []
if send_mode not in ["SYNC", "ASYNC", "PROTECTED"]:
raise AttributeError(f'Attribute send_mode should be on of {["SYNC", "ASYNC", "PROTECTED"]}.')
self.send_mode = send_mode
self.current_rps_setter = \
self._make_async_rps if any([self.send_mode == x for x in ["ASYNC", "PROTECTED"]]) else self._make_sync_rps
def _send_sync_with_method(self, method):
senders = {
"POST": self.sync_rest_client.post,
"PUT": self.sync_rest_client.put,
"DELETE": self.sync_rest_client.delete
}
return senders[method]
def _send_async_with_method(self, method):
senders = {
"POST": self.async_rest_client.post,
"PUT": self.async_rest_client.put,
"DELETE": self.async_rest_client.delete
}
return senders[method]
def _send_protected_with_method(self, method, request_num=0):
self.protected_rest_client.request_num = request_num
senders = {
"POST": self.protected_rest_client.post,
"PUT": self.protected_rest_client.put,
"DELETE": self.protected_rest_client.delete
}
return senders[method]
def _make_sync_rps(self, rand=False):
time.sleep(self._rps if rand is False else random.randrange(0, 5)) # TODO magis number
async def _make_async_rps(self, rand=False):
await asyncio.sleep(self._rps if rand is False else random.randrange(0, 5)) # TODO magis number
def _control_rps(self, mode_setter: Callable):
mode_setter()
async def _control_async_rps(self, mode_setter: Callable):
await mode_setter()
@staticmethod
def _func_to_execute(slf, fun, *args, **kwargs):
def wrapper():
return fun(slf, *args, **kwargs)
return wrapper
def get_response(self):
return self.response
def set_rps(self, rps):
self._rps = rps
def handle_each_request_by(self, slf, fun: Callable, *args, **kwargs):
self.request_handler = self._func_to_execute(slf, fun, *args, **kwargs)
def handle_each_response_by(self, slf, fun: Callable, *args, **kwargs):
self.response_handler = self._func_to_execute(slf, fun, *args, **kwargs)
def start(self):
pass
def stop(self):
pass
def suspend(self):
pass
def go_on(self):
pass
class JsonSender(BaseSender):
"""
POST, PUT, DELETE types are supported
"""
def __init__(self, method, json_bundle, host, path, headers, rps, send_mode):
super().__init__(host, path, headers, rps, send_mode)
if method not in ["POST", "PUT", "DELETE"]:
raise AttributeError(f'Attribute method should be on of {["POST", "PUT", "DELETE"]}.')
self.method = method
self.json_bundle = json_bundle
async def _start_in_protected_mode(self, json_bundle):
self.current_rps_setter = self._make_async_rps
request_tasks = [
self._send_protected_with_method(self.method, request_num=request_num)(self.path, json=json)
for request_num, json in enumerate(json_bundle)
]
for f in request_tasks:
await self._control_async_rps(self.current_rps_setter)
if self._need_abort:
break
elif self._need_suspend:
while self._need_suspend:
await asyncio.sleep(0.5) # TODO magis number
self.request_handler()
res = await f
self.response = res.response
self.response_handler()
if any([condition for condition in self.conditions_for_resend]):
if self.send_mode != "PROTECTED":
print("Cannot apply conditions_for_resend because method name is not 'PROTECTED'")
else:
self.bundle_to_resend.append(res.request_num)
else:
self.results_bundle.append(self.response)
if self.bundle_to_resend:
print(
f"\nNeed to resend several requests (in the amount of {len(self.bundle_to_resend)}), "
f"resend them for additional check:",
end="\n",
)
self._start_in_sync_mode(self.bundle_to_resend)
async def _start_in_async_mode(self, json_bundle):
self.current_rps_setter = self._make_async_rps
request_tasks = [
self._send_async_with_method(self.method)(self.path, json=json)
for json in json_bundle
]
for f in request_tasks:
await self._control_async_rps(self.current_rps_setter)
if self._need_abort:
break
elif self._need_suspend:
while self._need_suspend:
await asyncio.sleep(0.5) # TODO magis number
self.request_handler()
self.response = await f
self.response_handler()
self.results_bundle.append(self.response)
def _start_in_sync_mode(self, json_bundle):
self.current_rps_setter = self._make_sync_rps
for json in json_bundle:
self._control_rps(self.current_rps_setter)
if self._need_abort:
break
elif self._need_suspend:
while self._need_suspend:
time.sleep(0.5) # TODO magis number
self.request_handler()
self.response = self._send_sync_with_method(self.method)(self.path, json=json)
self.response_handler()
self.results_bundle.append(self.response)
def start(self):
if self.send_mode == "SYNC":
self._start_in_sync_mode(self.json_bundle)
elif self.send_mode == "ASYNC":
asyncio.run(self._start_in_async_mode(self.json_bundle))
else:
asyncio.run(self._start_in_protected_mode(self.json_bundle))
def stop(self):
self._need_abort = True
def suspend(self):
self._need_suspend = True
def go_on(self):
self._need_suspend = False
if __name__ == "__main__":
jb = [{"data": "Hello Beeceptor"} for x in range(0, 5)]
rs = JsonSender(
method="POST",
json_bundle=jb,
host="http://localhost:8000",
path='/',
headers={},
rps=100,
send_mode="PROTECTED"
)
def p(js: JsonSender, secs):
js.conditions_for_resend.append(not js.get_response().ok)
if not js.get_response().ok:
print("GOT NOT OK answer...")
js.suspend()
time.sleep(secs)
js.go_on()
rs.handle_each_response_by(rs, p, 0)
rs.start()
print(rs.results_bundle)