import requests import concurrent.futures import time import sys import os from graph import generateGraph from math import floor if len(sys.argv) != 2 or sys.argv[1] == '-h' or sys.argv[1] == '--help': print("Usage: python testes.py ") sys.exit(1) THREADS = 10 FRAMEWORK_NAME = sys.argv[1] URL_BASE = 'http://172.26.48.1:9080' API_REQUESTS = [ ('/status/ok', range(0, 50_000, 5000)), ('/image/load-image', range(0, 50_000, 5000)), ('/image/load-big-image', range(0, 1_000, 10)), ] def send_request(url): success = False responses = { 2: 0, # OK 4: 0, # Bad Request 5: 0, # Server Error } while not success: try: response = requests.get(url) except: continue success = response.status_code == 200 responses[floor(response.status_code/100)] += 1 return responses 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, num_requests): filename = getFileName(endpoint) if os.path.exists(filename): os.remove(filename) for num_request in num_requests: if num_request <= 0: continue ok_responses = 0 bad_responses = 0 server_errors = 0 with concurrent.futures.ThreadPoolExecutor(max_workers=THREADS) as executor: 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)] futures = [executor.submit(send_request, url) for _ in range(num_request)] concurrent.futures.wait(futures) elapsed_time = time.time() - start_time for future in futures: responses = future.result() ok_responses += responses[2] bad_responses += responses[4] server_errors += responses[5] print(f"{num_request}: {elapsed_time:.2f} seconds. {elapsed_time/num_request:.4f} seconds per request. {num_request/elapsed_time:.2f} requests per second. [OK: {ok_responses}, Bad Request: {bad_responses}, Server Error: {server_errors}]]") record(filename, num_request, f"{num_request/elapsed_time:.2f}") generateGraph(filename, FRAMEWORK_NAME, endpoint) time.sleep(3) for endpoint, num_requests in API_REQUESTS: print(f"# {endpoint}") run_tests(endpoint, num_requests)