Compare commits

...

3 Commits

Author SHA1 Message Date
José Henrique a6a8bd307b small fix heh 2023-10-29 19:03:14 -03:00
José Henrique 62b8b70689 adding boxplot 2023-10-29 19:01:24 -03:00
José Henrique 77aa085b0c add json route 2023-10-29 19:01:13 -03:00
10 changed files with 154 additions and 76 deletions

View File

@ -22,5 +22,11 @@ namespace TCC.Controllers
return Ok(sum); return Ok(sum);
} }
[HttpGet("json")]
public async Task<IActionResult> GetJsonResponse()
{
return Ok(new { message = "Hello World!" });
}
} }
} }

View File

@ -11,7 +11,7 @@ namespace TCC.Controllers
[HttpGet("ok")] [HttpGet("ok")]
public async Task<IActionResult> ReturnOk() public async Task<IActionResult> ReturnOk()
{ {
return Ok(new { status = 200 }); return Ok();
} }
} }
} }

62
ActixAPI/Cargo.lock generated
View File

@ -8,8 +8,11 @@ version = "0.1.0"
dependencies = [ dependencies = [
"actix-files", "actix-files",
"actix-web", "actix-web",
"futures",
"magick_rust", "magick_rust",
"qstring", "qstring",
"serde",
"serde_json",
] ]
[[package]] [[package]]
@ -549,12 +552,65 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "futures"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]] [[package]]
name = "futures-core" name = "futures-core"
version = "0.3.28" version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c"
[[package]]
name = "futures-executor"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa"
[[package]]
name = "futures-macro"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.32",
]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.28" version = "0.3.28"
@ -573,10 +629,16 @@ version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533"
dependencies = [ dependencies = [
"futures-channel",
"futures-core", "futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task", "futures-task",
"memchr",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
"slab",
] ]
[[package]] [[package]]

View File

@ -10,3 +10,6 @@ actix-web = "4"
actix-files = "0.6.2" actix-files = "0.6.2"
magick_rust = "0.19.1" magick_rust = "0.19.1"
qstring = "0.7.2" qstring = "0.7.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
futures = "0.3"

View File

@ -5,7 +5,7 @@ use magick_rust::MagickWand;
#[get("/status/ok")] #[get("/status/ok")]
async fn hello() -> impl Responder { async fn hello() -> impl Responder {
HttpResponse::Ok().body("{\"status\": 200}") HttpResponse::Ok()
} }
async fn static_serve(req: HttpRequest) -> Result<NamedFile> { async fn static_serve(req: HttpRequest) -> Result<NamedFile> {
@ -29,6 +29,17 @@ async fn simulation_harmonic_progression(req: HttpRequest) -> impl Responder {
HttpResponse::Ok().body(format!("{}", sum)) HttpResponse::Ok().body(format!("{}", sum))
} }
#[get("/simulation/json")]
async fn simulation_json() -> impl Responder {
// body = {"message": "Hello World"}
let body = serde_json::json!({
"message": "Hello World!"
});
HttpResponse::Ok()
.json(body)
}
#[get("/image/load-small-image")] #[get("/image/load-small-image")]
async fn load_small_image() -> Result<NamedFile> { async fn load_small_image() -> Result<NamedFile> {
let real_path = "static/small-image.png"; let real_path = "static/small-image.png";
@ -89,6 +100,7 @@ async fn main() -> std::io::Result<()> {
.service(save_big_image) .service(save_big_image)
.service(blur_image) .service(blur_image)
.service(simulation_harmonic_progression) .service(simulation_harmonic_progression)
.service(simulation_json)
.app_data(web::PayloadConfig::new(1024 * 1024 * 1024)) .app_data(web::PayloadConfig::new(1024 * 1024 * 1024))
}) })
.bind(("0.0.0.0", 5000))? .bind(("0.0.0.0", 5000))?

View File

@ -1,4 +1,4 @@
from flask import request, Blueprint from flask import request, Blueprint, jsonify
simulation_blueprint = Blueprint('simulation_blueprint', __name__) simulation_blueprint = Blueprint('simulation_blueprint', __name__)
@ -13,6 +13,9 @@ class SimulationController:
return sum return sum
def return_helloworld(self):
return jsonify(message="Hello World!")
simulation_controller = SimulationController() simulation_controller = SimulationController()
@simulation_blueprint.route('/simulation/harmonic-progression', methods=['GET']) @simulation_blueprint.route('/simulation/harmonic-progression', methods=['GET'])
@ -21,3 +24,7 @@ def return_ok():
sum = simulation_controller.harmonic_progression(n) sum = simulation_controller.harmonic_progression(n)
return str(sum), 200 return str(sum), 200
@simulation_blueprint.route('/simulation/json', methods=['GET'])
def return_ok():
return simulation_controller.return_helloworld(), 200

