Compare commits

..

31 Commits

Author SHA1 Message Date
3533491df1 Merge branch 'test' of github.com:ivanch/tcc into test 2023-10-03 15:30:15 -03:00
6f2d9c1f78 melhorias nos gráficos 2023-10-03 15:29:40 -03:00
6d8ec28dbb save image 2023-09-28 22:53:09 -03:00
4535ed5cf6 fix actix docker 2023-09-28 22:35:06 -03:00
4fa2ae956f curl 2023-09-28 21:03:32 -03:00
2e3398f683 average runs 2023-09-28 21:00:55 -03:00
2f91c3636f add actix 2023-09-28 20:40:45 -03:00
aa4b1e9709 add actix 2023-09-28 17:32:11 -03:00
b372e1c310 blur nativo 2023-09-27 22:28:52 -03:00
4ef0ab508f otimizando flask 2023-09-26 16:02:46 -03:00
9082b34aab ajuste 2023-09-25 20:52:02 -03:00
a9b48be817 ma 2023-09-25 20:07:14 -03:00
f2a9faed92 add express 2023-09-25 19:58:07 -03:00
095f04e113 add print 2023-09-24 12:57:37 -03:00
94c2296738 melhorando testes 2023-09-24 12:56:02 -03:00
2dbdbea018 novos graficos testes 2023-09-24 12:47:17 -03:00
f0fa227035 ajustando ASP com Flask 2023-09-24 10:56:01 -03:00
125b456743 consertos flask 2023-09-21 17:57:47 -03:00
1f63985364 add docker-compose 2023-09-18 23:44:09 -03:00
4ea131299a alguns consertos 2023-09-18 12:24:05 -03:00
35aaf31ba7 Merge branch 'FlaskAPI' of github.com:ivanch/tcc 2023-09-17 22:54:57 -03:00
dede-
8d9c1598a2 Add load image, Dockerfile e requirements 2023-09-17 22:37:18 -03:00
dede-
84df834b82 Add load image, Dockerfile e requirements 2023-09-17 21:55:40 -03:00
05dad7ade5 consertando url 2023-09-16 12:02:26 -03:00
cd1e9da710 testando rust 2023-09-12 21:39:28 -03:00
1dbd8c238f adicionando testes 2023-09-10 23:14:21 -03:00
dede-
74a1a8458e Só ta funfando o status e o blur por enqunato 2023-08-31 15:15:50 -03:00
6f93461ed9 mudando Dockerfile pra debian 2023-08-24 14:25:54 -03:00
09c7345e68 small fix 2023-08-23 11:16:36 -03:00
258f3a0243 add script de teste 2023-08-23 11:01:18 -03:00
6f6a77f897 adicionando pasta para ASP.NET 2023-08-23 11:01:09 -03:00
42 changed files with 2385 additions and 54 deletions

12
.gitignore vendored
View File

@@ -1,4 +1,14 @@
.vs
__pycache__
bin
obj
*.png
*.vscode
__pycache__
.idea
*/target
*.png
*.csv
*.jpg
*.mp4

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/tcc.iml" filepath="$PROJECT_DIR$/.idea/tcc.iml" />
</modules>
</component>
</project>

8
.idea/tcc.iml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@@ -23,6 +23,7 @@ namespace TCC.Controllers
mstream.Position = 0;
var result = ImageService.BoxBlurImage(mstream, radius);
mstream.Close();
var blurredImageStream = new MemoryStream();
result.Write(blurredImageStream);
@@ -31,28 +32,16 @@ namespace TCC.Controllers
return File(blurredImageStream, "image/png");
}
[HttpGet("load-image")]
[HttpGet("load-small-image")]
public async Task<IActionResult> GetSimpleImage()
{
var result = ImageService.GetSimpleImage();
var imageStream = new MemoryStream();
result.Write(imageStream);
imageStream.Position = 0;
return File(imageStream, "image/png");
return File(ImageService.GetSimpleImage(), "image/png");
}
[HttpGet("load-big-image")]
public async Task<IActionResult> GetBigImage()
{
var result = ImageService.GetBigImage();
var imageStream = new MemoryStream();
result.Write(imageStream);
imageStream.Position = 0;
return File(imageStream, "image/png");
return File(ImageService.GetBigImage(), "image/png");
}
[HttpPost("save-big-image")]
@@ -62,6 +51,9 @@ namespace TCC.Controllers
await HttpContext.Request.Body.CopyToAsync(mstream);
mstream.Position = 0;
ImageService.SaveImage(mstream);
mstream.Close();
return Ok();
}
}

