Compare commits

...

5 Commits

Author SHA1 Message Date
José Henrique Ivanchechen 51d09be512 fixes 2023-08-31 20:07:14 -03:00
José Henrique Ivanchechen 209275cce0 change 2023-08-31 19:46:13 -03:00
José Henrique Ivanchechen 5199519634 fix 2023-08-31 19:42:32 -03:00
José Henrique Ivanchechen 83605f9095 fix cpu 2023-08-31 19:18:37 -03:00
José Henrique Ivanchechen c101d0ca5e tentativa de ram e cpu 2023-08-31 18:47:04 -03:00
2 changed files with 127 additions and 22 deletions

View File

@ -1,3 +1,4 @@
import numpy as np
import matplotlib.pyplot as plt
def plot_graph(x, y, title, x_label, y_label, filename):
@ -8,8 +9,42 @@ def plot_graph(x, y, title, x_label, y_label, filename):
plt.savefig(f'{filename}.png')
plt.clf()
plt.close('all')
def getData(filename):
def plot_resource_graph(x_data, y_data, title, x_label, y_label, filename):
requests = x_data
resource = {
'CPU': [p[0] for p in y_data],
'RAM': [p[1] for p in y_data],
}
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, 100)
plt.savefig(f'resources_{filename}.png')
plt.clf()
plt.close('all')
def get_data(filename):
lines = []
with open(filename, 'r') as f:
lines = f.readlines()
@ -24,11 +59,29 @@ def getData(filename):
return x, y
def generateGraph(filename, framework_name, endpoint_name):
x, y = getData(filename)
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, y = get_data(filename)
new_filename = filename.replace('.txt', '').replace('.csv', '')
plot_graph(x, y, f'{framework_name} - {endpoint_name}', 'Number of requests', 'Requests per second', new_filename)
plot_graph(x, y, f'{framework_name} - {endpoint_name}', 'Número de requisições', 'Requisições por segundo', new_filename)
if __name__ == '__main__':
generateGraph('data.txt', 'Teste', 'endpoint')
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)

View File

@ -1,22 +1,27 @@
import requests
import docker
import concurrent.futures
import time
import sys
import os
from graph import generateGraph
from graph import generate_req_graph, generate_resource_graph
from math import floor
if len(sys.argv) != 2 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
print("Usage: python testes.py <name>")
if len(sys.argv) != 3 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
print("Usage: python testes.py <framework name> <container name>")
sys.exit(1)
THREADS = 10
FRAMEWORK_NAME = sys.argv[1]
URL_BASE = 'http://172.26.48.1:9080'
CONTAINER_NAME = sys.argv[2]
URL_BASE = 'http://localhost: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)),
('/status/ok', range(0, 30_000, 5000)),
('/image/load-image', range(0, 30_000, 5000)),
('/static/simpleimage.png', range(0, 30_000, 5000)),
('/image/load-big-image', range(0, 1_000, 200)),
('/static/bigimage.png', range(0, 1_000, 200)),
('/static/video.mp4', range(0, 10_000, 1_000)),
]
def send_request(url):
@ -35,18 +40,29 @@ def send_request(url):
responses[floor(response.status_code/100)] += 1
return responses
def getFileName(endpoint):
endpoint = endpoint.replace('/', '_')
return f"{FRAMEWORK_NAME}_{endpoint}.csv"
def getFileNames(endpoint):
endpoint = endpoint.replace('/', '')
files = [
f"req_{FRAMEWORK_NAME}_{endpoint}.csv",
f"resource_{FRAMEWORK_NAME}_{endpoint}.csv",
]
return files
def record(filename, requests, reqpersec):
with open(filename, "a") as file:
file.write(f"{requests},{reqpersec}\n")
def record_resource(filename, requests, cpu, ram):
with open(filename, "a") as file:
file.write(f"{requests},{cpu},{ram}\n")
def run_tests(endpoint, num_requests):
filename = getFileName(endpoint)
if os.path.exists(filename):
os.remove(filename)
files = getFileNames(endpoint)
for filename in files:
if os.path.exists(filename):
os.remove(filename)
for num_request in num_requests:
if num_request <= 0: continue
@ -54,6 +70,7 @@ def run_tests(endpoint, num_requests):
ok_responses = 0
bad_responses = 0
server_errors = 0
cpu, ram = 0, 0
with concurrent.futures.ThreadPoolExecutor(max_workers=THREADS) as executor:
url = f'{URL_BASE}{endpoint}'
@ -63,7 +80,13 @@ def run_tests(endpoint, num_requests):
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)]
half = floor(num_request/2)
for i in range(num_request):
futures.append(executor.submit(send_request, url))
if i == half:
cpu, ram = get_resource_usage()
concurrent.futures.wait(futures)
@ -76,11 +99,40 @@ def run_tests(endpoint, num_requests):
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)
record(files[0], num_request, f"{num_request/elapsed_time:.2f}")
record_resource(files[1], num_request, cpu, ram)
generate_req_graph(files[0], FRAMEWORK_NAME, endpoint)
generate_resource_graph(files[1], FRAMEWORK_NAME, endpoint)
time.sleep(3)
def get_resource_usage():
try:
client = docker.from_env()
stats = client.containers.get(CONTAINER_NAME).stats(stream=False)
except:
return 0, 0 # unable to get stats
return get_cpu_usage(stats), get_ram_usage(stats)
def get_cpu_usage(stats):
UsageDelta = stats['cpu_stats']['cpu_usage']['total_usage'] - stats['precpu_stats']['cpu_usage']['total_usage']
SystemDelta = stats['cpu_stats']['system_cpu_usage'] - stats['precpu_stats']['system_cpu_usage']
len_cpu = stats['cpu_stats']['online_cpus']
percentage = (UsageDelta / SystemDelta) * len_cpu
return f"{percentage:.2f}"
def get_ram_usage(stats):
usage = stats['memory_stats']['usage']
limit = stats['memory_stats']['limit']
percentage = (usage / limit)
# percent = round(percentage, 2)
return f"{percentage:.2f}"
for endpoint, num_requests in API_REQUESTS:
print(f"# {endpoint}")
run_tests(endpoint, num_requests)