View File

@ -2,18 +2,6 @@ from flask import jsonify, Blueprint
status_blueprint = Blueprint('status_blueprint', __name__) status_blueprint = Blueprint('status_blueprint', __name__)
class StatusController:
def __init__(self):
pass
def return_ok(self):
return jsonify(status=200)
status_controller = StatusController()
@status_blueprint.route('/status/ok', methods=['GET']) @status_blueprint.route('/status/ok', methods=['GET'])
def return_ok(): def return_ok():
return jsonify(status=200) return 200

View File

@ -1,8 +1,8 @@
FRAMEWORKS = [ FRAMEWORKS = [
('Actix', 'tcc-actix'), ('Actix', 'tcc-actix', 'orange'),
('ASP.NET', 'tcc-aspnet'), ('ASP.NET', 'tcc-aspnet', 'blue'),
('Flask', 'tcc-flask'), ('Flask', 'tcc-flask', 'green'),
('Express', 'tcc-express'), ('Express', 'tcc-express', 'red'),
] ]
ENDPOINTS = { ENDPOINTS = {
@ -12,18 +12,17 @@ ENDPOINTS = {
'Express': 'http://localhost:9084', 'Express': 'http://localhost:9084',
} }
BLUR_RADIUS = 5 AVG_RUNS = 5
AVG_RUNS = 3
API_REQUESTS = [ API_REQUESTS = [
('/status/ok', 'GET', range(0, 30_000, 5000), None), ('/status/ok', 'GET', range(0, 30_000, 5000), None),
('/simulation/harmonic-progression?n=100000', 'GET', range(0, 30_000, 5000), None), ('/simulation/harmonic-progression?n=100000', 'GET', range(0, 30_000, 5000), None),
('/simulation/json', 'GET', range(0, 30_000, 5000), None),
('/image/save-big-image', 'POST', range(0, 500, 50), open('big-image.png', 'rb').read()), ('/image/save-big-image', 'POST', range(0, 500, 50), open('big-image.png', 'rb').read()),
# (f'/image/blur?radius={BLUR_RADIUS}', 'POST', range(0, 500, 50), open('small-image.png', 'rb').read()),
('/image/load-small-image', 'GET', range(0, 30_000, 5000), None), ('/image/load-small-image', 'GET', range(0, 30_000, 5000), None),
('/static/small-image.png', 'GET', range(0, 30_000, 5000), None), ('/static/small-image.png', 'GET', range(0, 30_000, 5000), None),
('/image/load-big-image', 'GET', range(0, 500, 50), None), ('/image/load-big-image', 'GET', range(0, 500, 50), None),
('/static/big-image.png', 'GET', range(0, 500, 50), None), ('/static/big-image.png', 'GET', range(0, 500, 50), None),
('/static/video.mp4', 'GET', range(0, 10_000, 1_000), None), ('/static/video.mp4', 'GET', range(0, 10_000, 1_000), None),
('/static/nginx.html', 'GET', range(0, 30_000, 5000), None), ('/static/nginx.html', 'GET', range(0, 30_000, 5000), None),
] ]

View File

@ -1,54 +1,63 @@
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from common import API_REQUESTS, FRAMEWORKS from common import API_REQUESTS, FRAMEWORKS
from pylab import plot, show, savefig, xlim, figure, \
ylim, legend, boxplot, setp, axes
FRAMEWORKS = [f for f, _ in FRAMEWORKS] FRAMEWORKS_COLORS = [c for _, _, c in FRAMEWORKS]
FRAMEWORKS = [f for f, _, _ in FRAMEWORKS]
def plot_graph(x_data, y_data, title, x_label, y_label, filename): def setBoxColors(bp):
for i, framework in enumerate(FRAMEWORKS): for i, box in enumerate(bp['boxes']):
plt.plot(x_data, y_data[i], markersize=1, linewidth=1, linestyle='solid', label=framework) box.set(color=FRAMEWORKS_COLORS[i])
def plot_graph(x_data, y_data, title, x_label, y_label, filename, y_lim = None):
old_x_data = x_data
old_y_data = y_data
x_data = []
y_data = []
for i in range(len(old_x_data)):
if old_x_data[i] not in x_data:
x_data.append(old_x_data[i])
y_data.append([])
for f in range(2):
y_data[-1].append([])
for f in range(2):
y_data[-1][f].append(old_y_data[f][i])
fig = figure()
ax = axes()
all_positions = []
for j in range(len(x_data)):
positions = [len(FRAMEWORKS)*j + i + 1 for i in range(len(FRAMEWORKS))]
bp = boxplot(y_data[j], positions = positions, widths = 0.6)
all_positions.append(positions)
setBoxColors(bp)
avg_positions = []
for positions in all_positions:
avg_positions.append(np.average(positions))
ax.set_xticks(avg_positions)
ax.set_xticklabels([str(x) for x in x_data])
plt.title(title) plt.title(title)
plt.xlabel(x_label) plt.xlabel(x_label)
plt.ylabel(y_label) plt.ylabel(y_label)
plt.legend()
plt.savefig(f'{filename}.png')
plt.clf() if y_lim:
plt.close('all') plt.ylim(y_lim)
def plot_resource_graph(x_data, y_data, title, x_label, y_label, filename):
requests = x_data
frameworks = {}
print(y_data)
for i, framework in enumerate(FRAMEWORKS): for i, framework in enumerate(FRAMEWORKS):
frameworks[framework] = y_data[i] h, = plt.plot([], c=FRAMEWORKS_COLORS[i], label=framework)
# h.set_visible(False)
x = np.arange(len(requests))
width = 0.2
multiplier = 0
fig, ax = plt.subplots(layout='constrained', figsize=(7.5, 5))
print(x)
for framework, measurements in frameworks.items():
print(framework, measurements)
for attribute, measurement in frameworks.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*1.5), requests)
ax.legend(loc='upper left', ncols=len(frameworks.items()))
ax.set_ylim(0, 115)
plt.legend()
plt.savefig(f'{filename}.png') plt.savefig(f'{filename}.png')
plt.clf() plt.clf()
@ -116,7 +125,7 @@ def generate_resource_graph(filename, framework_name, endpoint_name):
y.append([data[resource_index] for data in y_data]) y.append([data[resource_index] for data in y_data])
graph_file = f'{resource}_{endpoint_name.replace("/", "").replace("?", "")}' graph_file = f'{resource}_{endpoint_name.replace("/", "").replace("?", "")}'
plot_resource_graph(x, y, f'Uso de {resource.upper()} - {endpoint_name}', 'Número de requisições', f'Uso de {resource.upper()} (%)', graph_file) plot_graph(x, y, f'Uso de {resource.upper()} - {endpoint_name}', 'Número de requisições', f'Uso de {resource.upper()} (%)', graph_file)
if __name__ == '__main__': if __name__ == '__main__':
endpoints = [config[0] for config in API_REQUESTS] endpoints = [config[0] for config in API_REQUESTS]