View File

@@ -3,7 +3,7 @@
namespace TCC.Controllers
{
[ApiController]
[Route("")]
[Route("status")]
public class StatusController : ControllerBase
{
public StatusController() { }

31
ASP.NET/Dockerfile Normal file
View File

@@ -0,0 +1,31 @@
FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim AS build-env
WORKDIR /App
# Copy everything
RUN apt update && apt install wget -y
COPY * .
# Restore as distinct layers
RUN dotnet restore
# Build a release
RUN dotnet build -c Release -o out
RUN cd out && \
wget https://files.ivanch.me/api/public/dl/iFuXSNhw/small-image.png && \
wget https://files.ivanch.me/api/public/dl/81Bkht5C/big-image.png && \
wget https://files.ivanch.me/api/public/dl/nAndfAjK/video.mp4 && \
rm -rf runtimes && \
mkdir -p ./static && \
mv small-image.png ./static && \
mv big-image.png ./static && \
mv video.mp4 ./static
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0-bullseye-slim
WORKDIR /App
COPY --from=build-env /App/out .
ENTRYPOINT ["dotnet", "/App/TCC.APP.dll"]

View File

@@ -1,5 +1,5 @@
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.FileProviders;
using TCC.Services;
namespace TCC
@@ -19,9 +19,13 @@ namespace TCC
options.Limits.MaxRequestBodySize = int.MaxValue; // if don't set default value is: 30 MB
});
var app = builder.Build();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "static")),
RequestPath = "/static"
});
app.MapControllers();
app.Run();

View File

@@ -14,7 +14,7 @@
"dotnetRunMessages": true,
"launchBrowser": false,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5100",
"applicationUrl": "http://0.0.0.0:9090",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

View File

@@ -1,5 +1,4 @@
using ImageMagick;
using tcc_app;
namespace TCC.Services
{
@@ -10,11 +9,13 @@ namespace TCC.Services
public MagickImage BoxBlurImage(Stream imageStream, int radius)
{
var image = new MagickImage(imageStream);
var blurredImage = new MagickImage(image);
image.GaussianBlur(radius, radius);
return image;
//var blurredImage = new MagickImage(image);
blurredImage = BoxBlurImageSeparable(image, blurredImage, radius, 0);
blurredImage = BoxBlurImageSeparable(image, blurredImage, 0, radius);
return blurredImage;
//blurredImage = BoxBlurImageSeparable(image, blurredImage, radius, 0);
//blurredImage = BoxBlurImageSeparable(blurredImage, blurredImage, 0, radius);
//return blurredImage;
}
private MagickImage BoxBlurImageSeparable(MagickImage image, MagickImage blurredImage, int radiusX, int radiusY)
@@ -62,14 +63,14 @@ namespace TCC.Services
file.Close();
}
public MagickImage GetSimpleImage()
public byte[] GetSimpleImage()
{
return ImageHelper.SimpleImage;
return File.ReadAllBytes("static/small-image.png");
}
public MagickImage GetBigImage()
public byte[] GetBigImage()
{
return ImageHelper.BigImage;
return File.ReadAllBytes("static/big-image.png");
}
}
}

View File

@@ -7,6 +7,7 @@
<RootNamespace>tcc_app</RootNamespace>
<PlatformTarget>AnyCPU</PlatformTarget>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<MagickCopyNativeLinux>true</MagickCopyNativeLinux>
</PropertyGroup>
<ItemGroup>

9
ASP.NET/appsettings.json Normal file
View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft.AspNetCore": "Trace"
}
},
"AllowedHosts": "*"
}

33
ASP.NET/static/nginx.html Normal file
View File

@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html {
color-scheme: light dark;
}
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
</p>
<p>
For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br />
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.
</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

2
ActixAPI/.dockerignore Normal file
View File

@@ -0,0 +1,2 @@
Dockerfile
target/

