-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot-flowRate2.py
More file actions
115 lines (93 loc) · 2.98 KB
/
Copy pathplot-flowRate2.py
File metadata and controls
115 lines (93 loc) · 2.98 KB
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
import optparse
import sys
import matplotlib
from pylab import *
# Class that parses a file of rates and plots a smoothed graph
class Plotter:
def __init__(self,file):
""" Initialize plotter with a file name. """
self.file = file
self.data = {}
self.min_time = None
self.max_time = None
self.max = None
return
def parse(self):
""" Parse the data file """
first = None
f = open(self.file)
for line in f.readlines():
if line.startswith("#"):
continue
try:
t, event, ip, port, size, lable, packetType = line.split()
if (event != "socket_recieve" or ip != "125.225.53.2"):
continue
except:
continue
t = float(t)
size = int(size)
if not (port in self.data):
self.data[port] = []
self.data[port].append((t,size))
if not self.min_time or t < self.min_time:
self.min_time = t
if not self.max_time or t > self.max_time:
self.max_time = t
return
def plot(self):
for key in self.data.keys():
self.plotFlow(self.data[key], "Flow " + str(key))
return
def plotFlow(self, data, aLabel):
""" Create a line graph of the rate over time. """
x = []
y = []
i = 0
while i < self.max_time:
bytes = 0
# loop through array of data and find relevant data
for (t,size) in data:
if (t >= i - 1) and (t <= i):
bytes += size
# compute interval
left = i - 1
if i - 1 < 0:
left = 0
right = i
# add data point
if (right - left) != 0:
rate = (bytes*8.0/1000000)/(right-left)
x.append(i)
y.append(rate)
if (not self.max) or (rate > self.max):
self.max = int(rate) + .5
i += 0.1
plot(x,y,label=aLabel)
return
def save(self):
xlabel('Time (seconds)')
ylabel('Rate (Mbps)')
ylim([0,1.1])
legend()
savefig('reports2/flowsRate.png')
return
def parse_options():
# parse options
parser = optparse.OptionParser(usage = "%prog [options]",
version = "%prog 0.1")
parser.add_option("-f","--file",type="string",dest="file",
default=None,
help="file")
(options,args) = parser.parse_args()
return (options,args)
if __name__ == '__main__':
(options,args) = parse_options()
if options.file == None:
print "plot.py -f file"
sys.exit()
clf()
p = Plotter(options.file)
p.parse()
p.plot()
p.save()