-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
265 lines (229 loc) · 8.87 KB
/
tasks.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import json
import random
import machines
import networking
def parse_results(results):
try:
parsed = results
if results is not None:
if type(results) is list:
parsed = []
for e in results:
parsed.append(parse_results(e))
elif type(results) is dict:
parsed = {}
for k,v in results.items():
parsed[k] = parse_results(v)
else:
try:
parsed = results.to_dict()
except:
parsed = results
return parsed
except Exception as e:
print("Error while parsing results: {}".format(e))
return {}
def parse_report(r):
r = parse_results(r)
try:
return {
"success": 100 if r["status"] is True else 0,
"time": r["time"],
"iterations": r["iterations"],
"errors": r["errors"],
"#errors": {
"per_type": len(r["errors"]),
"total": sum([int(r["errors"][e]) for e in r["errors"]])
},
"raw": r
}
except Exception as e:
print("Error while parsing report: {}".format(e))
return {}
def aggregate_reports(ls):
r = {}
try:
errors = {}
for l in ls:
if "errors" in l:
for e in l["errors"]:
newE = str(e).split(" ")
newE = " ".join(newE[:min(len(newE), 6)])
print("Err",e,newE)
if newE in errors:
errors[newE].append(l["errors"][e])
else:
errors[newE] = [l["errors"][e]]
try:
for e in errors:
errors[e] = {"total": sum(errors[e]), "avg": sum(errors[e]) / len(ls), "affected": len(errors[e]) / len(ls) * 100}
except Exception as e:
print("Error while parsing errors: {}".format(e))
errors = {}
r["errors"] = errors
success_ls = [l for l in ls if l["success"]==100]
try:
r["success"] = (len(success_ls) / len(ls))*100
except Exception as e:
print("Error while parsing success: {}".format(e))
try:
r["time"] = sum([l["time"] for l in success_ls]) / len(success_ls)
except Exception as e:
print("Error while parsing time: {}".format(e))
try:
r["iterations"] = sum([l["iterations"] for l in success_ls]) / len(success_ls)
except Exception as e:
print("Error while parsing iterations: {}".format(e))
r["#errors"] = {}
try:
r["#errors"]["total"] = sum([l["#errors"]["total"] for l in ls]) / len(ls)
except Exception as e:
print("Error while parsing #errors total: {}".format(e))
try:
r["#errors"]["per_type"] = sum([l["#errors"]["per_type"] for l in ls]) / len(ls)
except Exception as e:
print("Error while parsing #errors per_type: {}".format(e))
except Exception as e:
print("Error while aggregating reports: {}".format(e))
return r
def remake(machine, args, *, verbose=True, very_verbose=True):
results = {}
metadata = machine.metadata
if random.random() < args:
delete = machines.remove_all_machines({"name": machine.name}, [metadata["cloud"]], verbose=very_verbose)[0]
add = machines.add_machine(machine.name, metadata["type"], metadata["cloud"], metadata, verbose=very_verbose).to_dict()
try:
results["delete"] = parse_report(delete)
except Exception as e:
print("Error while parsing delete report: {}".format(e))
results["delete"] = {}
try:
results["add"] = {}
if "create" in add["data"] and add["data"]["create"] != {}:
results["add"]["create"] = parse_report(add["data"]["create"])
if "setup" in add["data"] and add["data"]["setup"] != {}:
results["add"]["setup"] = parse_report(add["data"]["setup"])
results["add"]["first_access"] = parse_report(add) #first access only if there is need to setup
except Exception as e:
print("Error while parsing add report: {}".format(e))
results["add"] = {}
return results
def aggregate_remake(ls, *, verbose=True, very_verbose=True):
add = {
"create":[],
"setup":[],
"first_access":[]
}
delete = []
for l in ls:
if l != {}:
if "delete" in l:
delete.append(l["delete"])
if "add" in l:
if "create" in l["add"]:
add["create"].append(l["add"]["create"])
if "setup" in l["add"]: #comment
add["setup"].append(l["add"]["setup"]) #tab
if "first_access" in l["add"]:
add["first_access"].append(l["add"]["first_access"])
return {
"add": {
"create": aggregate_reports(add["create"]),
"setup": aggregate_reports(add["setup"]),
"first_access": aggregate_reports(add["first_access"])
},
"delete": aggregate_reports(delete)
}
def probe_network(machine, args, *, verbose=True, very_verbose=True):
return networking.probe_newtork(machines.get_ip(machine))
def aggregate_network(ls, *, verbose=True, very_verbose=True):
lat_sum = 0
lat_count = 0
lat_failed = 0
upload_sum = 0
upload_count = 0
upload_failed = 0
download_sum = 0
download_count = 0
download_failed = 0
for l in ls:
if "latency" in l and l["latency"] != None and l["latency"] >= 0:
lat_sum += l["latency"]
lat_count += 1
else:
lat_failed += 1
if "bandwidth" in l:
if "upload" in l["bandwidth"] and l["bandwidth"]["upload"] != None and l["bandwidth"]["upload"] >= 0:
upload_sum += l["bandwidth"]["upload"]
upload_count += 1
else:
upload_failed += 1
if "download" in l["bandwidth"] and l["bandwidth"]["download"] != None and l["bandwidth"]["download"] >= 0:
download_sum += l["bandwidth"]["download"]
download_count += 1
download_sum += l["bandwidth"]["download"]
download_count += 1
else:
download_failed += 1
else:
upload_failed += 1
download_failed += 1
latency_avg = 1.0 * lat_sum / lat_count if lat_count > 0 else -1.0
latency_success = 100.0 - (lat_failed / len(ls) * 100)
bandwidth_upload_avg = 1.0 * upload_sum / upload_count if upload_count > 0 else -1.0
bandwidth_upload_success = 100.0 - (upload_failed / len(ls) * 100)
bandwidth_download_avg = 1.0 * download_sum / download_count if download_count > 0 else -1.0
bandwidth_download_success = 100.0 - (download_failed / len(ls) * 100)
return {
"latency": {
"avg": latency_avg,
"success": latency_success,
},
"bandwidth": {
"upload": {
"avg": bandwidth_upload_avg,
"success": bandwidth_upload_success
},
"download": {
"avg": bandwidth_download_avg,
"success": bandwidth_download_success
}
}
}
def exec_script(machine, args, *, verbose=True, very_verbose=True):
return parse_report(machines.exec_script(machine.name, args, verbose=very_verbose).to_dict())
def aggregate_script(ls, *, verbose=True, very_verbose=True):
return aggregate_reports(ls)
def fio(machine, args, *, verbose=True, very_verbose=True):
r = machines.exec_script(machine.name, "fio", {"<SIZE>": args}, verbose=very_verbose).to_dict()
try:
d = "\n".join((r["data"].split("\n"))[2:-2])
return json.loads(d)
except Exception as e:
print("Error while parsing fio report: {}".format(e))
return {}
def average_dicts(ls):
avg = {}
head = ls[0]
for k in head.keys():
if type(head[k]) is dict:
avg[k] = average_dicts([l[k] for l in ls])
elif type(head[k]) is list:
for i in range(len(head[k])):
avg[k] = average_dicts([l[k][i] for l in ls])
elif type(head[k]) is int or type(head[k]) is float:
avg[k] = sum([l[k] for l in ls]) / len(ls)
else:
avg[k] = head[k]
return avg
def aggregate_fio(ls, *, verbose=True, very_verbose=True):
new_ls = [l for l in ls if l != {} and l is not None]
res = {}
try:
if len(new_ls) != 0:
res = average_dicts(new_ls)
res["success"] = float(len(new_ls) / len(ls)) * 100
except Exception as e:
print("Error while aggregating fio report: {}".format(e))
res = {}
return res