View File

@ -61,14 +61,13 @@ def run_tests(endpoint, method, num_requests, metadata):
for num_request in num_requests: for num_request in num_requests:
if num_request <= 0: continue if num_request <= 0: continue
total_cpu, total_ram = 0, 0
total_time = 0
for run in range(AVG_RUNS): for run in range(AVG_RUNS):
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}'
@ -84,13 +83,10 @@ def run_tests(endpoint, method, num_requests, metadata):
if i == half: if i == half:
cpu, ram = get_resource_usage() cpu, ram = get_resource_usage()
total_cpu += float(cpu)
total_ram += float(ram)
concurrent.futures.wait(futures) concurrent.futures.wait(futures)
elapsed_time = time.time() - start_time elapsed_time = time.time() - start_time
total_time += elapsed_time
for future in futures: for future in futures:
responses = future.result() responses = future.result()
@ -103,15 +99,11 @@ def run_tests(endpoint, method, num_requests, metadata):
client = docker.from_env() client = docker.from_env()
client.containers.get(CONTAINER_NAME).restart() client.containers.get(CONTAINER_NAME).restart()
record(files[0], num_request, f"{num_request/elapsed_time:.2f}")
record_resource(files[1], num_request, cpu, ram)
time.sleep(3) time.sleep(3)
cpu = total_cpu / AVG_RUNS
ram = total_ram / AVG_RUNS
elapsed_time = total_time / AVG_RUNS
record(files[0], num_request, f"{num_request/elapsed_time:.2f}")
record_resource(files[1], num_request, cpu, ram)
def get_resource_usage(): def get_resource_usage():
if CONTAINER_NAME == "": return 0, 0 if CONTAINER_NAME == "": return 0, 0