tcc/scripts/graph.py

173 lines
5.1 KiB
Python
Raw Normal View History

2023-09-11 02:14:21 +00:00
import numpy as np
import matplotlib.pyplot as plt
2023-10-03 18:31:33 +00:00
from common import API_REQUESTS, FRAMEWORKS
2023-10-29 22:01:24 +00:00
from pylab import plot, show, savefig, xlim, figure, \
ylim, legend, boxplot, setp, axes
2023-10-03 18:31:33 +00:00
2023-10-29 22:01:24 +00:00
FRAMEWORKS_COLORS = [c for _, _, c in FRAMEWORKS]
FRAMEWORKS = [f for f, _, _ in FRAMEWORKS]
2023-10-03 18:31:33 +00:00
2023-10-29 22:01:24 +00:00
def setBoxColors(bp):
for i, box in enumerate(bp['boxes']):
box.set(color=FRAMEWORKS_COLORS[i])
2023-11-02 22:14:16 +00:00
for i, median in enumerate(bp['medians']):
# median.set(color=FRAMEWORKS_COLORS[i])
median.set(color='white')
for i in range(len(FRAMEWORKS)):
bp['whiskers'][i*2].set(color=FRAMEWORKS_COLORS[i])
bp['whiskers'][i*2 + 1].set(color=FRAMEWORKS_COLORS[i])
for i in range(len(FRAMEWORKS)):
bp['caps'][i*2].set(color=FRAMEWORKS_COLORS[i])
bp['caps'][i*2 + 1].set(color=FRAMEWORKS_COLORS[i])
2023-09-11 02:14:21 +00:00
2023-10-29 22:01:24 +00:00
def plot_graph(x_data, y_data, title, x_label, y_label, filename, y_lim = None):
old_x_data = x_data
old_y_data = y_data
2023-09-11 02:14:21 +00:00
2023-10-29 22:01:24 +00:00
x_data = []
y_data = []
2023-09-11 02:14:21 +00:00
2023-10-29 22:01:24 +00:00
for i in range(len(old_x_data)):
if old_x_data[i] not in x_data:
x_data.append(old_x_data[i])
y_data.append([])
2023-11-02 22:14:16 +00:00
for f in range(len(FRAMEWORKS)):
2023-10-29 22:01:24 +00:00
y_data[-1].append([])
2023-09-11 02:14:21 +00:00
2023-11-02 22:14:16 +00:00
for f in range(len(FRAMEWORKS)):
2023-10-29 22:01:24 +00:00
y_data[-1][f].append(old_y_data[f][i])
2023-09-11 02:14:21 +00:00
2023-10-29 22:01:24 +00:00
fig = figure()
ax = axes()
2023-09-11 02:14:21 +00:00
2023-10-29 22:01:24 +00:00
all_positions = []
2023-10-03 18:31:33 +00:00
2023-11-02 22:14:16 +00:00
print(filename)
# print(y_data)
2023-10-29 22:01:24 +00:00
for j in range(len(x_data)):
2023-11-02 22:14:16 +00:00
positions = [len(FRAMEWORKS)*j + i + 2*j for i in range(len(FRAMEWORKS))]
bp = boxplot(y_data[j], positions = positions, widths = 0.6, showfliers=False,
patch_artist=True)
2023-10-29 22:01:24 +00:00
all_positions.append(positions)
setBoxColors(bp)
2023-09-11 02:14:21 +00:00
2023-11-02 22:14:16 +00:00
for i in range(len(FRAMEWORKS)):
medians = []
for j in range(len(x_data)):
medians.append(np.median(y_data[j][i]))
positions = [all_positions[x][i] for x in range(len(x_data))]
plt.plot(positions, medians, color=FRAMEWORKS_COLORS[i], marker='.', linestyle='--', linewidth=0.3)
2023-10-29 22:01:24 +00:00
avg_positions = []
for positions in all_positions:
2023-11-02 22:14:16 +00:00
avg = np.average(positions)
avg_positions.append(avg)
2023-09-11 02:14:21 +00:00
2023-10-29 22:01:24 +00:00
ax.set_xticks(avg_positions)
ax.set_xticklabels([str(x) for x in x_data])
2023-09-11 02:14:21 +00:00
2023-10-29 22:01:24 +00:00
plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
if y_lim:
plt.ylim(y_lim)
2023-11-02 22:14:16 +00:00
if filename.startswith('req'):
for i, framework in enumerate(FRAMEWORKS):
h, = plt.plot([], c=FRAMEWORKS_COLORS[i], label=framework, marker='.', linestyle='--')
# h.set_visible(False)
plt.legend()
2023-10-29 22:01:24 +00:00
2023-11-02 22:14:16 +00:00
plt.tight_layout()
2023-09-11 02:14:21 +00:00
plt.savefig(f'{filename}.png')
plt.clf()
plt.close('all')
def get_data(filename):
lines = []
with open(filename, 'r') as f:
lines = f.readlines()
x = []
y = []
for line in lines:
line = line.strip().split(',')
if line:
x.append(int(line[0]))
y.append(float(line[1]))
return x, y
def get_resource_data(filename):
lines = []
with open(filename, 'r') as f:
lines = f.readlines()
x = []
y = []
for line in lines:
line = line.strip().split(',')
if line:
2023-10-03 18:31:33 +00:00
r = [round(float(line[1])*100), round(float(line[2])*100)]
if r[0] > 100:
r[0] = 100
if r[1] > 100:
r[1] = 100
2023-09-11 02:14:21 +00:00
x.append(int(line[0])) # requests
2023-10-03 18:31:33 +00:00
y.append(r) # cpu, ram
2023-09-11 02:14:21 +00:00
return x, y
def generate_req_graph(filename, framework_name, endpoint_name):
2023-10-03 18:31:33 +00:00
x, _ = get_data(filename)
y = []
for f in FRAMEWORKS:
newfile = filename.replace(framework_name, f)
_, y_data = get_data(newfile)
2023-09-11 02:14:21 +00:00
2023-10-03 18:31:33 +00:00
y.append(y_data)
graph_file = f'req_{endpoint_name.replace("/", "").replace("?", "")}'
plot_graph(x, y, f'Requisições atendidas por segundo - {endpoint_name}', 'Número de requisições', 'Requisições/segundo', graph_file)
2023-09-11 02:14:21 +00:00
def generate_resource_graph(filename, framework_name, endpoint_name):
2023-10-03 18:31:33 +00:00
x, _ = get_resource_data(filename)
for resource_index, resource in enumerate(['cpu', 'ram']):
y = []
for f in FRAMEWORKS:
newfile = filename.replace(framework_name, f)
_, y_data = get_resource_data(newfile)
y.append([data[resource_index] for data in y_data])
graph_file = f'{resource}_{endpoint_name.replace("/", "").replace("?", "")}'
2023-10-29 22:01:24 +00:00
plot_graph(x, y, f'Uso de {resource.upper()} - {endpoint_name}', 'Número de requisições', f'Uso de {resource.upper()} (%)', graph_file)
2023-10-03 18:31:33 +00:00
if __name__ == '__main__':
endpoints = [config[0] for config in API_REQUESTS]
for endpoint_name in endpoints:
framework_name = 'ASP.NET'
endpoint_file = endpoint_name.replace('/', '')
2023-11-02 22:14:16 +00:00
endpoint_name = '/' + endpoint_name.split('/')[-1]
2023-10-03 18:31:33 +00:00
filename = f'data/resource_ASP.NET_{endpoint_file}.csv'
filename = filename.replace("?", "_")
2023-10-03 18:31:33 +00:00
generate_resource_graph(filename, framework_name, endpoint_name)
2023-09-11 02:14:21 +00:00
2023-10-03 18:31:33 +00:00
filename = f'data/req_ASP.NET_{endpoint_file}.csv'
filename = filename.replace("?", "_")
2023-10-03 18:31:33 +00:00
generate_req_graph(filename, framework_name, endpoint_name)