mirror of https://github.com/ivanch/tcc.git
Compare commits
No commits in common. "51d09be512076da6f3ae6169c95a9a428073072f" and "2085b7d89bec1a4d6c6561961f9a43bcefe94458" have entirely different histories.
51d09be512
...
2085b7d89b
|
@ -1,4 +1,3 @@
|
||||||
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,42 +8,8 @@ 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 plot_resource_graph(x_data, y_data, title, x_label, y_label, filename):
|
def getData(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()
|
||||||
|
@ -59,29 +24,11 @@ def get_data(filename):
|
||||||
|
|
||||||
return x, y
|
return x, y
|
||||||
|
|
||||||
def get_resource_data(filename):
|
def generateGraph(filename, framework_name, endpoint_name):
|
||||||
lines = []
|
x, y = getData(filename)
|
||||||
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}', 'Número de requisições', 'Requisições por segundo', new_filename)
|
plot_graph(x, y, f'{framework_name} - {endpoint_name}', 'Number of requests', 'Requests per second', new_filename)
|
||||||
|
|
||||||
def generate_resource_graph(filename, framework_name, endpoint_name):
|
if __name__ == '__main__':
|
||||||
x, y = get_resource_data(filename)
|
generateGraph('data.txt', 'Teste', 'endpoint')
|
||||||
|
|
||||||
new_filename = filename.replace('.txt', '').replace('.csv', '')
|
|
||||||
plot_resource_graph(x, y, f'{framework_name} - {endpoint_name}', 'Uso de recursos', 'Uso (%)', new_filename)
|
|
||||||
|
|
|
@ -1,27 +1,22 @@
|
||||||
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 generate_req_graph, generate_resource_graph
|
from graph import generateGraph
|
||||||
from math import floor
|
from math import floor
|
||||||
|
|
||||||
if len(sys.argv) != 3 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
|
if len(sys.argv) != 2 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
|
||||||
print("Usage: python testes.py <framework name> <container name>")
|
print("Usage: python testes.py <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://localhost:9080'
|
|
||||||
API_REQUESTS = [
|
API_REQUESTS = [
|
||||||
('/status/ok', range(0, 30_000, 5000)),
|
('/status/ok', range(0, 50_000, 5000)),
|
||||||
('/image/load-image', range(0, 30_000, 5000)),
|
('/image/load-image', range(0, 50_000, 5000)),
|
||||||
('/static/simpleimage.png', range(0, 30_000, 5000)),
|
('/image/load-big-image', range(0, 1_000, 10)),
|
||||||
('/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):
|
||||||
|
@ -40,27 +35,16 @@ def send_request(url):
|
||||||
responses[floor(response.status_code/100)] += 1
|
responses[floor(response.status_code/100)] += 1
|
||||||
return responses
|
return responses
|
||||||
|
|
||||||
def getFileNames(endpoint):
|
def getFileName(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):
|
||||||
files = getFileNames(endpoint)
|
filename = getFileName(endpoint)
|
||||||
for filename in files:
|
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
|
|
||||||
|
@ -70,7 +54,6 @@ 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}'
|
||||||
|
@ -80,13 +63,7 @@ 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)
|
||||||
|
|
||||||
|
@ -99,40 +76,11 @@ 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(files[0], num_request, f"{num_request/elapsed_time:.2f}")
|
record(filename, num_request, f"{num_request/elapsed_time:.2f}")
|
||||||
record_resource(files[1], num_request, cpu, ram)
|
generateGraph(filename, FRAMEWORK_NAME, endpoint)
|
||||||
|
|
||||||
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