forked from Marie-Donnie/juice
-
Notifications
You must be signed in to change notification settings - Fork 1
/
analysis.py
executable file
·282 lines (230 loc) · 9.51 KB
/
analysis.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
"""Analyse your results from experiments
Usage:
analysis [-h | --help] [-v | --version] <command> [<args>...]
Options:
-h --help Show this help
-v --version Show version number
Commands:
deploy Claim resources from g5k and configure them
Run 'analysis COMMAND --help' for more information on a command
"""
import logging
import re
import os
import tarfile
import json
import pandas as pd
import matplotlib.pyplot as plt
from docopt import docopt
from utils.doc import doc, doc_lookup
pd.options.display.float_format = '{:20,.6f}'.format
plt.style.use('seaborn-white')
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Ubuntu'
plt.rcParams['font.monospace'] = 'Ubuntu Mono'
plt.rcParams['font.size'] = 10
plt.rcParams['axes.labelsize'] = 10
plt.rcParams['axes.labelweight'] = 'bold'
plt.rcParams['axes.titlesize'] = 10
plt.rcParams['xtick.labelsize'] = 6
plt.rcParams['ytick.labelsize'] = 8
plt.rcParams['legend.fontsize'] = 10
plt.rcParams['figure.titlesize'] = 12
DF_0 = []
DF_50 = []
DF_150 = []
@doc()
def full_run(directory, latency, remove_delete, **kwargs):
"""
usage: analysis full_run (--directory=directory) [--latency=latency]
[--remove_delete]
Full run from a directory
--directory=directory Path to the result directory
--latency=latency The latency for the wanted graph [default: 0]
--remove_delete Remove the delete actions from the graphs
"""
directories = check_directory(directory)
for result_dir in directories:
unzip_rally(result_dir)
add_results(result_dir, latency, remove_delete)
_plot(latency)
def check_directory(folder, **kwargs):
results = []
if os.path.exists(folder):
if os.path.isdir(folder):
directories = os.listdir(folder)
for directory in directories:
if _check_result_dir(directory, folder):
results.append(folder + directory)
return results
else:
logging.error("%s is not a directory." % directory)
else:
logging.error("%s does not exists." % directory)
def unzip_rally(directory, **kwargs):
tar = _find_tar(directory)
ar = tarfile.open(tar)
results_dir = os.path.join(directory, "results/")
if not os.path.exists(results_dir):
os.makedirs(results_dir)
ar.extractall(path=results_dir,
members=_safe_json(ar, directory))
ar.close()
return
def _collect_actions(actions, scenario, db, nodes, remove_delete):
result = []
if db == 'mariadb':
db = 'M'
elif db == 'cockroachdb':
db = 'C'
for a in actions:
a.update({'scenario': scenario})
a.update({'db': db})
a.update({'nodes': nodes})
if remove_delete:
# delete_user makes everything ugly
if a.get('name') != 'keystone_v3.delete_user':
result.append(a)
for suba in _collect_actions(a['children'], scenario,
db, nodes, remove_delete):
if a.get('name') != 'keystone_v3.delete_user':
result.append(suba)
else:
result.append(a)
for suba in _collect_actions(a['children'], scenario,
db, nodes, remove_delete):
result.append(suba)
return result
def add_results(directory, latency, remove_delete, **kwargs):
results = os.path.join(directory, "results")
dir_name = os.path.basename(directory)
for fil in os.listdir(results):
file_path = os.path.join(results, fil)
with open(file_path, "r") as fileopen:
json_file = json.load(fileopen)
subtask = json_file['tasks'][0]['subtasks'][0]
scenario = subtask['title'].split('.')[1]
key_scenarios = ['authenticate_user_and_validate_token',
'create_add_and_list_user_roles',
'create_and_list_tenants',
'get_entities',
'create_user_update_password',
'create_user_set_enabled_and_delete',
'create_and_list_users']
if (scenario not in key_scenarios):
continue
db = dir_name.split('-', 1)[0]
nodes = dir_name.split('-')[1]
laten = dir_name.split('-')[2].split('ms')[0]
if laten == latency:
data = subtask['workloads'][0]['data']
actions = []
for v in data:
for a in v['atomic_actions']:
actions.append(a)
all_actions = _collect_actions(actions,
scenario,
db,
nodes,
remove_delete)
df = pd.DataFrame(all_actions, columns=['name',
'started_at',
'finished_at',
'scenario',
'db',
'nodes'])
df['duration'] = df['finished_at'].subtract(df['started_at'])
less_is_better = df.drop(columns=['finished_at',
'started_at'])
table = pd.pivot_table(less_is_better,
values='duration',
index=['scenario',
'db',
'nodes',
'name'])
if laten == '0':
DF_0.append(table)
elif laten == '50':
DF_50.append(table)
elif laten == '150':
DF_150.append(table)
return
def _plot(latency):
if latency == '0':
df = DF_0
elif latency == '50':
df = DF_50
elif latency == '150':
df = DF_150
# Concatenate all data frames
df = pd.concat(df)
# Extract nodes as columns
df = df.unstack()
# Re-order by nodes (else the order is '25' '3' '45' lexicographically)
df = df.reindex(pd.Index(['3', '25', '45'], name='nodes'), level=2)
df = df.rename(columns=lambda x: x.replace('keystone_v3.', ''))
# Plot with stacked bars
df.plot.bar(stacked=True, legend=True)
plt.tight_layout()
plt.show()
def _check_result_dir(directory, folder):
pattern = re.compile(("(maria|cockroach)(db)-\d{1,3}"
"-\d{1,3}"))
if pattern.match(directory):
if "backup" in os.listdir(folder + directory):
return True
else:
logging.warning("No backup folder in %s" % directory)
return False
else:
logging.warning("%s does not match the correct pattern" % directory)
return False
def _find_tar(directory):
folder_pattern = re.compile(".*(maria|cockroach).*")
tar_pattern = re.compile("(rally-).*(grid5000.fr.tar.gz)")
tar_in_dir = []
for folder in os.listdir(directory):
if folder == "backup":
backup_folder = os.path.join(directory, folder)
for f in os.listdir(backup_folder):
path_to_f = os.path.join(backup_folder, f)
if folder_pattern.match(f) and os.path.isdir(path_to_f):
for tar in os.listdir(path_to_f):
path_to_tar = os.path.join(path_to_f, tar)
if (tar_pattern.match(tar) and
tarfile.is_tarfile(path_to_tar)):
tar_in_dir.append(path_to_tar)
return(path_to_tar)
# resolved = lambda x: os.path.realpath(os.path.abspath(x))
def resolved(path):
return os.path.realpath(os.path.abspath(path))
# see https://stackoverflow.com/questions/10060069/safely-extract-zip-or-tar-using-python
def badpath(path, base):
# joinpath will ignore base if path is absolute
return not resolved(os.path.join(base, path)).startswith(base)
def badlink(info, base):
# Links are interpreted relative to the directory containing the link
tip = resolved(os.path.join(base, os.path.dirname(info.name)))
return badpath(info.linkname, base=tip)
def _safe_json(members, directory):
base = resolved(directory)
for finfo in members:
if badpath(finfo.name, base):
logging.error("%s is blocked (illegal path)" % finfo.name)
elif finfo.issym() and badlink(finfo, base):
logging.error("%s is blocked: Hard link to: %s" % (finfo.name,
finfo.linkname))
elif finfo.islnk() and badlink(finfo, base):
logging.error("%s is blocked: Symlink to: %s" % (finfo.name,
finfo.linkname))
else:
if finfo.name.endswith('.json'):
finfo.name = re.sub('rally_home/', '', finfo.name)
yield finfo
if __name__ == '__main__':
args = docopt(__doc__,
version='analysis version 1.0.0',
options_first=True)
argv = [args['<command>']] + args['<args>']
doc_lookup(args['<command>'], argv)