1492
ActixAPI/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
ActixAPI/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "ActixAPI"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4"
actix-files = "0.6.2"
magick_rust = "0.19.1"
qstring = "0.7.2"

34
ActixAPI/Dockerfile Normal file
View File

@@ -0,0 +1,34 @@
FROM rust:latest
ENV MAGICK_VERSION 7.1
RUN apt-get update \
&& apt-get -y install wget curl build-essential clang pkg-config libjpeg-turbo-progs libpng-dev
RUN apt update && apt install curl -y \
&& curl https://imagemagick.org/archive/ImageMagick.tar.gz | tar xz \
&& cd ImageMagick-${MAGICK_VERSION}* \
&& ./configure --with-magick-plus-plus=no --with-perl=no \
&& make \
&& make install \
&& cd .. \
&& rm -r ImageMagick-${MAGICK_VERSION}*
WORKDIR /app
RUN wget https://files.ivanch.me/api/public/dl/iFuXSNhw/small-image.png && \
wget https://files.ivanch.me/api/public/dl/81Bkht5C/big-image.png && \
wget https://files.ivanch.me/api/public/dl/nAndfAjK/video.mp4
COPY . .
RUN cargo build --release && \
cp ./target/release/ActixAPI . && \
mv small-image.png ./static && \
mv big-image.png ./static && \
mv video.mp4 ./static && \
ldconfig /usr/local/lib
ENV LD_LIBRARY_PATH=/usr/local/lib
ENTRYPOINT ["./ActixAPI"]

82
ActixAPI/src/main.rs Normal file
View File

@@ -0,0 +1,82 @@
use qstring::QString;
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, HttpRequest, Result};
use actix_files::NamedFile;
use magick_rust::MagickWand;
#[get("/status/ok")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("{\"status\": 200}")
}
async fn static_serve(req: HttpRequest) -> Result<NamedFile> {
let path: &str = req.path();
let real_path = &path[1..];
Ok(NamedFile::open(real_path)?)
}
#[get("/image/load-small-image")]
async fn load_small_image() -> Result<NamedFile> {
let real_path = "static/small-image.png";
Ok(NamedFile::open(real_path)?)
}
#[get("/image/load-big-image")]
async fn load_big_image() -> Result<NamedFile> {
let real_path = "static/big-image.png";
Ok(NamedFile::open(real_path)?)
}
#[post("/image/save-big-image")]
async fn save_big_image(image_data: web::Bytes) -> Result<HttpResponse> {
// Write bytes to file
std::fs::write("image.png", &image_data).unwrap();
// Return the blurred image bytes
Ok(HttpResponse::Ok()
.content_type("application/json")
.body("{\"status\": 200}"))
}
#[post("/image/blur")]
async fn blur_image(image_data: web::Bytes, req: HttpRequest) -> Result<HttpResponse> {
// Load the image from the incoming bytes
let mut wand = MagickWand::new();
wand.read_image_blob(&image_data).unwrap();
let query_str = req.query_string();
let qs = QString::from(query_str);
let radius = qs.get("radius").unwrap_or("5").parse::<f64>().unwrap_or(5.0);
// Blur the image
wand.blur_image(radius, radius).unwrap();
// Convert the image back to bytes
let blurred_image_bytes = wand.write_image_blob("png").unwrap();
// Return the blurred image bytes
Ok(HttpResponse::Ok()
.content_type("image/png")
.body(blurred_image_bytes))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
println!("Hello, world!");
HttpServer::new(|| {
App::new()
.route("/static/{filename:.*}", web::get().to(static_serve))
.service(hello)
.service(load_small_image)
.service(load_big_image)
.service(save_big_image)
.service(blur_image)
.app_data(web::PayloadConfig::new(1024 * 1024 * 1024))
})
.bind(("0.0.0.0", 5000))?
.run()
.await
}

View File

@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html {
color-scheme: light dark;
}
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
</p>
<p>
For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br />
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.
</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

34
FlaskAPI/.dockerignore Normal file
View File

@@ -0,0 +1,34 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/engine/reference/builder/#dockerignore-file
**/.DS_Store
**/__pycache__
**/.venv
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

29
FlaskAPI/Dockerfile Normal file
View File

