-
Notifications
You must be signed in to change notification settings - Fork 20
/
plot_rate.py
164 lines (138 loc) · 4.62 KB
/
plot_rate.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
from helper import *
parser = argparse.ArgumentParser()
parser.add_argument('--files', '-f',
help="Rate timeseries output to one plot",
required=True,
action="store",
nargs='+',
dest="files")
parser.add_argument('--legend', '-l',
help="Legend to use if there are multiple plots. File names used as default.",
action="store",
nargs="+",
default=None,
dest="legend")
parser.add_argument('--out', '-o',
help="Output png file for the plot.",
default=None, # Will show the plot
dest="out")
parser.add_argument('-s', '--summarise',
help="Summarise the time series plot (boxplot). First 10 and last 10 values are ignored.",
default=False,
dest="summarise",
action="store_true")
parser.add_argument('--labels',
help="Labels for x-axis if summarising; defaults to file names",
required=False,
default=[],
nargs="+",
dest="labels")
parser.add_argument('--xlabel',
help="Custom label for x-axis",
required=False,
default=None,
dest="xlabel")
parser.add_argument('--ylabel',
help="Custom label for y-axis",
required=False,
default=None,
dest="ylabel")
parser.add_argument('-i',
help="Interfaces to plot (regex)",
default=".*",
dest="pat_iface")
parser.add_argument('--rx',
help="Plot receive rates on the interfaces.",
default=False,
action="store_true",
dest="rx")
parser.add_argument('--maxy',
help="Max mbps on y-axis..",
default=100,
action="store",
dest="maxy")
parser.add_argument('--miny',
help="Min mbps on y-axis..",
default=0,
action="store",
dest="miny")
parser.add_argument('--normalize',
help="normalise y-axis",
default=False,
action="store_true",
dest="normalise")
args = parser.parse_args()
if args.labels is None:
args.labels = args.files
pat_iface = re.compile(args.pat_iface)
to_plot=[]
"""Output of bwm-ng csv has the following columns:
unix_timestamp;iface_name;bytes_out;bytes_in;bytes_total;packets_out;packets_in;packets_total;errors_out;errors_in
"""
if args.normalise and args.labels == []:
raise "Labels required if summarising/normalising."
sys.exit(-1)
bw = map(lambda e: int(e.replace('M','')), args.labels)
idx = 0
for f in args.files:
data = read_list(f)
#xaxis = map(float, col(0, data))
#start_time = xaxis[0]
#xaxis = map(lambda x: x - start_time, xaxis)
#rate = map(float, col(2, data))
rate = {}
column = 2
if args.rx:
column = 3
for row in data:
try:
ifname = row[1]
except:
break
if ifname not in ['eth0', 'lo']:
if not rate.has_key(ifname):
rate[ifname] = []
try:
rate[ifname].append(float(row[column]) * 8.0 / (1 << 20))
except:
break
if args.summarise:
for k in rate.keys():
if pat_iface.match(k):
print k
vals = filter(lambda e: e < 1500, rate[k][10:-10])
if args.normalise:
vals = map(lambda e: e / bw[idx], vals)
idx += 1
to_plot.append(vals)
else:
for k in sorted(rate.keys()):
if pat_iface.match(k):
print k
plt.plot(rate[k], label=k)
plt.title("TX rates")
if args.rx:
plt.title("RX rates")
if args.ylabel:
plt.ylabel(args.ylabel)
elif args.normalise:
plt.ylabel("Normalized BW")
else:
plt.ylabel("Mbps")
plt.grid()
plt.legend()
plt.ylim((int(args.miny), int(args.maxy)))
if args.summarise:
plt.boxplot(to_plot)
plt.xticks(range(1, 1+len(args.files)), args.labels)
if not args.summarise:
if args.xlabel:
plt.xlabel(args.xlabel)
else:
plt.xlabel("Time")
if args.legend:
plt.legend(args.legend)
if args.out:
plt.savefig(args.out)
else:
plt.show()