tcc/scripts/graph.py

127 lines
3.7 KiB
Python
Raw Normal View History

2023-09-11 02:14:21 +00:00
import numpy as np
import matplotlib.pyplot as plt
2023-09-24 15:47:17 +00:00
import os
2023-09-25 22:58:07 +00:00
from common import API_REQUESTS, FRAMEWORKS
2023-09-24 15:47:17 +00:00
2023-09-25 22:58:07 +00:00
FRAMEWORKS = [f for f, _ in FRAMEWORKS]
2023-09-24 15:47:17 +00:00
def plot_graph(x_data, y_data, title, x_label, y_label, filename):
for i, framework in enumerate(FRAMEWORKS):
plt.plot(x_data, y_data[i], markersize=1, linewidth=1, linestyle='solid', label=framework)
2023-09-11 02:14:21 +00:00
plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
2023-09-24 15:47:17 +00:00
plt.legend()
2023-09-11 02:14:21 +00:00
plt.savefig(f'{filename}.png')
plt.clf()
plt.close('all')
def plot_resource_graph(x_data, y_data, title, x_label, y_label, filename):
requests = x_data
2023-09-24 15:47:17 +00:00
frameworks = {}
print(y_data)
for i, framework in enumerate(FRAMEWORKS):
frameworks[framework] = y_data[i]
2023-09-11 02:14:21 +00:00
x = np.arange(len(requests))
2023-09-24 15:47:17 +00:00
width = 0.10
2023-09-11 02:14:21 +00:00
multiplier = 0
fig, ax = plt.subplots(layout='constrained')
2023-09-24 15:47:17 +00:00
print(x)
for framework, measurements in frameworks.items():
print(framework, measurements)
for attribute, measurement in frameworks.items():
2023-09-11 02:14:21 +00:00
offset = width * multiplier
rects = ax.bar(x + offset, measurement, width, label=attribute)
ax.bar_label(rects, padding=3)
multiplier += 1
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
ax.set_title(title)
ax.set_xticks(x + (width/2), requests)
2023-09-24 15:47:17 +00:00
ax.legend(loc='upper left', ncols=len(frameworks.items()))
ax.set_ylim(0, 120)
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:
x.append(int(line[0])) # requests
y.append([float(v)*100 for v in line[1:]]) # cpu, ram
return x, y
def generate_req_graph(filename, framework_name, endpoint_name):
2023-09-24 15:47:17 +00:00
x, _ = get_data(filename)
y = []
for f in FRAMEWORKS:
newfile = filename.replace(framework_name, f)
2023-09-25 23:52:02 +00:00
_, y_data = get_data(newfile)
2023-09-24 15:47:17 +00:00
y.append(y_data)
2023-09-11 02:14:21 +00:00
2023-09-24 15:47:17 +00:00
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-09-24 15:47:17 +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("?", "")}'
plot_resource_graph(x, y, f'Uso de {resource.upper()} - {endpoint_name}', 'Número de requisições', 'Uso (%)', graph_file)
if __name__ == '__main__':
endpoints = [config[0] for config in API_REQUESTS]
for endpoint_name in endpoints:
2023-09-25 22:58:07 +00:00
framework_name = 'ASP.NET'
2023-09-24 15:47:17 +00:00
endpoint_file = endpoint_name.replace('/', '')
2023-09-25 22:58:07 +00:00
filename = f'data/resource_ASP.NET_{endpoint_file}.csv'
2023-09-24 15:47:17 +00:00
generate_resource_graph(filename, framework_name, endpoint_name)
2023-09-11 02:14:21 +00:00
2023-09-25 22:58:07 +00:00
filename = f'data/req_ASP.NET_{endpoint_file}.csv'
2023-09-24 15:47:17 +00:00
generate_req_graph(filename, framework_name, endpoint_name)