-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_packet_loss.py
executable file
·273 lines (221 loc) · 7.61 KB
/
plot_packet_loss.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
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import argparse
from re import search, findall, MULTILINE
from os.path import basename, getsize
COLORS = mcolors.CSS4_COLORS.keys()
# COLORS = [
# 'blue',
# 'cyan',
# 'green',
# 'yellow',
# 'orange',
# 'red',
# 'magenta',
# ]
class MoonGenLog(object):
_filepath = None
_filename = None
_valid = None
_rate = None
_tx_avg = None
_tx_stddev = None
_rx_avg = None
_rx_stddev = None
_packet_loss_avg = None
_packet_loss_stddev = None
def __init__(self, filepath):
self._filepath = filepath
if getsize(self._filepath) <= 0:
print(f'Invalid log file: {self._filepath}, ' +
'file is empty, skipping...')
self._valid = False
return
self._filename = basename(filepath)
self._rate = int(search(r'(\d+?)kpps', self._filename).group(1))
log = ''
with open(self._filepath, 'r') as f:
log = f.read()
rgx_summary_line = r'^.* bytes \(incl. CRC\)$'
rx_line, tx_line = findall(rgx_summary_line, log, flags=MULTILINE)
if not ('TX' in tx_line and 'RX' in rx_line):
print(f'Invalid log file: {self._filepath}, ' +
'TX or RX summary not found, skipping...')
self._valid = False
return
rgx_rate = r'(\d+?) \(StdDev (\d+?)\) Mbit/s'
tx_search = search(rgx_rate, tx_line)
rx_search = search(rgx_rate, rx_line)
self._tx_avg = int(tx_search.group(1))
self._tx_stddev = int(tx_search.group(2))
self._rx_avg = int(rx_search.group(1))
self._rx_stddev = int(rx_search.group(2))
if self._tx_stddev > 0.2 * self._tx_avg:
print(f'Invalid log file: {self._filepath}, ' +
'TX rate has too large standard deviation, skipping...')
self._valid = False
return
self._packet_loss_avg = 100 * max(self._tx_avg - self._rx_avg, 0) \
/ self._tx_avg
self._packet_loss_stddev = 100 * self._rx_stddev / self._tx_avg
self._valid = True
def filepath(self):
return self._filepath
def filename(self):
return self._filename
def valid(self):
return self._valid
def rate(self):
return self._rate
def tx_avg(self):
return self._tx_avg
def tx_stddev(self):
return self._tx_stddev
def rx_avg(self):
return self._rx_avg
def rx_stddev(self):
return self._rx_stddev
def packet_loss_avg(self):
return self._packet_loss_avg
def packet_loss_stddev(self):
return self._packet_loss_stddev
class PacketLossPlot(object):
_moongen_logs = None
_name = None
_color = None
_absolute = None
def __init__(self, moongen_log_filepaths, name, color, absolute):
self._moongen_logs = []
for filepath in moongen_log_filepaths:
moongen_log = MoonGenLog(filepath)
if not moongen_log.valid():
continue
self._moongen_logs.append(moongen_log)
self._name = name
self._color = color
self._absolute = absolute
def plot(self):
data = {}
for moongen_log in self._moongen_logs:
rate = moongen_log.rate()
if rate not in data:
data[rate] = []
data[rate].append(moongen_log)
_x = []
_y = []
_yerr = []
for rate, logs in data.items():
_x.append(rate)
if not self._absolute:
_y.append(np.mean([log.packet_loss_avg() for log in logs]))
_yerr.append(np.mean([log.packet_loss_stddev() for log in logs]))
else:
_y.append(np.mean([log.rx_avg() for log in logs]))
_yerr.append(np.mean([log.rx_stddev() for log in logs]))
order = np.argsort(_x)
x = np.array(_x)[order]
y = np.array(_y)[order]
yerr = np.array(_yerr)[order]
plt.errorbar(
x,
y,
yerr=yerr,
label=self._name,
color=self._color,
ecolor=self._color,
linewidth=1,
linestyle='--',
marker='o',
elinewidth=1,
capsize=5,
)
def setup_parser():
parser = argparse.ArgumentParser(
description='Plot packet loss graph'
)
parser.add_argument('-t',
'--title',
type=str,
help='Title of the plot',
)
parser.add_argument('-W', '--width',
type=float,
default=12,
help='Width of the plot in inches'
)
parser.add_argument('-H', '--height',
type=float,
default=6,
help='Height of the plot in inches'
)
parser.add_argument('-o', '--output',
type=argparse.FileType('w+'),
help='''Path to the output plot
(default: packet_loss.pdf)''',
default='packet_loss.pdf'
)
parser.add_argument('-l', '--logarithmic',
action='store_true',
help='Plot logarithmic packet loss axis',
)
parser.add_argument('-a', '--absolute',
action='store_true',
help='Plot absolute number of packets received back at the load gen instead of packet loss',
)
for color in COLORS:
parser.add_argument(f'--{color}',
type=argparse.FileType('r'),
nargs='+',
help=f'''Paths to MoonGen measurement logs for
the {color} plot''',
)
for color in COLORS:
parser.add_argument(f'--{color}-name',
type=str,
default=color,
help=f'''Name of {color} plot''',
)
return parser
def parse_args(parser):
args = parser.parse_args()
if not any([args.__dict__[color] for color in COLORS]):
parser.error('At least one set of moongen log paths must be ' +
'provided')
return args
def main():
parser = setup_parser()
args = parse_args(parser)
fig = plt.figure(figsize=(args.width, args.height))
ax = fig.add_subplot(1, 1, 1)
ax.set_axisbelow(True)
if args.title:
plt.title(args.title)
if not args.absolute:
plt.ylabel('Packet Loss (%)')
else:
plt.ylabel('Throughput (kpps)')
plt.xlabel('Load (kpps)')
plt.grid()
if not args.logarithmic and not args.absolute:
plt.ylim(-5, 105)
# plt.ylim(-1, 2)
ax.set_yscale('log' if args.logarithmic else 'linear')
for color in COLORS:
if args.__dict__[color]:
plot = PacketLossPlot(
moongen_log_filepaths=[h.name for h in args.__dict__[color]],
name=args.__dict__[f'{color}_name'],
color=color,
absolute=args.absolute,
)
plot.plot()
legend = plt.legend()
legend.get_frame().set_facecolor('white')
legend.get_frame().set_alpha(0.8)
fig.tight_layout()
plt.savefig(args.output.name)
plt.close()
if __name__ == '__main__':
main()