tcc/scripts/graph.py

155 lines
4.7 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
from common import API_REQUESTS, FRAMEWORKS
from pylab import plot, show, savefig, xlim, figure, \
ylim, legend, boxplot, setp, axes
FRAMEWORKS_COLORS = [c for _, _, c in FRAMEWORKS]
FRAMEWORKS = [f for f, _, _ in FRAMEWORKS]
def setBoxColors(bp):
for i, box in enumerate(bp['boxes']):
box.set(color=FRAMEWORKS_COLORS[i])
box.set(facecolor=FRAMEWORKS_COLORS[i])
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])
for i, median in enumerate(bp['medians']):
median.set(color='white')
def plot_graph(x_data, y_data, title, x_label, y_label, filename, y_lim = None):
print(filename)
old_x_data = x_data
old_y_data = y_data
x_data = []
y_data = []
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([])
for f in range(len(FRAMEWORKS)):
y_data[-1].append([])
for f in range(len(FRAMEWORKS)):
y_data[-1][f].append(old_y_data[f][i])
fig, axes = plt.subplots(ncols=len(x_data), sharey=True)
for ax, j in zip(axes, [i for i in range(len(x_data))]):
bp = ax.boxplot(y_data[j], showfliers=False, patch_artist=True, positions=[i for i in range(len(FRAMEWORKS))])
ax.set(xlabel=x_data[j], xticklabels=['' for _ in range(len(FRAMEWORKS))])
ax.margins(0.05)
setBoxColors(bp)
if j % 2 == 1:
ax.set_facecolor('#f2f2f2')
# set title
fig.suptitle(title)
fig.supxlabel(x_label)
fig.supylabel(y_label)
if y_lim:
plt.ylim(y_lim)
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()
plt.tight_layout()
fig.subplots_adjust(hspace=0, wspace=0)
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:
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
x.append(int(line[0])) # requests
y.append(r) # cpu, ram
return x, y
def generate_req_graph(filename, framework_name, endpoint_name):
x, _ = get_data(filename)
y = []
for f in FRAMEWORKS:
newfile = filename.replace(framework_name, f)
_, y_data = get_data(newfile)
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)
def generate_resource_graph(filename, framework_name, endpoint_name):
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("?", "")}'
plot_graph(x, y, f'Uso de {resource.upper()} - {endpoint_name}', 'Número de requisições', f'Uso de {resource.upper()} (%)', graph_file)
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('/', '')
endpoint_name = '/' + endpoint_name.split('/')[-1]
filename = f'data/resource_ASP.NET_{endpoint_file}.csv'
filename = filename.replace("?", "_")
generate_resource_graph(filename, framework_name, endpoint_name)
filename = f'data/req_ASP.NET_{endpoint_file}.csv'
filename = filename.replace("?", "_")
generate_req_graph(filename, framework_name, endpoint_name)