def plot_train_qlen(self, input_dir, plt_name, traffic_files, algorithm): algo = algorithm[0] conf = algorithm[1] # folders = glob.glob('%s/%s_*' % (input_dir, conf['pre'])) bb = {} for tf in traffic_files: for e in range(self.epochs): bb['%s_%s' % (algo, e)] = [] input_file = input_dir + \ '/%s_%d/%s/qlen.txt' % (conf['pre'], e, tf) results = self.get_qlen_stats(input_file, conf['sw']) avg_qlen = float(results['avg_qlen']) print("%s %s Epoch %d: Avg Qlen %f" % (algo, tf, e, avg_qlen)) bb['%s_%s' % (algo, e)].append(avg_qlen) p_bar = [] p_legend = [] for i in range(self.epochs): p_bar.append(bb['%s_%d' % (algo, i)][0]) p_legend.append('Epoch %i' % i) print("%s: Total Average Qlen: %f" % (tf, avg(p_bar))) plt.plot(p_bar) x_val = list(range(self.epochs + 1)) if self.epochs > 100: x_step = x_val[0::(self.epochs / 10)] plt.xticks(x_step) plt.xlabel('Epoch') plt.ylabel('Average Queue Length') axes = plt.gca() axes.set_ylim([0, self.max_queue]) plt.savefig("%s/%s_%s" % (self.plt_dir, plt_name, tf)) plt.gcf().clear()
def plot_train_bw(self, input_dir, plt_name, traffic_files, algorithm): algo = algorithm[0] conf = algorithm[1] fbb = self.num_ifaces * self.max_bw # folders = glob.glob('%s/%s_*' % (input_dir, conf['pre'])) bb = {} for tf in traffic_files: for e in range(self.epochs): bb['%s_%s' % (algo, e)] = [] input_file = input_dir + \ '/%s_%d/%s/rate_summary.json' % (conf['pre'], e, tf) results = self.get_bw_dict(input_file) avg_bw = float(results['avg_bw']) print("%s %s Epoch %d: Avg BW %f" % (algo, tf, e, avg_bw)) bb['%s_%s' % (algo, e)].append(avg_bw / fbb / 2) p_bar = [] p_legend = [] for i in range(self.epochs): p_bar.append(bb['%s_%d' % (algo, i)][0]) p_legend.append('Epoch %i' % i) print("%s: Total Average Bandwidth: %f" % (tf, avg(p_bar))) plt.plot(p_bar) x_val = list(range(self.epochs + 1)) if self.epochs > 100: x_step = x_val[0::(self.epochs / 10)] plt.xticks(x_step) plt.xlabel('Epoch') plt.ylabel('Normalized Average Bisection Bandwidth') axes = plt.gca() axes.set_ylim([-0.1, 1.1]) plt.savefig("%s/%s_%s" % (self.plt_dir, plt_name, tf)) plt.gcf().clear()
def evolving_average(self, input_list, n=100): avgreward = [] summation = avg(input_list[:n - 1]) i = n for r in input_list[n:]: summation += float(r) avgreward.append(summation / float(i)) i += 1 return avgreward
def get_bw_stats(self, input_file, pat_iface): pat_iface = re.compile(pat_iface) data = read_list(input_file) rate = {} column = 3 for row in data: try: ifname = row[1] except Exception as e: break if pat_iface.match(ifname): if ifname not in rate: rate[ifname] = [] try: rate[ifname].append(float(row[column]) * 8.0 / (1 << 20)) except Exception as e: break vals = {} avg_bw = [] match = 0 avg_bw_iface = {} filtered_bw = {} for iface, bws in rate.items(): rate[iface] = self.moving_average(bws[10:-10], 100) avg_bw.append(avg(bws[10:-10])) avg_bw_iface[iface] = avg(bws[10:-10]) match += 1 # Update the num of interfaces to reflect true matches # TODO: This is a hack. Remove. self.num_ifaces = match total_bw = list(itertools.chain.from_iterable(rate.values())) vals["avg_bw_iface"] = avg_bw_iface vals["avg_bw"] = fsum(avg_bw) vals["median_bw"] = np.median(total_bw) vals["max_bw"] = max(total_bw) vals["stdev_bw"] = stdev(total_bw) return vals, rate
def get_qlen_stats(self, input_file, pat_iface): data = read_list(input_file) pat_iface = re.compile(pat_iface) qlen = {} for row in data: try: ifname = row[0] except Exception as e: break if pat_iface.match(ifname): if ifname not in qlen: qlen[ifname] = [] try: qlen[ifname].append(int(row[2])) except Exception as e: break vals = {} qlens = [] avg_qlen_iface = {} full_qlen_iface = {} for iface, queues in qlen.items(): qlens.append(queues) avg_qlen_iface[iface] = avg(queues) full_qlen_iface[iface] = queues # qlens = map(float, col(2, data))[10:-10] qlens = list(itertools.chain.from_iterable(qlens)) vals["full_qlen_iface"] = full_qlen_iface vals["avg_qlen_iface"] = avg_qlen_iface vals["avg_qlen"] = avg(qlens) vals["median_qlen"] = np.median(qlens) vals["max_qlen"] = max(qlens) vals["stdev_qlen"] = stdev(qlens) vals["xcdf_qlen"], vals["ycdf_qlen"] = cdf(qlens) return vals
def plot_train_qlen_iface(self, input_dir, plt_name, traffic_files, algorithm): self.check_plt_dir(plt_name) algo = algorithm[0] conf = algorithm[1] # folders = glob.glob('%s/%s_*' % (input_dir, conf['pre'])) bb = {} for tf in traffic_files: for epoch in range(self.epochs): bb['%s_%s' % (algo, epoch)] = {} input_file = input_dir + \ '/%s_%d/%s/qlen.txt' % (conf['pre'], epoch, tf) results = self.get_qlen_stats(input_file, conf['sw']) avg_qlen = results["avg_qlen_iface"] print("Qlen Average: %s %s Epoch %d" % (algo, tf, epoch)) print(avg_qlen) for iface, qlen in avg_qlen.items(): bb['%s_%s' % (algo, epoch)].setdefault(iface, []).append( float(qlen)) for iface, bw in bb['%s_%s' % (algo, 0)].items(): p_bar = [] p_legend = [] for i in range(self.epochs): p_bar.append(bb['%s_%d' % (algo, i)][iface][0]) p_legend.append('Epoch %i' % i) print("Interface %s: Total Average Qlen: %f" % (iface, avg(p_bar))) plt.plot(p_bar, label=iface) x_val = list(range(self.epochs + 1)) if self.epochs > 100: x_step = x_val[0::(self.epochs / 10)] plt.xticks(x_step) plt.xlabel('Epoch') plt.ylabel('Average Queue Length') axes = plt.gca() # axes.set_ylim([0, self.max_queue]) plt.legend(loc='center right') plt.savefig("%s_%s" % (plt_name, tf)) plt.gcf().clear()
def plot_train_bw_iface(self, input_dir, plt_name, traffic_files, algorithm): self.check_plt_dir(plt_name) algo = algorithm[0] conf = algorithm[1] fbb = self.max_bw # folders = glob.glob('%s/%s_*' % (input_dir, conf['pre'])) bb = {} for tf in traffic_files: for epoch in range(self.epochs): bb['%s_%s' % (algo, epoch)] = {} input_file = input_dir + \ '/%s_%d/%s/rate_summary.json' % (conf['pre'], epoch, tf) results = self.get_bw_dict(input_file) avg_bw = results['avg_bw_iface'] print("Average BW %s %s Epoch %d" % (algo, tf, epoch)) print(avg_bw) for iface, bw in avg_bw.iteritems(): bb['%s_%s' % (algo, epoch)].setdefault(iface, []).append( float(bw) / fbb) for iface, bw in bb['%s_%s' % (algo, 0)].iteritems(): p_bar = [] p_legend = [] for i in range(self.epochs): p_bar.append(bb['%s_%d' % (algo, i)][iface][0]) p_legend.append('Epoch %i' % i) print("Interface %s: Total Average Bandwidth: %f" % (iface, avg(p_bar))) plt.plot(p_bar, label=iface) x_val = list(range(self.epochs + 1)) if self.epochs > 100: x_step = x_val[0::(self.epochs / 10)] plt.xticks(x_step) plt.xlabel('Epoch') plt.ylabel('Normalized Average Bisection Bandwidth') axes = plt.gca() axes.set_ylim([-0.1, 1.1]) plt.legend(loc='center right') plt.savefig("%s_%s" % (plt_name, tf)) plt.gcf().clear()
def plot_effective_bw(self, input_dir, plt_name, traffic_files, algorithm): algo = algorithm[0] conf = algorithm[1] # folders = glob.glob('%s/%s_*' % (input_dir, conf['pre'])) bb = {} hosts = ["h1", "h2", "h3", "h4"] for tf in traffic_files: for epoch in range(self.epochs): bb['%s_%s' % (algo, epoch)] = {} for host in hosts: input_file = input_dir + \ '/%s_%d/%s/%s.out' % (conf['pre'], epoch, tf, host) eff_bw_stat = self.get_eff_bw_stats(input_file) avg_eff_bw = np.median(eff_bw_stat) print("Effective Bandwidth Average: %s %s Epoch %d" % (algo, tf, epoch)) print(avg_eff_bw) bb['%s_%s' % (algo, epoch)][host] = avg_eff_bw for host, avg_eff_bw in bb['%s_%s' % (algo, 0)].items(): p_bar = [] p_legend = [] for i in range(self.epochs): p_bar.append(bb['%s_%d' % (algo, i)][host]) p_legend.append('Epoch %i' % i) print("Host %s: Total Average Effective Bandwidth: %f" % (host, avg(p_bar))) plt.plot(p_bar, label=host) x_val = list(range(self.epochs + 1)) if self.epochs > 100: x_step = x_val[0::(self.epochs / 10)] plt.xticks(x_step) plt.xlabel('Epoch') plt.ylabel('Average Effective Bandwidth (bytes/sec)') axes = plt.gca() # axes.set_ylim([0, self.max_queue]) plt.legend(loc='center right') plt.savefig("%s/%s_%s" % (self.plt_dir, plt_name, tf)) plt.gcf().clear()