mirror of https://github.com/ivanch/tcc.git
Compare commits
5 Commits
2085b7d89b
...
51d09be512
Author | SHA1 | Date |
---|---|---|
José Henrique Ivanchechen | 51d09be512 | |
José Henrique Ivanchechen | 209275cce0 | |
José Henrique Ivanchechen | 5199519634 | |
José Henrique Ivanchechen | 83605f9095 | |
José Henrique Ivanchechen | c101d0ca5e |
|
@ -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):
|
||||||
|
@ -8,8 +9,42 @@ def plot_graph(x, y, title, x_label, y_label, filename):
|
||||||
plt.savefig(f'{filename}.png')
|
plt.savefig(f'{filename}.png')
|
||||||
|
|
||||||
plt.clf()
|
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 = []
|
lines = []
|
||||||
with open(filename, 'r') as f:
|
with open(filename, 'r') as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
|
@ -24,11 +59,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)*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', '')
|
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)
|
|
@ -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]
|
||||||
URL_BASE = 'http://172.26.48.1:9080'
|
CONTAINER_NAME = sys.argv[2]
|
||||||
|
URL_BASE = 'http://localhost: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}'
|
||||||
|
@ -63,7 +80,13 @@ def run_tests(endpoint, num_requests):
|
||||||
futures = []
|
futures = []
|
||||||
#with requests.Session() as session:
|
#with requests.Session() as session:
|
||||||
# futures = [executor.submit(send_request, session, url) for _ in range(num_request)]
|
# 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)
|
concurrent.futures.wait(futures)
|
||||||
|
|
||||||
|
@ -76,11 +99,40 @@ 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_resource_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']['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:
|
for endpoint, num_requests in API_REQUESTS:
|
||||||
print(f"# {endpoint}")
|
print(f"# {endpoint}")
|
||||||
run_tests(endpoint, num_requests)
|
run_tests(endpoint, num_requests)
|
||||||
|
|
Loading…
Reference in New Issue