1 #!/usr/bin/env python
3 import sys
4 from pylab import figure, plot, show, subplots_adjust, title
6 def get_closet_index(time, timelist, current_index):
7     #print current_index
8     best_difference = float('inf')
9     best_index = current_index
10     for i in xrange(best_index, len(timelist)):
11         difference = abs(time - timelist[i])
12         if difference < best_difference:
13             best_difference = difference
14             best_index = i
15         if difference > best_difference:
16             return best_index
18     return best_index
20 def sum_best_time(current_indicies, time, timelists, datalists, type):
21     #For the given time, search each of the time lists and get the best index.
22     #By best, we mean with time closet to the specified time.
23     #Use that index in the corresponding datalist and add the value to the sum.
25     sum = type(0)
27     for i in xrange(len(timelists)):
28         current_indicies[i] = get_closet_index(time, timelists[i], current_indicies[i])
29         sum = sum + datalists[i][current_indicies[i]]
31     return sum
33 def get_sum_with_times(timelists, datalists, type):
34     current_indicies = [0 for l in datalists]
35     target_length = min([len(l) for l in datalists])
36     result_timelist = []
37     result_datalist = []
38     for t in xrange(len(timelists)):
39         if len(timelists[t]) == target_length: #
40             result_timelist = timelists[t]
42     for i in xrange(len(result_timelist)):
43         result_datalist.append(sum_best_time(current_indicies, result_timelist[i], timelists, datalists, type))
45     return (result_timelist, result_datalist)
47 def get_smallest_list(lists):
48     minlength = min([len(l) for l in lists])
49     for l in lists:
50         if len(l) == minlength:
51             return l
53 def add_plot(timelists, datalists, index, count, title, figure, type=float, add_sum=False, ymax=None, hline=None):
54     subplot = figure.add_subplot(count, 1, index)
55     subplot.set_title(title)
57     for i in xrange(len(datalists)):
58         subplot.plot(timelists[i], datalists[i])
60     if add_sum and len(datalists) > 1:
61         #subplot.plot(get_smallest_list(timelists), get_sum(datalists, type))
62         sum_tuple = get_sum_with_times(timelists, datalists, type)
63         subplot.plot(sum_tuple[0], sum_tuple[1])
65     if hline != None:
66         subplot.axhline(y = hline)
67     subplot.set_ylim(ymax=ymax)
69     return subplot
71 # File Objects
72 files = []
74 # Data points
75 times = []
76 time_begin = float('inf')
77 local_rates = []
78 ideal_weights = []
79 local_weights = []
80 total_weights = []
81 flows = []
82 flows_5k = []
83 flows_10k = []
84 flows_20k = []
85 flows_50k = []
86 flows_avg = []
87 max_flow_rates = []
88 max_flow_hashs = []
89 local_limits = []
90 total_over_max_weights = []
92 # On/off points
93 on_times = []
94 off_times = []
96 # Open all the files.
97 for i in xrange(1, len(sys.argv)):
98     files.append(open(sys.argv[i], "r"))
100 for file in files:
101     time = []
102     local_rate = []
103     ideal_weight = []
104     local_weight = []
105     total_weight = []
106     flow = []
107     flow_5k = []
108     flow_10k = []
109     flow_20k = []
110     flow_50k = []
111     flow_avg = []
112     max_flow_rate = []
113     max_flow_hash = []
114     local_limit = []
115     total_over_max_weight = []
117     last_time = 0.0
119     for line in file:
120         if line == "--Switching enforcement on.--\n":
121             on_times.append(last_time)
122         if line == "--Switching enforcement off.--\n":
123             off_times.append(last_time)
124         splitline = line.split(" ")
125         if len(splitline) == 12:
126             # It's a data line.
127             time.append(float(splitline[0]))
128             local_rate.append(int(splitline[1]))
129             ideal_weight.append(float(splitline[2]))
130             local_weight.append(float(splitline[3]))
131             total_weight.append(float(splitline[4]))
132             flow.append(int(splitline[5]))
133             flow_5k.append(int(splitline[6]))
134             flow_10k.append(int(splitline[7]))
135             flow_20k.append(int(splitline[8]))
136             flow_50k.append(int(splitline[9]))
137             flow_avg.append(int(splitline[10]))
138             local_limit.append(int(splitline[11]))
139             last_time = float(splitline[0])
140         if len(splitline) == 14 or len(splitline) == 15:
141             # It's a data line.
142             time.append(float(splitline[0]))
143             local_rate.append(int(splitline[1]))
144             ideal_weight.append(float(splitline[2]))
145             local_weight.append(float(splitline[3]))
146             total_weight.append(float(splitline[4]))
147             flow.append(int(splitline[5]))
148             flow_5k.append(int(splitline[6]))
149             flow_10k.append(int(splitline[7]))
150             flow_20k.append(int(splitline[8]))
151             flow_50k.append(int(splitline[9]))
152             flow_avg.append(int(splitline[10]))
153             max_flow_rate.append(int(splitline[11]))
154             max_flow_hash.append(int(splitline[12]))
155             local_limit.append(int(splitline[13]))
156             last_time = float(splitline[0])
157         if len(splitline) == 16:
158             try:
159                 # It's a data line.
160                 time.append(float(splitline[0]))
161                 local_rate.append(int(splitline[1]))
162                 ideal_weight.append(float(splitline[2]))
163                 local_weight.append(float(splitline[3]))
164                 total_weight.append(float(splitline[4]))
165                 flow.append(int(splitline[5]))
166                 flow_5k.append(int(splitline[6]))
167                 flow_10k.append(int(splitline[7]))
168                 flow_20k.append(int(splitline[8]))
169                 flow_50k.append(int(splitline[9]))
170                 flow_avg.append(int(splitline[10]))
171                 max_flow_rate.append(int(splitline[11]))
172                 max_flow_hash.append(int(splitline[12]))
173                 local_limit.append(int(splitline[13]))
174                 total_over_max_weight.append(float(splitline[15]))
175                 last_time = float(splitline[0])
176             except ValueError:
177                 print "Warning: Caught ValueError on line: " + line
179     file.close()
181     times.append(time)
182     local_rates.append(local_rate)
183     ideal_weights.append(ideal_weight)
184     local_weights.append(local_weight)
185     total_weights.append(total_weight)
186     flows.append(flow)
187     flows_5k.append(flow_5k)
188     flows_10k.append(flow_10k)
189     flows_20k.append(flow_20k)
190     flows_50k.append(flow_50k)
191     flows_avg.append(flow_avg)
192     max_flow_rates.append(max_flow_rate)
193     max_flow_hashs.append(max_flow_hash)
194     local_limits.append(local_limit)
195     total_over_max_weights.append(total_over_max_weight)
197 for t in xrange(len(times)):
198     mintime = min(times[t])
199     if mintime < time_begin:
200         time_begin = mintime
202 for t in xrange(len(times)):
203     for i in xrange(len(times[t])):
204         times[t][i] -= time_begin
206 print time_begin
208 fig = figure()
209 subplots = []
210 subplots_adjust(left = 0.12, right = 0.94, bottom = 0.05, top = 0.94)
212 graph_count = 5
214 subplots.append(add_plot(times, local_rates, 1, graph_count, "Local Rate", fig, int, True))
216 subplots.append(add_plot(times, local_limits, 2, graph_count, "Local Limit", fig, int, True))
218 subplots.append(add_plot(times, local_weights, 3, graph_count, "Weight", fig, float, False))
220 #subplots.append(add_plot(times, ideal_weights, 4, graph_count, "Ideal Weight", fig, float, False))
222 #subplots.append(add_plot(times, total_over_max_weights, 5, graph_count, "Ideal Weight", fig, float, False))
224 subplots.append(add_plot(times, flows, 4, graph_count, "# of flows", fig, int, False))
226 #add_plot(times, flows_5k, 6, graph_count, "# of flows > 5KB/s", fig, int, False)
227 #add_plot(times, flows_20k, 7, graph_count, "# of flows > 20KB/s", fig, int, False)
228 #add_plot(times, flows_50k, 6, graph_count, "# of flows > 50KB/s", fig, int, False)
230 subplots.append(add_plot(times, flows_avg, 5, graph_count, "Average flow rate", fig, int, False))
232 #subplots.append(add_plot(times, max_flow_rates, 6, graph_count, "Max flow rate", fig, int, False, ymax=160000))
233 #subplots.append(add_plot(times, max_flow_hashs, 7, graph_count, "Max flow hash", fig, int, False))
235 xlimits = subplots[0].get_xlim()
237 for sub in subplots:
238     for on in on_times:
239         if on < time_begin:
240             on = time_begin
241         sub.axvline(x = (on - time_begin), color = 'green')
243     for off in off_times:
244         if off < time_begin:
245             off = time_begin
246         sub.axvline(x = (off - time_begin), color = 'red')
248     sub.set_xlim(xmin = xlimits[0], xmax = xlimits[1])
250 show()