@@ -0,0 +1,29 @@
ARG PYTHON_VERSION=3.10.12
FROM python:${PYTHON_VERSION}-slim as base
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /app
RUN apt-get update && apt-get install -y imagemagick && apt-get install -y wget && ls
RUN wget https://files.ivanch.me/api/public/dl/iFuXSNhw/small-image.png && \
wget https://files.ivanch.me/api/public/dl/81Bkht5C/big-image.png && \
wget https://files.ivanch.me/api/public/dl/nAndfAjK/video.mp4 && \
rm -rf runtimes && \
mkdir -p ./static && \
mv small-image.png ./static && \
mv big-image.png ./static && \
mv video.mp4 ./static
COPY . .
RUN --mount=type=cache,target=/root/.cache/pip \
--mount=type=bind,source=requirements.txt,target=requirements.txt \
python -m pip install -r requirements.txt
EXPOSE 5000
# Run the application.
CMD gunicorn 'app:app' --bind=0.0.0.0:5000 --timeout 3600

12
FlaskAPI/app.py Normal file
View File

@@ -0,0 +1,12 @@
from flask import Flask
from controllers.status import status_blueprint
from controllers.image import image_blueprint
app = Flask(__name__)
#
app.register_blueprint(status_blueprint)
app.register_blueprint(image_blueprint)
if __name__ == '__main__':
app.run()

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@@ -0,0 +1,42 @@
import io
from services.image import ImageService
from flask import request, Response, Blueprint, jsonify, send_file
image_blueprint = Blueprint('image_blueprint', __name__)
image_service = ImageService()
@image_blueprint.route('/image/blur', methods=['POST'])
def blur_image():
radius = int(request.args.get('radius'))
image = request.get_data()
if radius and image:
return send_file(io.BytesIO(image_service.box_blur_image(image, radius)),
mimetype='image/png',
as_attachment=True,
download_name='blurred_image.png')
return "Bad request", 400
@image_blueprint.route('/image/load-small-image', methods=['GET'])
def get_simple_image():
return send_file(io.BytesIO(image_service.get_simple_image()),
mimetype='image/png',
as_attachment=True,
download_name='small-image.png')
@image_blueprint.route('/image/load-big-image', methods=['GET'])
def get_big_image():
return send_file(io.BytesIO(image_service.get_big_image()),
mimetype='image/png',
as_attachment=True,
download_name='big-image.png')
@image_blueprint.route('/image/save-big-image', methods=['POST'])
def save_image():
image_service.save_image(request.get_data())
return "OK", 200

View File

@@ -0,0 +1,19 @@
from flask import jsonify, Blueprint
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'])
def return_ok():
return jsonify(status=200)

View File

@@ -0,0 +1,3 @@
Flask>=1.0
gunicorn>=19.9.0
Wand

View File

@@ -0,0 +1,60 @@
from wand.image import Image, Color
def box_blur_image_separable(image, radius_x, radius_y):
width, height = image.width, image.height
kernel_x_size = 2 * radius_x + 1
kernel_y_size = 2 * radius_y + 1
kernel_area = kernel_x_size * kernel_y_size
blurred_image = image.clone()
for y in range(height):
for x in range(width):
r_total, g_total, b_total = 0, 0, 0
for offset_y in range(-radius_y, radius_y + 1):
for offset_x in range(-radius_x, radius_x + 1):
new_x = x + offset_x
new_y = y + offset_y
if 0 <= new_x < width and 0 <= new_y < height:
pixel = image[new_x, new_y]
r_total += pixel.red_int8
g_total += pixel.green_int8
b_total += pixel.blue_int8
r_avg = int(r_total / kernel_area)
g_avg = int(g_total / kernel_area)
b_avg = int(b_total / kernel_area)
blurred_image[x, y] = Color(f'rgb({r_avg},{g_avg},{b_avg})')
return blurred_image
class ImageService:
def __init__(self):
pass
def box_blur_image(self, img, radius):
with Image(blob=img) as image:
image.gaussian_blur(radius, radius)
return image.make_blob()
# blurred_image = box_blur_image_separable(image, radius, 0)
# blurred_image = box_blur_image_separable(blurred_image, 0, radius)
# return blurred_image.make_blob()
def get_simple_image(self):
with open("./static/small-image.png", "rb") as file:
return file.read()
def get_big_image(self):
with open("./static/big-image.png", "rb") as file:
return file.read()
def save_image(self, img):
with open("image.png", "wb") as file:
file.write(img)
return True

