4 from pylab import figure, plot, show, subplots_adjust, title
6 def get_closet_index(time, timelist, 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
15 if difference > best_difference:
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.
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]]
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])
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])
50 if len(l) == minlength:
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])
66 subplot.axhline(y = hline)
67 subplot.set_ylim(ymax=ymax)
76 time_begin = float('inf')
90 total_over_max_weights = []
97 for i in xrange(1, len(sys.argv)):
98 files.append(open(sys.argv[i], "r"))
115 total_over_max_weight = []
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:
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:
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:
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])
177 print "Warning: Caught ValueError on line: " + line
182 local_rates.append(local_rate)
183 ideal_weights.append(ideal_weight)
184 local_weights.append(local_weight)
185 total_weights.append(total_weight)
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:
202 for t in xrange(len(times)):
203 for i in xrange(len(times[t])):
204 times[t][i] -= time_begin
210 subplots_adjust(left = 0.12, right = 0.94, bottom = 0.05, top = 0.94)
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()
241 sub.axvline(x = (on - time_begin), color = 'green')
243 for off in off_times:
246 sub.axvline(x = (off - time_begin), color = 'red')
248 sub.set_xlim(xmin = xlimits[0], xmax = xlimits[1])