-
Notifications
You must be signed in to change notification settings - Fork 0
/
castalia.py
executable file
·403 lines (309 loc) · 14.7 KB
/
castalia.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python2.7
import sys
import numpy as np
import argparse
import itertools
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import os
from subprocess import call
def run_simulation(args):
return Runner(*args).run()
class Castalia(object):
def __init__(self, configuration, input_file="omnetpp.ini"):
self.castalia_installation = "/home/frey/Desktop/Projekte/work/sics/SemInt/Castalia-master/Castalia/"
self.binary = self.castalia_installation + '/bin/Castalia'
self.cwd = os.getcwd()
self.configuration = configuration
self.input_file = input_file
if os.path.exists(self.binary) == False:
raise Exception("The castalia binary could not be found at path" + self.binary)
self.log_file_path = self.cwd + '/' + self.configuration + '-Log.txt'
def run(self):
environment = dict(os.environ)
if self.binary.endswith("Castalia"):
with open(self.log_file_path, 'w') as logfile:
call([self.binary, "-i", self.input_file, "-c", self.configuration], env=environment, cwd=self.cwd, stdout=logfile, stderr=logfile)
else:
with open(self.log_file_path, 'w') as logfile:
call([self.binary, "-i", self.input_file, "-s", self.configuration, "-o", "2"], env=environment, cwd=self.cwd, stdout=logfile, stderr=logfile)
class CastaliaResultParser:
def __init__(self):
self.results = {}
self.nodes = []
self.files = []
def _parse_line(self, line):
# let's split the line by '|'
data = line.split('|')
# well, it's actual data and not a header
if len(data) > 1:
# let's filter empty elements and newlines from the list
data = [element for element in data if len(element) > 0 and element != '\n']
if data[0] != ' ':
# let's check if we have multiple parameters
parameters = data.pop(0).strip().split(',')
# get the keys
parameters = [parameter.split('=')[1] for parameter in parameters]
# our future dict key
key = ','.join(parameters)
if key not in self.results.keys():
self.results[key] = []
for value in data:
self.results[key].append(float(value))
def read_breakdown_packets(self, filename):
self.results = {}
self.nodes = []
with open(filename, "r") as filehandle:
for line in filehandle:
self._parse_line(line)
def _get_bins(self, bins):
result = [element[1:-1].split(',') for element in bins]
result = itertools.chain(*result)
result = [int(element) for element in result if element != "inf"]
result = sorted(list(set(result)), key=int)
return result
def plot_histogram(self):
data = self.prepare_data()
bins = self._get_bins(self.nodes)
print("Generating histograms for application latency: ")
for payload in data.keys():
for node in data[payload].keys():
for rate in sorted(data[payload][node],key=int):
sys.stdout.write('.')
title = "Application Latency (#Nodes: " + node + " Payload: " + payload + " Bytes, Rate: " + rate + ")"
current_filename = "application_latency-" + payload + "_" + node + "_" + rate + ".png"
hist_data = [float(element) for element in data[payload][node][rate]]
figure, axis = plt.subplots(1)
plt.hist(hist_data, bins, color='#003366')
plt.title(title)
self.files.append(current_filename)
figure.savefig(current_filename)
plt.close()
def prepare_data(self):
data = {}
for key in self.results.keys():
payload, rate, node = key.split(',')
if payload not in data.keys():
data[payload] = {}
if node not in data[payload].keys():
data[payload][node] = {}
if rate not in data[payload][node].keys():
data[payload][node][rate] = []
data[payload][node][rate] = self.results[key]
return data
def plot_breakdown_packets(self):
data = self.prepare_data()
print("Generating bar plots for packet breakdown: ")
# let's generate plots
for payload in data.keys():
for node in data[payload].keys():
rates = []
failed = []
success = []
for rate in sorted(data[payload][node],key=int):
rates.append(float(rate))
failed.append(data[payload][node][rate][0])
success.append(data[payload][node][rate][3])
sys.stdout.write('.')
title = "Packet Breakdown (#Nodes: " + node + " Payload: " + payload + " Bytes)"
self.generate_bar_plot("packets_breakdown_" + payload + "_" + node + ".png", title, rates, [failed, success], "rate", "packets", ['failed','success'])
print("\n")
def read_multiple_columns(self, filename):
self.results = {}
self.nodes = []
with open(filename, "r") as filehandle:
for line in filehandle:
preamble = line.split('|')
if len(preamble) > 1:
if preamble[0] == ' ':
preamble.pop(0)
self.nodes = [element.split('=')[-1].strip() for element in preamble]
self._parse_line(line)
def plot(self, title, filename, xlabel, ylabel):
data = self.prepare_data()
nr_of_nodes = -1
print("Generating line plots for " + title + ": ")
for payload in data.keys():
for node in data[payload].keys():
rates = []
average = []
minimum = []
maximum = []
for rate in sorted(data[payload][node],key=int):
rates.append(float(rate))
average.append(data[payload][node][rate][0])
minimum.append(data[payload][node][rate][1])
maximum.append(data[payload][node][rate][2])
sys.stdout.write('.')
title = "Average Latency (#Nodes: " + node + " Payload: " + payload + " Bytes)"
current_filename = filename + "-" + payload + "-" + node + ".png"
figure, axis = plt.subplots(1)
axis.plot(rates, average, lw=2, label=title, color='#003366')
#axis.fill_between(rates, [i + j for i, j in zip(average, maximum)], [i - j for i, j in zip(average, minimum)], facecolor='#003366', alpha=0.5)
axis.fill_between(rates, maximum, minimum, facecolor='#003366', alpha=0.5)
axis.set_title(title)
axis.set_xlabel(xlabel)
axis.set_ylabel(ylabel)
axis.grid()
figure.savefig(current_filename)
self.files.append(current_filename)
plt.close()
print("\n")
def plot_ext(self, title, filename, xlabel, ylabel):
data = {}
nr_of_nodes = -1
for key in self.results.keys():
payload, rate = key.split(',')
if payload not in data.keys():
data[payload] = {}
if rate not in data[payload].keys():
data[payload][rate] = {}
data[payload][rate] = self.results[key]
# store the number of nodes
nr_of_nodes = len(self.results[key])
print("Generating line plots for " + title + ": ")
# let's generate plots
for payload in data.keys():
# we create a new plot for each #nodes
for current_node in range(nr_of_nodes):
rates = []
reception_rate = []
sys.stdout.write('.')
for rate in sorted(data[payload], key=int):
rates.append(rate)
reception_rate.append(float(data[payload][rate][current_node]))
# create the title of the figure
current_title = title + "(#Nodes = " + self.nodes[current_node] + ", Payload = " + payload + ")"
current_filename = filename + "-" + payload + "-" + self.nodes[current_node] + ".png"
# finally let's plot the reception rate
self.generate_line_plot(current_filename, current_title, [rates], [reception_rate], xlabel, ylabel, [])
def generate_line_plot(self, filename, title, xlist, ylist, xlabel, ylabel, labels):
figure = plt.figure()
axis = figure.add_subplot(111)
markers = ['s','^','v','2','*','3','d']
for index, value in enumerate(xlist):
if len(xlist) > 1:
plt.plot(value, ylist[index], drawstyle="line", lw=2.5, label=labels[index])
else:
plt.plot(value, ylist[index], drawstyle="line", lw=2.5, color="#003366")
plt.title(title)
axis.set_xlabel(xlabel)
axis.set_ylabel(ylabel)
plt.grid()
plt.savefig(filename)
self.files.append(filename)
def generate_line_plot_ext(self, filename, title, xlist, mu, sigma, xlabel, ylabel):
figure, axis = plt.subplots(1)
axis.plot(xlist, mu, lw=2, label=title, color='#003366')
axis.fill_between(xlist, [i + j for i, j in zip(mu, sigma)], [i - j for i, j in zip(mu, sigma)], facecolor='#003366', alpha=0.5)
axis.set_title(title)
axis.set_xlabel(xlabel)
axis.set_ylabel(ylabel)
axis.grid()
figure.savefig(filename)
self.files.append(filename)
def write_result_file(self, pattern, input_file, output_file):
castalia = Castalia(pattern, input_file)
castalia.binary += "Results"
castalia.log_file_path = os.getcwd() + "/" + output_file
castalia.run()
def generate_bar_plot(self, filename, title, xdata, ydata, xlabel, ylabel, labels):
figure, axis = plt.subplots()
index = np.array(xdata)
width = 1.0
bar_widths = -1
if bar_widths > 0:
axis.bar(index, ydata, bar_widths)
else:
print len(index)
print len(ydata[1])
print len(ydata[0])
axis.bar(index, ydata[1], width, color="#990000")
axis.bar(index+width, ydata[0], width, color="#003366")
axis.set_ylabel(ylabel)
axis.set_xlabel(xlabel)
#axis.set_xticks(index + width)
axis.set_title(title)
#axis.legend((ydata[0][0], ydata[1][0]), (labels[0], labels[1]) )
axis.grid(axis="y")
plt.savefig(filename)
self.files.append(filename)
plt.close()
def prepare_line_plot(self, filename, title, xlabel, ylabel, data):
xlist = []
std = []
mean = []
for key in sorted(data):
xlist.append(key)
mean.append(float(np.mean(data[key])))
std.append(float(np.std(data[key])))
self.generate_line_plot(filename, title, xlist, mean, std, xlabel, ylabel)
class ReportGenerator:
def __init__(self):
self.scenarios = {}
def _get_header(self):
header = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
header += '<html xmlns="http://www.w3.org/1999/xhtml">'
header += ' <head>'
header += ' <title>Results</title>'
header += ' <link rel="stylesheet" href="style.css" type="text/css">'
header += ' </head>'
header += ' <body>'
return header
def _get_footer(self):
footer = ' </body>'
footer += '</html>'
return footer
def write(self, filename):
with open(filename, "w") as output:
output.write(self._get_header())
for scenario in self.scenarios:
output.write(self._get_scenario(scenario))
output.write(self._get_footer())
def _get_scenario(self, scenario):
result = '<h3>' + scenario + '</h3>'
counter = 0
for entry in self.scenarios[scenario]:
result += '<a href="' + entry +'"><img src="' + entry + '"width=250 height=250></a>'
if counter < 5:
counter += 1
else:
counter = 0
result += '</br></br>'
return result
def main():
parser = argparse.ArgumentParser(description='cthulhu - a script for running and evaluating castalia simulations')
parser.add_argument('-r', '--run', dest='run', default=False, const=True, action='store_const', help='run simulations')
parser.add_argument('-c', dest='configuration', type=str, default="", action='store', help='a castalia configuarion to run')
parser.add_argument('-i', dest='omnetpp_ini', type=str, default="omnetpp.ini", action='store', help='omnetpp.ini file which should be considered')
parser.add_argument('-p', '--plot', dest='plot', default=False, const=True, action='store_const', help='plot simulation results')
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
arguments = parser.parse_args()
if arguments.run == True:
castalia = Castalia(arguments.configuration, arguments.omnetpp_ini)
castalia.run()
if arguments.plot == True:
results = CastaliaResultParser()
results.write_result_file("application level", arguments.omnetpp_ini, "application.txt")
results.read_multiple_columns("application.txt")
results.plot_histogram()
results.write_result_file("average latency", arguments.omnetpp_ini, "latency.txt")
results.read_multiple_columns("latency.txt")
results.plot("Average Latency", "average_latency", "rate", "latency")
results.write_result_file("Packets received", arguments.omnetpp_ini, "received.txt")
results.read_multiple_columns("received.txt")
results.plot_ext("Received Packets", "received_packets", "rate", "packets")
results.write_result_file("Packets reception rate", arguments.omnetpp_ini, "reception.txt")
results.read_multiple_columns("reception.txt")
results.plot_ext("Packet Reception Rate", "packet_reception_rate", "rate", "packet reception rate")
results.write_result_file("Packet breakdown", arguments.omnetpp_ini, "breakdown.txt")
results.read_breakdown_packets("breakdown.txt")
results.plot_breakdown_packets()
report = ReportGenerator()
report.scenarios['Test'] = results.files
report.write('test.html')
if __name__ == "__main__":
main()