-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
245 lines (189 loc) · 6.85 KB
/
tests.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
"""
General architecture tests
Brief: This tests suite should be completly separated from the load balancer implementation
and it should make just the port forwarding assumption.
"""
import requests
from endpoint_waker import EndpointWaker
import timeit
HOST = None
PORT = None
MAIN_ENDPOINT = None
REGIONS_SPECS = None
REQ_NUM = None
MAXIMUM_TIME_DEVIATION = None
def init_testing_environment(cfg):
print('General architecture tests environment init')
global HOST
global PORT
global MAIN_ENDPOINT
global REGIONS_SPECS
global REQ_NUM
global MAXIMUM_TIME_DEVIATION
HOST = cfg['host']
PORT = cfg['port']
MAIN_ENDPOINT = cfg['main_endpoint']
REGIONS_SPECS = cfg['regions']
REQ_NUM = cfg['req_num']
MAXIMUM_TIME_DEVIATION = cfg['epsilon']
endpoints = []
for r in REGIONS_SPECS:
region_name = r
region_instances = REGIONS_SPECS[r]
for i in range(region_instances):
endpoint = HOST + ':' + str(PORT) + '/' + MAIN_ENDPOINT + '/' + region_name + '/' + str(i)
endpoints.append(endpoint)
# Wake up all instances to be sure that there are no
# abnormal response times
wake_up_all_instances(endpoints)
def create_endpoints():
global HOST
global PORT
global MAIN_ENDPOINT
global REGIONS_SPECS
global REQ_NUM
global MAXIMUM_TIME_DEVIATION
endpoints = []
for r in REGIONS_SPECS:
region_name = r
region_instances = REGIONS_SPECS[r]
for i in range(region_instances):
endpoint = HOST + ':' + str(PORT) + '/' + MAIN_ENDPOINT + '/' + region_name + '/' + str(i)
endpoints.append(endpoint)
return endpoints
def create_regional_endpoints():
global HOST
global PORT
global MAIN_ENDPOINT
global REGIONS_SPECS
global REQ_NUM
global MAXIMUM_TIME_DEVIATION
endpoints = []
for r in REGIONS_SPECS:
region_name = r
endpoint = HOST + ':' + str(PORT) + '/' + MAIN_ENDPOINT + '/' + region_name
endpoints.append(endpoint)
return endpoints
def wake_up_all_instances(endpoints):
wakers = []
for e in endpoints:
t = EndpointWaker(e)
t.run()
for w in wakers:
w.join()
"""
@return: { endpoint: max response per instance, [array with time values during test for plotting] }
@brif: Testing policy using EMA (exponential moving average) with dynamic parameters k
"""
def requests_per_instance(cfg):
print('Estimating maximum req number per instance')
init_testing_environment(cfg)
requests_per_instance = {}
endpoints = create_endpoints()
for endpoint in endpoints:
early_ema_time = 0.0
time_values = []
for i in range(REQ_NUM):
r = requests.get(endpoint)
data = r.json()
response_time = float(data['response_time'])
time_values.append(response_time)
k = float(2 / (i + 1))
current_ema_time = response_time * k + early_ema_time * (1 - k)
if response_time > current_ema_time + MAXIMUM_TIME_DEVIATION:
requests_per_instance[endpoint] = (i, time_values)
break
else:
time_values.append(response_time)
early_ema_time = current_ema_time
print('Done with endpoint: {}'.format(endpoint))
return requests_per_instance
"""
@return: {region : mean latency value per region }
"""
def latency_per_region(cfg):
print('Calculating latency per region')
init_testing_environment(cfg)
endpoints = create_regional_endpoints()
latency_per_region = {}
for endpoint in endpoints:
latencies = []
for i in range(REQ_NUM):
start_time = timeit.default_timer()
r = requests.get(endpoint)
elapsed = timeit.default_timer() - start_time
data = r.json()
print(data)
response_time = float(data['response_time'])
work_time = float(data['work_time'])
latency = response_time + work_time + elapsed
latencies.append(latency)
mean_latency = sum(latencies) / REQ_NUM
region = endpoint.split('/')[-1]
print(region)
latency_per_region[region] = mean_latency
return latency_per_region
"""
@return: {region : mean work time value per region }
"""
def work_time_per_region(cfg):
print('Calculating work time per region')
init_testing_environment(cfg)
endpoints = create_regional_endpoints()
work_time_per_region = {}
for endpoint in endpoints:
work_times = []
for i in range(REQ_NUM):
r = requests.get(endpoint)
data = r.json()
print(data)
work_time = float(data['work_time'])
work_times.append(work_time)
mean_work_time = sum(work_times) / REQ_NUM
region = endpoint.split('/')[-1]
work_time_per_region[region] = mean_work_time
return work_time_per_region
"""
@return: {instance : response time without load }
@brief: The idea here is to let specific time intervals between requests
to make sure that any instance which is tested is not loaded.
"""
def response_time_without_load(cfg):
print('Calculating response time per worker without load')
init_testing_environment(cfg)
endpoints = create_endpoints()
response_time_without_load = {}
for endpoint in endpoints:
response_times = []
for i in range(REQ_NUM):
# Let sufficient time between calls
if i % 10 == 0:
r = requests.get(endpoint)
data = r.json()
response_time = float(data['response_time'])
response_times.append(response_time)
mean_response_time = sum(response_times) / (REQ_NUM // 10)
response_time_without_load[endpoint] = mean_response_time
return response_time_without_load
"""
@return: {instance : mean forward unit latency estimation }
This function will calculate the average elapsed time for a number
of requests for any region
"""
def forwarding_unit_latency_estiamation(cfg):
print('Calculating forwarding unit latency per region')
init_testing_environment(cfg)
endpoints = create_regional_endpoints()
forwarding_unit_latency_estiamation = {}
for endpoint in endpoints:
fw_latencies = []
for i in range(REQ_NUM):
start_time = timeit.default_timer()
r = requests.get(endpoint)
elapsed = timeit.default_timer() - start_time
fw_latency = elapsed
fw_latencies.append(fw_latency)
mean_fw_latency = sum(fw_latencies) / REQ_NUM
region = endpoint.split('/')[-1]
forwarding_unit_latency_estiamation[region] = mean_fw_latency
return forwarding_unit_latency_estiamation