View File

@@ -1,16 +0,0 @@
using ImageMagick;
namespace tcc_app
{
public static class ImageHelper
{
public static MagickImage SimpleImage;
public static MagickImage BigImage;
static ImageHelper()
{
SimpleImage = new MagickImage("simpleimage.png");
BigImage = new MagickImage("bigimage.png");
}
}
}

View File

@@ -1,9 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

51
docker-compose.yml Normal file
View File

@@ -0,0 +1,51 @@
version: "3"
services:
tcc-aspnet:
image: tcc:aspnet
container_name: tcc-aspnet
build: ./ASP.NET
restart: always
ports:
- "9081:80"
deploy:
resources:
limits:
cpus: '1'
memory: 1GB
tcc-flask:
image: tcc:flask
container_name: tcc-flask
build: ./FlaskAPI
restart: always
ports:
- "9082:5000"
deploy:
resources:
limits:
cpus: '1'
memory: 1GB
tcc-actix:
image: tcc:actix
container_name: tcc-actix
build: ./ActixAPI
restart: always
ports:
- "9083:5000"
deploy:
resources:
limits:
cpus: '1'
memory: 1GB
tcc-express:
image: tcc:express
container_name: tcc-express
build: ./tcc-express
restart: always
ports:
- "9084:5000"
deploy:
resources:
limits:
cpus: '1'
memory: 1GB

27
scripts/common.py Normal file
View File

@@ -0,0 +1,27 @@
FRAMEWORKS = [
('Actix', 'tcc-actix'),
('ASP.NET', 'tcc-aspnet'),
('Flask', 'tcc-flask'),
('Express', 'tcc-express'),
]
ENDPOINTS = {
'Actix': 'http://localhost:9083',
'ASP.NET': 'http://localhost:9081',
'Flask': 'http://localhost:9082',
'Express': 'http://localhost:9084',
}
BLUR_RADIUS = 5
AVG_RUNS = 3
API_REQUESTS = [
('/status/ok', 'GET', range(0, 30_000, 5000), None),
('/image/save-big-image', 'POST', range(0, 10_000, 1_000), open('big-image.png', 'rb').read()),
(f'/image/blur?radius={BLUR_RADIUS}', 'POST', range(0, 1_000, 50), open('small-image.png', 'rb').read()),
('/image/load-small-image', '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),
('/static/big-image.png', 'GET', range(0, 500, 50), None),
('/static/video.mp4', 'GET', range(0, 10_000, 1_000), None),
]

132
scripts/graph.py Normal file
View File

@@ -0,0 +1,132 @@
import numpy as np
import matplotlib.pyplot as plt
from common import API_REQUESTS, FRAMEWORKS
FRAMEWORKS = [f for f, _ in FRAMEWORKS]
def plot_graph(x_data, y_data, title, x_label, y_label, filename):
for i, framework in enumerate(FRAMEWORKS):
plt.plot(x_data, y_data[i], markersize=1, linewidth=1, linestyle='solid', label=framework)
plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.legend()
plt.savefig(f'{filename}.png')
plt.clf()
plt.close('all')
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):
frameworks[framework] = y_data[i]
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.savefig(f'{filename}.png')
plt.clf()
plt.close('all')
def get_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]))
y.append(float(line[1]))
return x, y
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:
r = [round(float(line[1])*100), round(float(line[2])*100)]
if r[0] > 100:
r[0] = 100
if r[1] > 100:
r[1] = 100
x.append(int(line[0])) # requests
y.append(r) # cpu, ram
return x, y
def generate_req_graph(filename, framework_name, endpoint_name):
x, _ = get_data(filename)
y = []
for f in FRAMEWORKS:
newfile = filename.replace(framework_name, f)
_, y_data = get_data(newfile)
y.append(y_data)
graph_file = f'req_{endpoint_name.replace("/", "").replace("?", "")}'
plot_graph(x, y, f'Requisições atendidas por segundo - {endpoint_name}', 'Número de requisições', 'Requisições/segundo', graph_file)
def generate_resource_graph(filename, framework_name, endpoint_name):
x, _ = get_resource_data(filename)
for resource_index, resource in enumerate(['cpu', 'ram']):
y = []
for f in FRAMEWORKS:
newfile = filename.replace(framework_name, f)
_, y_data = get_resource_data(newfile)
y.append([data[resource_index] for data in y_data])
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)
if __name__ == '__main__':
endpoints = [config[0] for config in API_REQUESTS]
for endpoint_name in endpoints:
framework_name = 'ASP.NET'
endpoint_file = endpoint_name.replace('/', '')
filename = f'data/resource_ASP.NET_{endpoint_file}.csv'
generate_resource_graph(filename, framework_name, endpoint_name)
filename = f'data/req_ASP.NET_{endpoint_file}.csv'
generate_req_graph(filename, framework_name, endpoint_name)

