tentativa de ram e cpu

This commit is contained in:
José Henrique Ivanchechen 2023-08-31 18:47:04 -03:00
parent 2085b7d89b
commit c101d0ca5e
2 changed files with 124 additions and 20 deletions

View File

@ -1,3 +1,4 @@
import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
def plot_graph(x, y, title, x_label, y_label, filename): def plot_graph(x, y, title, x_label, y_label, filename):
@ -9,7 +10,39 @@ def plot_graph(x, y, title, x_label, y_label, filename):
plt.clf() plt.clf()
def getData(filename): 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):
lines = [] lines = []
with open(filename, 'r') as f: with open(filename, 'r') as f:
lines = f.readlines() lines = f.readlines()
@ -24,11 +57,29 @@ def getData(filename):
return x, y return x, y
def generateGraph(filename, framework_name, endpoint_name): def get_resource_data(filename):
x, y = 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])) # 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)
new_filename = filename.replace('.txt', '').replace('.csv', '') 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__': def generate_resource_graph(filename, framework_name, endpoint_name):
generateGraph('data.txt', 'Teste', 'endpoint') 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 requests
import docker
import concurrent.futures import concurrent.futures
import time import time
import sys import sys
import os import os
from graph import generateGraph from graph import generate_req_graph, generate_resource_graph
from math import floor from math import floor
if len(sys.argv) != 2 or sys.argv[1] == '-h' or sys.argv[1] == '--help': if len(sys.argv) != 3 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
print("Usage: python testes.py <name>") print("Usage: python testes.py <framework name> <container name>")
sys.exit(1) sys.exit(1)
THREADS = 10 THREADS = 10
FRAMEWORK_NAME = sys.argv[1] FRAMEWORK_NAME = sys.argv[1]
CONTAINER_NAME = sys.argv[2]
URL_BASE = 'http://172.26.48.1:9080' URL_BASE = 'http://172.26.48.1:9080'
API_REQUESTS = [ API_REQUESTS = [
('/status/ok', range(0, 50_000, 5000)), ('/status/ok', range(0, 30_000, 5000)),
('/image/load-image', range(0, 50_000, 5000)), ('/image/load-image', range(0, 30_000, 5000)),
('/image/load-big-image', range(0, 1_000, 10)), ('/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): def send_request(url):
@ -35,18 +40,29 @@ def send_request(url):
responses[floor(response.status_code/100)] += 1 responses[floor(response.status_code/100)] += 1
return responses return responses
def getFileName(endpoint): def getFileNames(endpoint):
endpoint = endpoint.replace('/', '_') endpoint = endpoint.replace('/', '')
return f"{FRAMEWORK_NAME}_{endpoint}.csv"
files = [
f"req_{FRAMEWORK_NAME}_{endpoint}.csv",
f"resource_{FRAMEWORK_NAME}_{endpoint}.csv",
]
return files
def record(filename, requests, reqpersec): def record(filename, requests, reqpersec):
with open(filename, "a") as file: with open(filename, "a") as file:
file.write(f"{requests},{reqpersec}\n") 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): def run_tests(endpoint, num_requests):
filename = getFileName(endpoint) files = getFileNames(endpoint)
if os.path.exists(filename): for filename in files:
os.remove(filename) if os.path.exists(filename):
os.remove(filename)
for num_request in num_requests: for num_request in num_requests:
if num_request <= 0: continue if num_request <= 0: continue
@ -54,6 +70,7 @@ def run_tests(endpoint, num_requests):
ok_responses = 0 ok_responses = 0
bad_responses = 0 bad_responses = 0
server_errors = 0 server_errors = 0
cpu, ram = 0, 0
with concurrent.futures.ThreadPoolExecutor(max_workers=THREADS) as executor: with concurrent.futures.ThreadPoolExecutor(max_workers=THREADS) as executor:
url = f'{URL_BASE}{endpoint}' url = f'{URL_BASE}{endpoint}'
@ -67,6 +84,8 @@ def run_tests(endpoint, num_requests):
concurrent.futures.wait(futures) concurrent.futures.wait(futures)
cpu, ram = get_resource_usage()
elapsed_time = time.time() - start_time elapsed_time = time.time() - start_time
for future in futures: for future in futures:
@ -76,11 +95,45 @@ def run_tests(endpoint, num_requests):
server_errors += responses[5] 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}]]") 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}") record(files[0], num_request, f"{num_request/elapsed_time:.2f}")
generateGraph(filename, FRAMEWORK_NAME, endpoint) record_resource(files[1], num_request, cpu, ram)
generate_req_graph(files[0], FRAMEWORK_NAME, endpoint)
generate_req_graph(files[1], FRAMEWORK_NAME, endpoint)
time.sleep(3) 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']['cpu_usage']['system_cpu_usage'] - stats['precpu_stats']['cpu_usage']['system_cpu_usage']
len_cpu = len(stats['cpu_stats']['cpu_usage']['percpu_usage'])
percentage = (UsageDelta / SystemDelta) * len_cpu * 100
# percent = round(percentage, 2)
return f"{percentage:.2f}"
def get_ram_usage(stats):
usage = stats['memory_stats']['usage']
limit = stats['memory_stats']['limit']
percentage = (usage / limit) * 100
# percent = round(percentage, 2)
return f"{percentage:.2f}"
for endpoint, num_requests in API_REQUESTS: for endpoint, num_requests in API_REQUESTS:
print(f"# {endpoint}") print(f"# {endpoint}")
run_tests(endpoint, num_requests) run_tests(endpoint, num_requests)