tcc/scripts/graph.py

85 lines
2.3 KiB
Python
Raw Normal View History

2023-08-31 21:47:04 +00:00
import numpy as np
2023-08-24 23:52:15 +00:00
import matplotlib.pyplot as plt
def plot_graph(x, y, title, x_label, y_label, filename):
plt.plot(x, y, 'ro', markersize=1, linewidth=0.5, linestyle='solid')
plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.savefig(f'{filename}.png')
2023-08-25 12:24:01 +00:00
plt.clf()
2023-08-31 21:47:04 +00:00
def plot_resource_graph(x, y, title, x_label, y_label, filename):
requests = x
resource = {
'CPU': (p[0] for p in y),
'RAM': (p[1] for p in y),
}
x = np.arange(len(requests))
width = 0.25
multiplier = 0
fig, ax = plt.subplots(layout='constrained')
for attribute, measurement in resource.items():
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, requests)
ax.legend(loc='upper left', ncols=len(resource.items()))
ax.set_ylim(0, 250)
plt.savefig(f'resources_{filename}.png')
plt.clf()
def get_data(filename):
2023-08-24 23:52:15 +00:00
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
2023-08-31 21:47:04 +00:00
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) for v in line[1:]]) # cpu, ram
return x, y
def generate_req_graph(filename, framework_name, endpoint_name):
x, y = get_data(filename)
2023-08-29 18:55:28 +00:00
new_filename = filename.replace('.txt', '').replace('.csv', '')
2023-08-31 21:47:04 +00:00
plot_graph(x, y, f'{framework_name} - {endpoint_name}', 'Número de requisições', 'Requisições por segundo', new_filename)
2023-08-24 23:52:15 +00:00
2023-08-31 21:47:04 +00:00
def generate_resource_graph(filename, framework_name, endpoint_name):
x, y = get_resource_data(filename)
new_filename = filename.replace('.txt', '').replace('.csv', '')
plot_resource_graph(x, y, f'{framework_name} - {endpoint_name}', 'Uso de recursos', 'Uso (%)', new_filename)