17
scripts/init.py Normal file
View File

@@ -0,0 +1,17 @@
import requests
def download_file(url):
local_filename = url.split('/')[-1]
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
# If you have chunk encoded response uncomment if
# and set chunk_size parameter to None.
#if chunk:
f.write(chunk)
return local_filename
def init():
download_file('https://files.ivanch.me/api/public/dl/iFuXSNhw/small-image.png')
download_file('https://files.ivanch.me/api/public/dl/81Bkht5C/big-image.png')

153
scripts/testes.py Normal file
View File

@@ -0,0 +1,153 @@
import requests
import docker
import concurrent.futures
import time
import os
from math import floor
from init import init
from common import FRAMEWORKS, ENDPOINTS, API_REQUESTS, AVG_RUNS
init()
THREADS = 10
FRAMEWORK_NAME = ""
CONTAINER_NAME = ""
URL_BASE = 'http://localhost:9090'
def send_request(url, method = 'GET', payload = None):
success = False
responses = {
2: 0, # OK
4: 0, # Bad Request
5: 0, # Server Error
}
while not success:
try:
response = None
if method == 'GET':
response = requests.get(url)
elif method == 'POST':
response = requests.post(url, data=payload, headers={'Content-Type': 'image/png'})
except:
continue
success = response.status_code == 200
responses[floor(response.status_code/100)] += 1
return responses
def getFileNames(endpoint):
endpoint = endpoint.replace('/', '')
files = [
f"data/req_{FRAMEWORK_NAME}_{endpoint}.csv",
f"data/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, method, num_requests, metadata):
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
total_cpu, total_ram = 0, 0
total_time = 0
for run in range(AVG_RUNS):
ok_responses = 0
bad_responses = 0
server_errors = 0
with concurrent.futures.ThreadPoolExecutor(max_workers=THREADS) as executor:
url = f'{URL_BASE}{endpoint}'
start_time = time.time()
futures = []
#with requests.Session() as session:
# futures = [executor.submit(send_request, session, url) for _ in range(num_request)]
half = floor(num_request/2)
for i in range(num_request):
futures.append(executor.submit(send_request, url, method, metadata))
if i == half:
cpu, ram = get_resource_usage()
total_cpu += float(cpu)
total_ram += float(ram)
concurrent.futures.wait(futures)
elapsed_time = time.time() - start_time
total_time += elapsed_time
for future in futures:
responses = future.result()
ok_responses += responses[2]
bad_responses += responses[4]
server_errors += responses[5]
print(f"[#{run}] {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}]]")
client = docker.from_env()
client.containers.get(CONTAINER_NAME).restart()
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():
if CONTAINER_NAME == "": return 0, 0
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}"
if __name__ == "__main__":
if not os.path.exists("data"):
os.mkdir("data")
for i in range(len(FRAMEWORKS)):
FRAMEWORK_NAME = FRAMEWORKS[i][0]
CONTAINER_NAME = FRAMEWORKS[i][1]
URL_BASE = ENDPOINTS[FRAMEWORK_NAME]
for endpoint, method, num_requests, metadata in API_REQUESTS:
print(f"# {FRAMEWORK_NAME} - {endpoint}")
run_tests(endpoint, method, num_requests, metadata)

1
tcc-express Submodule

Submodule tcc-express added at 504b59278f