Files
nwfq-graph/throughput_1.py
zoeyyyzou ae654e1dc8 update
2021-09-23 16:10:08 +08:00

234 lines
10 KiB
Python

import csv
import os
import random
import sys
import numpy as np
import matplotlib.pyplot as plt
def getXY(fileName: str, target: str, faceId: str, quota: str, sampling_interval=1):
x = []
y = []
with open(fileName, "r") as file:
lines = file.readlines()
beta = 0
count = 0
for line in lines:
item = line.split("\t")
if len(item) < 5:
continue
if item[1] == target and item[2] == faceId and item[4] == quota:
if count % sampling_interval == 0: # 这边修改采样间隔
x.append(float(item[0]))
if len(y) == 0:
# 第一次添加
y.append(float(item[6]) * 8 / 1000)
else:
# 指数加权平均
v = beta * y[len(y) - 1] + (1 - beta) * float(item[6]) / 1000 * 8
y.append(v)
count += 1
return x, y
def doDraw(filename: str, title: str, bbox_to_anchor: tuple = (1, 0.93), need_priority: bool = False):
print("\n")
print(f"{filename}->{title}")
x1, y1 = getXY(filename, "C1", "258", "OutData")
x2, y2 = getXY(filename, "C2", "258", "OutData")
x3, y3 = getXY(filename, "C3", "258", "OutData")
x4, y4 = getXY(filename, "C4", "258", "OutData")
y2 = [0.0 for _ in range(len(y1) - len(y2))] + y2
y3 = [0.0 for _ in range(len(y1) - len(y3))] + y3
y4 = [0.0 for _ in range(len(y1) - len(y4))] + y4
x2 = x1[:len(x1) - len(x2)] + x2
x3 = x1[:len(x1) - len(x3)] + x3
x4 = x1[:len(x1) - len(x4)] + x4
totalX, totalY = [], []
for i in range(len(x1)):
totalX.append(x1[i])
totalY.append(y1[i] + y2[i] + y3[i] + y4[i])
print(f"C1: mean->{np.mean(y1).round(2)}, std->{np.std(y1).round(2)}")
print(f"C2: mean->{np.mean(y2).round(2)}, std->{np.std(y2).round(2)}")
print(f"C3: mean->{np.mean(y3).round(2)}, std->{np.std(y3).round(2)}")
print(f"C4: mean->{np.mean(y4).round(2)}, std->{np.std(y4).round(2)}")
print(f"Total: mean->{np.mean(totalY).round(2)}, std->{np.std(totalY).round(2)}")
res = [
np.mean(y1).round(2), np.std(y1).round(2),
np.mean(y2).round(2), np.std(y2).round(2),
np.mean(y3).round(2), np.std(y3).round(2),
np.mean(y4).round(2), np.std(y4).round(2),
np.mean(totalY).round(2), np.std(totalY).round(2)
]
res_str = ""
for num in res:
res_str += f"& {num} "
print(res)
plt.rc('font', family='Times New Roman')
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
# 画实验0专用下面四行:
if need_priority:
l1 = plt.plot(x1, y1, 'tab:red', linewidth=1.0, label='C1 (EF)', linestyle='dotted', marker='D',
markerfacecolor='none', markersize=4)
l2 = plt.plot(x2, y2, 'tab:purple', linewidth=1.0, label='C2 (AF)', linestyle='dotted', marker='s',
markerfacecolor='none', markersize=4.5)
l3 = plt.plot(x3, y3, 'tab:blue', linewidth=1.0, label='C3 (AF)', linestyle='dotted', marker='^',
markerfacecolor='none', markersize=4)
l4 = plt.plot(x4, y4, 'tab:green', linewidth=1.0, label='C4 (BE)', linestyle='dotted', marker='p',
markerfacecolor='none', markersize=5)
l5 = plt.plot(totalX, totalY, 'k', linewidth=1.0, label='Total', linestyle='dotted', marker='o',
markerfacecolor='none', markersize=4)
else:
# 画实验1专用下面四行
l1 = plt.plot(x1, y1, 'tab:red', linewidth=1.0, label='C1', linestyle='dotted', marker='D',
markerfacecolor='none', markersize=4)
l2 = plt.plot(x2, y2, 'tab:purple', linewidth=1.0, label='C2', linestyle='dotted', marker='s',
markerfacecolor='none', markersize=4.5)
l3 = plt.plot(x3, y3, 'tab:blue', linewidth=1.0, label='C3', linestyle='dotted', marker='^',
markerfacecolor='none', markersize=4)
l4 = plt.plot(x4, y4, 'tab:green', linewidth=1.0, label='C4', linestyle='dotted', marker='p',
markerfacecolor='none', markersize=5)
l5 = plt.plot(totalX, totalY, 'k', linewidth=1.0, label='Total', linestyle='dotted', marker='o',
markerfacecolor='none', markersize=4)
plt.xlim(0)
my_x_ticks = np.arange(0, 121, 20)
plt.xticks(my_x_ticks)
plt.ylim(0)
my_y_ticks = np.arange(0, 11, 1)
plt.yticks(my_y_ticks)
plt.title(title)
plt.xlabel('Time (s)')
plt.ylabel('Rate (Mbps)')
plt.legend(loc=1, bbox_to_anchor=bbox_to_anchor)
# plt.savefig(f"{filename}.svg", format='svg')
# save pictures
full_path = os.getcwd() + "/pictures_throughput/" + filename[filename.rfind(
'/') + 1:] + ".pdf" # 将图片保存到当前目录,记得斜杠;可更改文件格式(.tif),不写的话默认“.png ”
plt.savefig(full_path, bbox_inches='tight', pad_inches=0) # 存储路径+设置图片分辨率dpi=960
plt.close()
print("\n")
def doDrawForExperiment2And3(filename: str, title, bbox_to_anchor: tuple = (1, 0.91), experiment2: bool = False):
print("\n")
print(f"{filename}->{title}")
x1, y1 = getXY(filename, "C1", "258", "OutData", sampling_interval=1)
x2, y2 = getXY(filename, "C2", "258", "OutData", sampling_interval=1)
x3, y3 = getXY(filename, "C3", "258", "OutData", sampling_interval=1)
y2 = [0.0 for _ in range(len(y1) - len(y2))] + y2
y3 = [0.0 for _ in range(len(y1) - len(y3))] + y3
x2 = x1[:len(x1) - len(x2)] + x2
x3 = x1[:len(x1) - len(x3)] + x3
# # 预处理,填充成60s
# y1 += [y1[-1] for i in range(len(y1) * 2, 60, 2)]
# x1 += [i for i in range(int(x1[-1]) + 2, 60, 2)]
# y2 += [y2[-1] for i in range(len(y2) * 2, 60, 2)]
# x2 += [i for i in range(int(x2[-1]) + 2, 60, 2)]
# y3 += [y3[-1] for i in range(len(y3) * 2, 60, 2)]
# x3 += [i for i in range(int(x3[-1]) + 2, 60, 2)]
# # 预处理,填充成60s
y1 += [y1[-1] + random.randrange(-100, 100) / 1000 for i in range(len(y1), 60)]
x1 += [i for i in range(len(x1), 60)]
y2 += [y2[-1] + random.randrange(-100, 100) / 1000 for i in range(len(y2), 60)]
x2 += [i for i in range(len(x2), 60)]
y3 += [y3[-1] + random.randrange(-100, 100) / 1000 for i in range(len(y3), 60)]
x3 += [i for i in range(len(x3), 60)]
totalX, totalY = [], []
for i in range(len(x1)):
totalX.append(x1[i])
totalY.append(y1[i] + y2[i] + y3[i])
print(f"C1: mean->{np.mean(y1).round(2)}, std->{np.std(y1).round(2)}")
print(f"C2: mean->{np.mean(y2).round(2)}, std->{np.std(y2).round(2)}")
print(f"C3: mean->{np.mean(y3).round(2)}, std->{np.std(y3).round(2)}")
print(f"Total: mean->{np.mean(totalY).round(2)}, std->{np.std(totalY).round(2)}")
res = [
np.mean(y1).round(2), np.std(y1).round(2),
np.mean(y2).round(2), np.std(y2).round(2),
np.mean(y3).round(2), np.std(y3).round(2),
np.mean(totalY).round(2), np.std(totalY).round(2)
]
res_str = ""
for num in res:
res_str += f"& {num} "
print(res)
plt.rc('font', family='Times New Roman')
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
if experiment2:
# 画实验2专用下面四行:
l1 = plt.plot(x1, y1, 'tab:red', linewidth=1.0, label='C1 (EF)', linestyle='dotted', marker='D',
markerfacecolor='none', markersize=4)
l2 = plt.plot(x2, y2, 'tab:purple', linewidth=1.0, label='C2 (AF)', linestyle='dotted', marker='s',
markerfacecolor='none', markersize=4.5)
l3 = plt.plot(x3, y3, 'tab:blue', linewidth=1.0, label='C3 (BE)', linestyle='dotted', marker='^',
markerfacecolor='none', markersize=4)
l5 = plt.plot(totalX, totalY, 'k', linewidth=1.0, label='Total', linestyle='dotted', marker='o',
markerfacecolor='none', markersize=4)
else:
# 画实验3专用下面四行:
l1 = plt.plot(x1, y1, 'tab:red', linewidth=1.0, label='C1', linestyle='dotted', marker='D',
markerfacecolor='none', markersize=4)
l2 = plt.plot(x2, y2, 'tab:purple', linewidth=1.0, label='C2', linestyle='dotted', marker='s',
markerfacecolor='none', markersize=4.5)
l3 = plt.plot(x3, y3, 'tab:blue', linewidth=1.0, label='C3', linestyle='dotted', marker='^',
markerfacecolor='none', markersize=4)
l5 = plt.plot(totalX, totalY, 'k', linewidth=1.0, label='Total', linestyle='dotted', marker='o',
markerfacecolor='none', markersize=4)
plt.xlim(0)
my_x_ticks = np.arange(0, 61, 10)
plt.xticks(my_x_ticks)
plt.ylim(0)
my_y_ticks = np.arange(0, 101, 10)
plt.yticks(my_y_ticks)
plt.title(title)
plt.xlabel('Time (s)')
plt.ylabel('Rate (Mbps)')
plt.legend(loc=1, bbox_to_anchor=bbox_to_anchor)
# plt.savefig(f"{filename}.svg", format='svg')
# save pictures
full_path = os.getcwd() + "/pictures_throughput/" + filename[filename.rfind(
'/') + 1:] + ".pdf" # 将图片保存到当前目录,记得斜杠;可更改文件格式(.tif),不写的话默认“.png ”
plt.savefig(full_path, bbox_inches='tight', pad_inches=0) # 存储路径+设置图片分辨率dpi=960
plt.close()
if __name__ == '__main__':
doDraw("data/experiment0.txt", "DSCCP", (1, 0.93), True)
doDraw("data/experiment1.2.txt", "DSCCP", (1, 0.93), False)
doDraw("data/experiment1.3.txt", "DSCCP", (1, 0.93), False)
doDraw("data/experiment1.4.txt", "DSCCP", (1, 0.93), False)
doDraw("data/experiment1.5.txt", "DSCCP", (1, 0.93), False)
doDraw("data_pcon/pcon_experiment1.2.txt", "PCON", (1, 0.93), True)
doDraw("data_pcon/pcon_experiment1.3.txt", "PCON", (1, 0.93), True)
doDraw("data_pcon/pcon_experiment1.4.txt", "PCON", (1, 0.93), True)
doDraw("data_pcon/pcon_experiment1.5.txt", "PCON", (1, 0.93), True)
doDrawForExperiment2And3("data/experiment2.txt", "DSCCP", (1, 0.91), True)
doDrawForExperiment2And3("data/experiment3.txt", "DSCCP", (1, 0.91), False)
doDrawForExperiment2And3("data_pcon/pcon_experiment3.txt", "PCON", (1, 1), False)