From 92245c468ad26df240997551041b96b50fd989fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Henrique=20Ivanchechen?= Date: Thu, 24 Aug 2023 20:52:15 -0300 Subject: [PATCH] melhorando testes --- .gitignore | 3 +- ASP.NET/Controllers/ImageController.cs | 8 ++-- ASP.NET/Dockerfile | 3 +- scripts/graph.py | 30 +++++++++++++ scripts/testes.py | 58 ++++++++++++++++++++------ 5 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 scripts/graph.py diff --git a/.gitignore b/.gitignore index 93870c6..39584b1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vs bin obj -*.png \ No newline at end of file +*.png +*.csv \ No newline at end of file diff --git a/ASP.NET/Controllers/ImageController.cs b/ASP.NET/Controllers/ImageController.cs index ffbd67e..ec6ea91 100644 --- a/ASP.NET/Controllers/ImageController.cs +++ b/ASP.NET/Controllers/ImageController.cs @@ -34,11 +34,11 @@ namespace TCC.Controllers [HttpGet("load-image")] public async Task GetSimpleImage() { - var result = ImageService.GetSimpleImage(); + //var result = ImageService.GetSimpleImage(); + var result = System.IO.File.ReadAllBytes("simpleimage.png"); - var imageStream = new MemoryStream(); - result.Write(imageStream); - imageStream.Position = 0; + var imageStream = new byte[result.Length]; + result.CopyTo(imageStream, 0); return File(imageStream, "image/png"); } diff --git a/ASP.NET/Dockerfile b/ASP.NET/Dockerfile index 8380598..ff81a83 100644 --- a/ASP.NET/Dockerfile +++ b/ASP.NET/Dockerfile @@ -15,7 +15,8 @@ FROM mcr.microsoft.com/dotnet/aspnet:6.0-bullseye-slim WORKDIR /App -RUN wget https://files.ivanch.me/api/public/dl/QFCLgtrG/simpleimage.png && \ +RUN apt update && apt install wget -y && \ + wget https://files.ivanch.me/api/public/dl/QFCLgtrG/simpleimage.png && \ wget https://files.ivanch.me/api/public/dl/E0VLgWbx/bigimage.png && \ rm -rf runtimes diff --git a/scripts/graph.py b/scripts/graph.py new file mode 100644 index 0000000..12e0d5f --- /dev/null +++ b/scripts/graph.py @@ -0,0 +1,30 @@ +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') + +def getData(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 generateGraph(filename, framework_name, endpoint_name): + x, y = getData(filename) + plot_graph(x, y, f'{framework_name} - {endpoint_name}', 'Number of requests', 'Requests per second', filename.split('.')[0]) + +if __name__ == '__main__': + generateGraph('data.txt', 'ASP.NET', 'test') \ No newline at end of file diff --git a/scripts/testes.py b/scripts/testes.py index d0eb763..bdc1ad3 100644 --- a/scripts/testes.py +++ b/scripts/testes.py @@ -1,33 +1,65 @@ import requests import concurrent.futures import time +import sys +from graph import generateGraph -URL_BASE = 'http://localhost:5100' +if len(sys.argv) != 2 or sys.argv[1] == '-h' or sys.argv[1] == '--help': + print("Usage: python testes.py ") + sys.exit(1) -num_requests = [1000, 5000, 10_000, 50_000, 100_000, 500_000, 1_000_000] +FRAMEWORK_NAME = sys.argv[1] +URL_BASE = 'http://localhost:9080' +ENDPOINTS = [ + '/status/ok', + '/image/load-image', +] -def send_request(session, url): - response = session.get(url) +num_requests = [10, 1000, 5000, 10_000, 50_000, 100_000, 500_000, 1_000_000] + +def send_request(url): + success = False + while not success: + try: + response = requests.get(url) + except: + continue + success = response.status_code == 200 return response.status_code -def main(): - for num_request in num_requests: +def getFileName(endpoint): + endpoint = endpoint.replace('/', '_') + return f"{FRAMEWORK_NAME}_{endpoint}.csv" + +def record(filename, requests, reqpersec): + with open(filename, "a") as file: + file.write(f"{requests},{reqpersec}\n") + +def run_tests(endpoint): + filename = getFileName(endpoint) + + for num_request in range(0, 50_000, 1000): + if num_request <= 0: continue + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: - url = f'{URL_BASE}/status/ok' + url = f'{URL_BASE}{endpoint}' start_time = time.time() futures = [] - with requests.Session() as session: - futures = [executor.submit(send_request, session, url) for _ in range(num_request)] + #with requests.Session() as session: + # futures = [executor.submit(send_request, session, url) for _ in range(num_request)] + futures = [executor.submit(send_request, url) for _ in range(num_request)] concurrent.futures.wait(futures) elapsed_time = time.time() - start_time - successful_responses = sum(1 for future in futures if future.result() == 200) + print(f"{num_request}: {elapsed_time:.2f} seconds. {elapsed_time/num_request:.4f} seconds per request. {num_request/elapsed_time:.2f} requests per second.") + record(filename, num_request, f"{num_request/elapsed_time:.2f}") + generateGraph(filename, FRAMEWORK_NAME, endpoint) - print(f"All requests completed in {elapsed_time:.2f} seconds. {elapsed_time/num_request:.4f} seconds per request. {num_request/elapsed_time:.2f} requests per second.") - print(f"Successful responses: {successful_responses}/{num_request}") + time.sleep(3) -main() \ No newline at end of file +for endpoint in ENDPOINTS: + run_tests(endpoint)