import numpy as np import matplotlib.pyplot as plt import os from common import API_REQUESTS, FRAMEWORKS FRAMEWORKS = [f for f, _ in FRAMEWORKS] 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) plt.title(title) plt.xlabel(x_label) plt.ylabel(y_label) plt.legend() 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 frameworks = {} print(y_data) for i, framework in enumerate(FRAMEWORKS): frameworks[framework] = y_data[i] x = np.arange(len(requests)) width = 0.10 multiplier = 0 fig, ax = plt.subplots(layout='constrained') print(x) for framework, measurements in frameworks.items(): print(framework, measurements) for attribute, measurement in frameworks.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/2), requests) ax.legend(loc='upper left', ncols=len(frameworks.items())) ax.set_ylim(0, 120) 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): x, _ = get_data(filename) y = [] for f in FRAMEWORKS: newfile = filename.replace(framework_name, f) _, y_data = get_resource_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_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: framework_name = 'ASP.NET' endpoint_file = endpoint_name.replace('/', '') filename = f'data/resource_ASP.NET_{endpoint_file}.csv' generate_resource_graph(filename, framework_name, endpoint_name) filename = f'data/req_ASP.NET_{endpoint_file}.csv' generate_req_graph(filename, framework_name, endpoint_name)