mirror of
https://github.com/ivanch/tcc.git
synced 2025-09-15 14:55:20 +00:00
Compare commits
14 Commits
FlaskAPI
...
a6a8bd307b
Author | SHA1 | Date | |
---|---|---|---|
a6a8bd307b | |||
62b8b70689 | |||
77aa085b0c | |||
d5d2ab4956 | |||
2e71c4e8ed | |||
bba35341fb | |||
f0fa227035 | |||
125b456743 | |||
1f63985364 | |||
4ea131299a | |||
35aaf31ba7 | |||
05dad7ade5 | |||
cd1e9da710 | |||
1dbd8c238f |
10
.gitignore
vendored
10
.gitignore
vendored
@@ -1,4 +1,14 @@
|
|||||||
.vs
|
.vs
|
||||||
|
__pycache__
|
||||||
bin
|
bin
|
||||||
obj
|
obj
|
||||||
|
*.vscode
|
||||||
|
__pycache__
|
||||||
|
.idea
|
||||||
|
|
||||||
|
*/target
|
||||||
|
|
||||||
*.png
|
*.png
|
||||||
|
*.csv
|
||||||
|
*.jpg
|
||||||
|
*.mp4
|
@@ -23,6 +23,7 @@ namespace TCC.Controllers
|
|||||||
mstream.Position = 0;
|
mstream.Position = 0;
|
||||||
|
|
||||||
var result = ImageService.BoxBlurImage(mstream, radius);
|
var result = ImageService.BoxBlurImage(mstream, radius);
|
||||||
|
mstream.Close();
|
||||||
|
|
||||||
var blurredImageStream = new MemoryStream();
|
var blurredImageStream = new MemoryStream();
|
||||||
result.Write(blurredImageStream);
|
result.Write(blurredImageStream);
|
||||||
@@ -31,28 +32,16 @@ namespace TCC.Controllers
|
|||||||
return File(blurredImageStream, "image/png");
|
return File(blurredImageStream, "image/png");
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("load-image")]
|
[HttpGet("load-small-image")]
|
||||||
public async Task<IActionResult> GetSimpleImage()
|
public async Task<IActionResult> GetSimpleImage()
|
||||||
{
|
{
|
||||||
var result = ImageService.GetSimpleImage();
|
return File(ImageService.GetSimpleImage(), "image/png");
|
||||||
|
|
||||||
var imageStream = new MemoryStream();
|
|
||||||
result.Write(imageStream);
|
|
||||||
imageStream.Position = 0;
|
|
||||||
|
|
||||||
return File(imageStream, "image/png");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("load-big-image")]
|
[HttpGet("load-big-image")]
|
||||||
public async Task<IActionResult> GetBigImage()
|
public async Task<IActionResult> GetBigImage()
|
||||||
{
|
{
|
||||||
var result = ImageService.GetBigImage();
|
return File(ImageService.GetBigImage(), "image/png");
|
||||||
|
|
||||||
var imageStream = new MemoryStream();
|
|
||||||
result.Write(imageStream);
|
|
||||||
imageStream.Position = 0;
|
|
||||||
|
|
||||||
return File(imageStream, "image/png");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("save-big-image")]
|
[HttpPost("save-big-image")]
|
||||||
@@ -62,6 +51,9 @@ namespace TCC.Controllers
|
|||||||
await HttpContext.Request.Body.CopyToAsync(mstream);
|
await HttpContext.Request.Body.CopyToAsync(mstream);
|
||||||
mstream.Position = 0;
|
mstream.Position = 0;
|
||||||
|
|
||||||
|
ImageService.SaveImage(mstream);
|
||||||
|
mstream.Close();
|
||||||
|
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
32
ASP.NET/Controllers/SimulationController.cs
Normal file
32
ASP.NET/Controllers/SimulationController.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using TCC.Services;
|
||||||
|
|
||||||
|
namespace TCC.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("simulation")]
|
||||||
|
public class SimulationController : ControllerBase
|
||||||
|
{
|
||||||
|
public SimulationController()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("harmonic-progression")]
|
||||||
|
public async Task<IActionResult> GetHarmonicProgression([FromQuery] int n)
|
||||||
|
{
|
||||||
|
float sum = 0;
|
||||||
|
for (int i = 1; i <= n; i++)
|
||||||
|
{
|
||||||
|
sum += 1.0f / i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("json")]
|
||||||
|
public async Task<IActionResult> GetJsonResponse()
|
||||||
|
{
|
||||||
|
return Ok(new { message = "Hello World!" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,7 @@ FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim AS build-env
|
|||||||
WORKDIR /App
|
WORKDIR /App
|
||||||
|
|
||||||
# Copy everything
|
# Copy everything
|
||||||
|
RUN apt update && apt install wget -y
|
||||||
COPY * .
|
COPY * .
|
||||||
|
|
||||||
# Restore as distinct layers
|
# Restore as distinct layers
|
||||||
@@ -10,15 +11,23 @@ RUN dotnet restore
|
|||||||
# Build a release
|
# Build a release
|
||||||
RUN dotnet build -c Release -o out
|
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 && \
|
||||||
|
wget https://files.ivanch.me/api/public/dl/RzXwJG7o/nginx.html && \
|
||||||
|
rm -rf runtimes && \
|
||||||
|
mkdir -p ./static && \
|
||||||
|
mv small-image.png ./static && \
|
||||||
|
mv big-image.png ./static && \
|
||||||
|
mv video.mp4 ./static && \
|
||||||
|
mv nginx.html ./static
|
||||||
|
|
||||||
# Build runtime image
|
# Build runtime image
|
||||||
FROM mcr.microsoft.com/dotnet/aspnet:6.0-bullseye-slim
|
FROM mcr.microsoft.com/dotnet/aspnet:6.0-bullseye-slim
|
||||||
|
|
||||||
WORKDIR /App
|
WORKDIR /App
|
||||||
|
|
||||||
RUN wget https://files.ivanch.me/api/public/dl/QFCLgtrG/simpleimage.png && \
|
|
||||||
wget https://files.ivanch.me/api/public/dl/E0VLgWbx/bigimage.png && \
|
|
||||||
rm -rf runtimes
|
|
||||||
|
|
||||||
COPY --from=build-env /App/out .
|
COPY --from=build-env /App/out .
|
||||||
|
|
||||||
ENTRYPOINT ["dotnet", "/App/TCC.APP.dll"]
|
ENTRYPOINT ["dotnet", "/App/TCC.APP.dll"]
|
@@ -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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,4 +1,5 @@
|
|||||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
using TCC.Services;
|
using TCC.Services;
|
||||||
|
|
||||||
namespace TCC
|
namespace TCC
|
||||||
@@ -18,9 +19,13 @@ namespace TCC
|
|||||||
options.Limits.MaxRequestBodySize = int.MaxValue; // if don't set default value is: 30 MB
|
options.Limits.MaxRequestBodySize = int.MaxValue; // if don't set default value is: 30 MB
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
app.UseStaticFiles(new StaticFileOptions
|
||||||
|
{
|
||||||
|
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "static")),
|
||||||
|
RequestPath = "/static"
|
||||||
|
});
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
@@ -14,7 +14,7 @@
|
|||||||
"dotnetRunMessages": true,
|
"dotnetRunMessages": true,
|
||||||
"launchBrowser": false,
|
"launchBrowser": false,
|
||||||
"launchUrl": "weatherforecast",
|
"launchUrl": "weatherforecast",
|
||||||
"applicationUrl": "http://0.0.0.0:5100",
|
"applicationUrl": "http://0.0.0.0:9090",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
using ImageMagick;
|
using ImageMagick;
|
||||||
using tcc_app;
|
|
||||||
|
|
||||||
namespace TCC.Services
|
namespace TCC.Services
|
||||||
{
|
{
|
||||||
@@ -10,11 +9,13 @@ namespace TCC.Services
|
|||||||
public MagickImage BoxBlurImage(Stream imageStream, int radius)
|
public MagickImage BoxBlurImage(Stream imageStream, int radius)
|
||||||
{
|
{
|
||||||
var image = new MagickImage(imageStream);
|
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, radius, 0);
|
||||||
blurredImage = BoxBlurImageSeparable(image, blurredImage, 0, radius);
|
//blurredImage = BoxBlurImageSeparable(blurredImage, blurredImage, 0, radius);
|
||||||
return blurredImage;
|
//return blurredImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
private MagickImage BoxBlurImageSeparable(MagickImage image, MagickImage blurredImage, int radiusX, int radiusY)
|
private MagickImage BoxBlurImageSeparable(MagickImage image, MagickImage blurredImage, int radiusX, int radiusY)
|
||||||
@@ -62,14 +63,14 @@ namespace TCC.Services
|
|||||||
file.Close();
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
33
ASP.NET/static/nginx.html
Normal file
33
ASP.NET/static/nginx.html
Normal 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
2
ActixAPI/.dockerignore
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
Dockerfile
|
||||||
|
target/
|
1554
ActixAPI/Cargo.lock
generated
Normal file
1554
ActixAPI/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
ActixAPI/Cargo.toml
Normal file
15
ActixAPI/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[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"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1"
|
||||||
|
futures = "0.3"
|
36
ActixAPI/Dockerfile
Normal file
36
ActixAPI/Dockerfile
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
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 && \
|
||||||
|
wget https://files.ivanch.me/api/public/dl/RzXwJG7o/nginx.html
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN cargo build --release && \
|
||||||
|
cp ./target/release/ActixAPI . && \
|
||||||
|
mv small-image.png ./static && \
|
||||||
|
mv big-image.png ./static && \
|
||||||
|
mv video.mp4 ./static && \
|
||||||
|
mv nginx.html ./static && \
|
||||||
|
ldconfig /usr/local/lib
|
||||||
|
|
||||||
|
ENV LD_LIBRARY_PATH=/usr/local/lib
|
||||||
|
|
||||||
|
ENTRYPOINT ["./ActixAPI"]
|
109
ActixAPI/src/main.rs
Normal file
109
ActixAPI/src/main.rs
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn static_serve(req: HttpRequest) -> Result<NamedFile> {
|
||||||
|
let path: &str = req.path();
|
||||||
|
let real_path = &path[1..];
|
||||||
|
|
||||||
|
Ok(NamedFile::open(real_path)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/simulation/harmonic-progression")]
|
||||||
|
async fn simulation_harmonic_progression(req: HttpRequest) -> impl Responder {
|
||||||
|
let query_str = req.query_string();
|
||||||
|
let qs = QString::from(query_str);
|
||||||
|
let radius = qs.get("n").unwrap_or("1").parse::<f64>().unwrap_or(1.0);
|
||||||
|
|
||||||
|
let mut sum = 0.0;
|
||||||
|
for i in 1..=radius as i32 {
|
||||||
|
sum += 1.0 / i as f64;
|
||||||
|
}
|
||||||
|
|
||||||
|
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")]
|
||||||
|
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)
|
||||||
|
.service(simulation_harmonic_progression)
|
||||||
|
.service(simulation_json)
|
||||||
|
.app_data(web::PayloadConfig::new(1024 * 1024 * 1024))
|
||||||
|
})
|
||||||
|
.bind(("0.0.0.0", 5000))?
|
||||||
|
.run()
|
||||||
|
.await
|
||||||
|
}
|
33
ActixAPI/static/nginx.html
Normal file
33
ActixAPI/static/nginx.html
Normal 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>
|
8
FlaskAPI/.idea/.gitignore
generated
vendored
8
FlaskAPI/.idea/.gitignore
generated
vendored
@@ -1,8 +0,0 @@
|
|||||||
# Default ignored files
|
|
||||||
/shelf/
|
|
||||||
/workspace.xml
|
|
||||||
# Editor-based HTTP Client requests
|
|
||||||
/httpRequests/
|
|
||||||
# Datasource local storage ignored files
|
|
||||||
/dataSources/
|
|
||||||
/dataSources.local.xml
|
|
19
FlaskAPI/.idea/FlaskAPI.iml
generated
19
FlaskAPI/.idea/FlaskAPI.iml
generated
@@ -1,19 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<module type="PYTHON_MODULE" version="4">
|
|
||||||
<component name="Flask">
|
|
||||||
<option name="enabled" value="true" />
|
|
||||||
</component>
|
|
||||||
<component name="NewModuleRootManager">
|
|
||||||
<content url="file://$MODULE_DIR$" />
|
|
||||||
<orderEntry type="inheritedJdk" />
|
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
|
||||||
</component>
|
|
||||||
<component name="TemplatesService">
|
|
||||||
<option name="TEMPLATE_CONFIGURATION" value="Jinja2" />
|
|
||||||
<option name="TEMPLATE_FOLDERS">
|
|
||||||
<list>
|
|
||||||
<option value="$MODULE_DIR$/../FlaskAPI\templates" />
|
|
||||||
</list>
|
|
||||||
</option>
|
|
||||||
</component>
|
|
||||||
</module>
|
|
@@ -1,6 +0,0 @@
|
|||||||
<component name="InspectionProjectProfileManager">
|
|
||||||
<settings>
|
|
||||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
|
||||||
<version value="1.0" />
|
|
||||||
</settings>
|
|
||||||
</component>
|
|
4
FlaskAPI/.idea/misc.xml
generated
4
FlaskAPI/.idea/misc.xml
generated
@@ -1,4 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (FlaskAPI)" project-jdk-type="Python SDK" />
|
|
||||||
</project>
|
|
8
FlaskAPI/.idea/modules.xml
generated
8
FlaskAPI/.idea/modules.xml
generated
@@ -1,8 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="ProjectModuleManager">
|
|
||||||
<modules>
|
|
||||||
<module fileurl="file://$PROJECT_DIR$/.idea/FlaskAPI.iml" filepath="$PROJECT_DIR$/.idea/FlaskAPI.iml" />
|
|
||||||
</modules>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
17
FlaskAPI/.vscode/launch.json
vendored
17
FlaskAPI/.vscode/launch.json
vendored
@@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"version": "0.0.1",
|
|
||||||
"configurations": [
|
|
||||||
|
|
||||||
{
|
|
||||||
"name": "Python: Flask",
|
|
||||||
"type": "python",
|
|
||||||
"request": "launch",
|
|
||||||
"module": "flask",
|
|
||||||
"env": { "FLASK_APP": "app.py", "FLASK_DEBUG": "1" },
|
|
||||||
"args": ["run", "--no-debugger", "--no-reload"],
|
|
||||||
"jinja": true,
|
|
||||||
"justMyCode": true
|
|
||||||
}
|
|
||||||
|
|
||||||
]
|
|
||||||
}
|
|
0
FlaskAPI/.vscode/settings.json
vendored
0
FlaskAPI/.vscode/settings.json
vendored
@@ -1,45 +1,31 @@
|
|||||||
# syntax=docker/dockerfile:1
|
|
||||||
|
|
||||||
# Comments are provided throughout this file to help you get started.
|
|
||||||
# If you need more help, visit the Dockerfile reference guide at
|
|
||||||
# https://docs.docker.com/engine/reference/builder/
|
|
||||||
|
|
||||||
ARG PYTHON_VERSION=3.10.12
|
ARG PYTHON_VERSION=3.10.12
|
||||||
FROM python:${PYTHON_VERSION}-slim as base
|
FROM python:${PYTHON_VERSION}-slim as base
|
||||||
|
|
||||||
# Prevents Python from writing pyc files.
|
|
||||||
ENV PYTHONDONTWRITEBYTECODE=1
|
ENV PYTHONDONTWRITEBYTECODE=1
|
||||||
|
|
||||||
# Keeps Python from buffering stdout and stderr to avoid situations where
|
|
||||||
# the application crashes without emitting any logs due to buffering.
|
|
||||||
ENV PYTHONUNBUFFERED=1
|
ENV PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Copy the source code into the container.
|
|
||||||
COPY . .
|
|
||||||
|
|
||||||
RUN apt-get update && apt-get install -y imagemagick && apt-get install -y wget && ls
|
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 && \
|
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/81Bkht5C/big-image.png && \
|
||||||
wget https://files.ivanch.me/api/public/dl/nAndfAjK/video.mp4 && \
|
wget https://files.ivanch.me/api/public/dl/nAndfAjK/video.mp4 && \
|
||||||
|
wget https://files.ivanch.me/api/public/dl/RzXwJG7o/nginx.html && \
|
||||||
rm -rf runtimes && \
|
rm -rf runtimes && \
|
||||||
mkdir -p ./static && \
|
mkdir -p ./static && \
|
||||||
mv small-image.png ./static && \
|
mv small-image.png ./static && \
|
||||||
mv big-image.png ./static && \
|
mv big-image.png ./static && \
|
||||||
mv video.mp4 ./static
|
mv video.mp4 ./static && \
|
||||||
|
mv nginx.html ./static
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
# Download dependencies as a separate step to take advantage of Docker's caching.
|
|
||||||
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
|
|
||||||
# Leverage a bind mount to requirements.txt to avoid having to copy them into
|
|
||||||
# into this layer.
|
|
||||||
RUN --mount=type=cache,target=/root/.cache/pip \
|
RUN --mount=type=cache,target=/root/.cache/pip \
|
||||||
--mount=type=bind,source=requirements.txt,target=requirements.txt \
|
--mount=type=bind,source=requirements.txt,target=requirements.txt \
|
||||||
python -m pip install -r requirements.txt
|
python -m pip install -r requirements.txt
|
||||||
|
|
||||||
# Expose the port that the application listens on.
|
|
||||||
EXPOSE 5000
|
EXPOSE 5000
|
||||||
|
|
||||||
# Run the application.
|
# Run the application.
|
||||||
CMD gunicorn 'app:app' --bind=0.0.0.0:5000
|
CMD gunicorn 'app:app' --bind=0.0.0.0:5000 --timeout 3600
|
||||||
|
Binary file not shown.
@@ -1,12 +1,14 @@
|
|||||||
from flask import Flask
|
from flask import Flask
|
||||||
from controllers.status import status_blueprint
|
from controllers.status import status_blueprint
|
||||||
|
from controllers.simulation import simulation_blueprint
|
||||||
from controllers.image import image_blueprint
|
from controllers.image import image_blueprint
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app.run()
|
|
||||||
|
|
||||||
#
|
#
|
||||||
app.register_blueprint(status_blueprint)
|
app.register_blueprint(status_blueprint)
|
||||||
|
app.register_blueprint(simulation_blueprint)
|
||||||
app.register_blueprint(image_blueprint)
|
app.register_blueprint(image_blueprint)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run()
|
||||||
|
@@ -1,49 +0,0 @@
|
|||||||
# Comments are provided throughout this file to help you get started.
|
|
||||||
# If you need more help, visit the Docker compose reference guide at
|
|
||||||
# https://docs.docker.com/compose/compose-file/
|
|
||||||
|
|
||||||
# Here the instructions define your application as a service called "server".
|
|
||||||
# This service is built from the Dockerfile in the current directory.
|
|
||||||
# You can add other services your application may depend on here, such as a
|
|
||||||
# database or a cache. For examples, see the Awesome Compose repository:
|
|
||||||
# https://github.com/docker/awesome-compose
|
|
||||||
services:
|
|
||||||
server:
|
|
||||||
build:
|
|
||||||
context: .
|
|
||||||
ports:
|
|
||||||
- 5000:5000
|
|
||||||
|
|
||||||
# The commented out section below is an example of how to define a PostgreSQL
|
|
||||||
# database that your application can use. `depends_on` tells Docker Compose to
|
|
||||||
# start the database before your application. The `db-data` volume persists the
|
|
||||||
# database data between container restarts. The `db-password` secret is used
|
|
||||||
# to set the database password. You must create `db/password.txt` and add
|
|
||||||
# a password of your choosing to it before running `docker compose up`.
|
|
||||||
# depends_on:
|
|
||||||
# db:
|
|
||||||
# condition: service_healthy
|
|
||||||
# db:
|
|
||||||
# image: postgres
|
|
||||||
# restart: always
|
|
||||||
# user: postgres
|
|
||||||
# secrets:
|
|
||||||
# - db-password
|
|
||||||
# volumes:
|
|
||||||
# - db-data:/var/lib/postgresql/data
|
|
||||||
# environment:
|
|
||||||
# - POSTGRES_DB=example
|
|
||||||
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
|
|
||||||
# expose:
|
|
||||||
# - 5432
|
|
||||||
# healthcheck:
|
|
||||||
# test: [ "CMD", "pg_isready" ]
|
|
||||||
# interval: 10s
|
|
||||||
# timeout: 5s
|
|
||||||
# retries: 5
|
|
||||||
# volumes:
|
|
||||||
# db-data:
|
|
||||||
# secrets:
|
|
||||||
# db-password:
|
|
||||||
# file: db/password.txt
|
|
||||||
|
|
Binary file not shown.
Binary file not shown.
@@ -1,28 +1,27 @@
|
|||||||
|
import io
|
||||||
from services.image import ImageService
|
from services.image import ImageService
|
||||||
from static.image_helper import ImageHelper
|
|
||||||
from flask import request, Response, Blueprint, jsonify, send_file
|
from flask import request, Response, Blueprint, jsonify, send_file
|
||||||
|
|
||||||
image_blueprint = Blueprint('image_blueprint', __name__)
|
image_blueprint = Blueprint('image_blueprint', __name__)
|
||||||
image_service = ImageService()
|
image_service = ImageService()
|
||||||
|
|
||||||
|
|
||||||
@image_blueprint.route('/image/blur', methods=['POST'])
|
@image_blueprint.route('/image/blur', methods=['POST'])
|
||||||
def blur_image():
|
def blur_image():
|
||||||
radius = int(request.form.get('radius'))
|
radius = int(request.args.get('radius'))
|
||||||
image = request.files.get('file')
|
image = request.get_data()
|
||||||
|
|
||||||
if radius and image:
|
if radius and image:
|
||||||
return send_file(image_service.box_blur_image(image, radius),
|
return send_file(io.BytesIO(image_service.box_blur_image(image, radius)),
|
||||||
mimetype='image/jpeg',
|
mimetype='image/png',
|
||||||
as_attachment=True,
|
as_attachment=True,
|
||||||
download_name='blurred_image.jpeg')
|
download_name='blurred_image.png')
|
||||||
|
|
||||||
return "Bad request", 400
|
return "Bad request", 400
|
||||||
|
|
||||||
|
|
||||||
@image_blueprint.route('/image/load-small-image', methods=['GET'])
|
@image_blueprint.route('/image/load-small-image', methods=['GET'])
|
||||||
def get_simple_image():
|
def get_simple_image():
|
||||||
return send_file(image_service.get_simple_image(),
|
return send_file(io.BytesIO(image_service.get_simple_image()),
|
||||||
mimetype='image/png',
|
mimetype='image/png',
|
||||||
as_attachment=True,
|
as_attachment=True,
|
||||||
download_name='small-image.png')
|
download_name='small-image.png')
|
||||||
@@ -30,7 +29,7 @@ def get_simple_image():
|
|||||||
|
|
||||||
@image_blueprint.route('/image/load-big-image', methods=['GET'])
|
@image_blueprint.route('/image/load-big-image', methods=['GET'])
|
||||||
def get_big_image():
|
def get_big_image():
|
||||||
return send_file(image_service.get_big_image(),
|
return send_file(io.BytesIO(image_service.get_big_image()),
|
||||||
mimetype='image/png',
|
mimetype='image/png',
|
||||||
as_attachment=True,
|
as_attachment=True,
|
||||||
download_name='big-image.png')
|
download_name='big-image.png')
|
||||||
@@ -38,4 +37,6 @@ def get_big_image():
|
|||||||
|
|
||||||
@image_blueprint.route('/image/save-big-image', methods=['POST'])
|
@image_blueprint.route('/image/save-big-image', methods=['POST'])
|
||||||
def save_image():
|
def save_image():
|
||||||
pass
|
image_service.save_image(request.get_data())
|
||||||
|
|
||||||
|
return "OK", 200
|
||||||
|
30
FlaskAPI/controllers/simulation.py
Normal file
30
FlaskAPI/controllers/simulation.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
from flask import request, Blueprint, jsonify
|
||||||
|
|
||||||
|
simulation_blueprint = Blueprint('simulation_blueprint', __name__)
|
||||||
|
|
||||||
|
class SimulationController:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def harmonic_progression(self, n):
|
||||||
|
sum = 0
|
||||||
|
for i in range(1, n):
|
||||||
|
sum += 1/i
|
||||||
|
|
||||||
|
return sum
|
||||||
|
|
||||||
|
def return_helloworld(self):
|
||||||
|
return jsonify(message="Hello World!")
|
||||||
|
|
||||||
|
simulation_controller = SimulationController()
|
||||||
|
|
||||||
|
@simulation_blueprint.route('/simulation/harmonic-progression', methods=['GET'])
|
||||||
|
def return_ok():
|
||||||
|
n = int(request.args.get('n'))
|
||||||
|
sum = simulation_controller.harmonic_progression(n)
|
||||||
|
|
||||||
|
return str(sum), 200
|
||||||
|
|
||||||
|
@simulation_blueprint.route('/simulation/json', methods=['GET'])
|
||||||
|
def return_ok():
|
||||||
|
return simulation_controller.return_helloworld(), 200
|
@@ -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
|
||||||
|
Binary file not shown.
@@ -1,69 +1,60 @@
|
|||||||
from wand.image import Image
|
from wand.image import Image, Color
|
||||||
|
|
||||||
from static.image_helper import ImageHelper
|
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()
|
||||||
|
|
||||||
def box_blur_image_separable(image, blurred_image, radius_x, radius_y):
|
for y in range(height):
|
||||||
pixels = image.get_pixels()
|
for x in range(width):
|
||||||
blurred_pixels = blurred_image.get_pixels()
|
|
||||||
|
|
||||||
for pixel in pixels:
|
|
||||||
x, y = pixel[0], pixel[1]
|
|
||||||
|
|
||||||
r_total, g_total, b_total = 0, 0, 0
|
r_total, g_total, b_total = 0, 0, 0
|
||||||
pixel_count = 0
|
|
||||||
for offset_y in range(-radius_y, radius_y + 1):
|
for offset_y in range(-radius_y, radius_y + 1):
|
||||||
for offset_x in range(-radius_x, radius_x + 1):
|
for offset_x in range(-radius_x, radius_x + 1):
|
||||||
new_x = x + offset_x
|
new_x = x + offset_x
|
||||||
new_y = y + offset_y
|
new_y = y + offset_y
|
||||||
|
|
||||||
if 0 <= new_x < image.width and 0 <= new_y < image.height:
|
if 0 <= new_x < width and 0 <= new_y < height:
|
||||||
pixel_color = pixels[new_y * image.width + new_x]
|
pixel = image[new_x, new_y]
|
||||||
r_total += pixel_color.red
|
r_total += pixel.red_int8
|
||||||
g_total += pixel_color.green
|
g_total += pixel.green_int8
|
||||||
b_total += pixel_color.blue
|
b_total += pixel.blue_int8
|
||||||
pixel_count += 1
|
|
||||||
|
|
||||||
blurred_pixel = blurred_pixels[y * image.width + x]
|
r_avg = int(r_total / kernel_area)
|
||||||
blurred_pixel.red = int(r_total / pixel_count)
|
g_avg = int(g_total / kernel_area)
|
||||||
blurred_pixel.green = int(g_total / pixel_count)
|
b_avg = int(b_total / kernel_area)
|
||||||
blurred_pixel.blue = int(b_total / pixel_count)
|
|
||||||
|
blurred_image[x, y] = Color(f'rgb({r_avg},{g_avg},{b_avg})')
|
||||||
|
|
||||||
return blurred_image
|
return blurred_image
|
||||||
|
|
||||||
|
|
||||||
def save_image(file_stream):
|
|
||||||
with open("image.png", "wb") as file:
|
|
||||||
file.write(file_stream.read())
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
|
|
||||||
class ImageService:
|
class ImageService:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def box_blur_image(self, img, radius):
|
def box_blur_image(self, img, radius):
|
||||||
temp_path = 'temp_image.png'
|
with Image(blob=img) as image:
|
||||||
img.save(temp_path)
|
image.gaussian_blur(radius, radius)
|
||||||
|
return image.make_blob()
|
||||||
with Image(filename=temp_path) as img:
|
|
||||||
img.blur(radius, 2)
|
|
||||||
blurred_temp_path = 'blurred_temp_image.png'
|
|
||||||
img.save(filename='blurred_temp_image.png')
|
|
||||||
return blurred_temp_path
|
|
||||||
|
|
||||||
|
# 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):
|
def get_simple_image(self):
|
||||||
with ImageHelper.SimpleImage as img:
|
with open("./static/small-image.png", "rb") as file:
|
||||||
img = ImageHelper.SimpleImage
|
return file.read()
|
||||||
simple_image = 'simple_image.png'
|
|
||||||
img.save(filename='simple_image.png')
|
|
||||||
return simple_image
|
|
||||||
|
|
||||||
|
|
||||||
def get_big_image(self):
|
def get_big_image(self):
|
||||||
with ImageHelper.BigImage as img:
|
with open("./static/big-image.png", "rb") as file:
|
||||||
img = ImageHelper.BigImage
|
return file.read()
|
||||||
big_image = 'big_image.png'
|
|
||||||
img.save(filename='big_image.png')
|
def save_image(self, img):
|
||||||
return big_image
|
with open("image.png", "wb") as file:
|
||||||
|
file.write(img)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
Binary file not shown.
@@ -1,17 +0,0 @@
|
|||||||
from wand.image import Image
|
|
||||||
|
|
||||||
|
|
||||||
class ImageHelper:
|
|
||||||
SimpleImage = None
|
|
||||||
BigImage = None
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def load_images():
|
|
||||||
ImageHelper.SimpleImage = Image(filename="./static/small-image.png")
|
|
||||||
#ImageHelper.SimpleImage.save(filename="./static/small-image.png")
|
|
||||||
|
|
||||||
ImageHelper.BigImage = Image(filename="./static/big-image.png")
|
|
||||||
#ImageHelper.BigImage.save(filename="./static/big-image.png")
|
|
||||||
pass
|
|
||||||
|
|
||||||
ImageHelper.load_images()
|
|
Binary file not shown.
Before Width: | Height: | Size: 44 KiB |
51
docker-compose.yml
Normal file
51
docker-compose.yml
Normal 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
|
28
scripts/common.py
Normal file
28
scripts/common.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
FRAMEWORKS = [
|
||||||
|
('Actix', 'tcc-actix', 'orange'),
|
||||||
|
('ASP.NET', 'tcc-aspnet', 'blue'),
|
||||||
|
('Flask', 'tcc-flask', 'green'),
|
||||||
|
('Express', 'tcc-express', 'red'),
|
||||||
|
]
|
||||||
|
|
||||||
|
ENDPOINTS = {
|
||||||
|
'Actix': 'http://localhost:9083',
|
||||||
|
'ASP.NET': 'http://localhost:9081',
|
||||||
|
'Flask': 'http://localhost:9082',
|
||||||
|
'Express': 'http://localhost:9084',
|
||||||
|
}
|
||||||
|
|
||||||
|
AVG_RUNS = 5
|
||||||
|
|
||||||
|
API_REQUESTS = [
|
||||||
|
('/status/ok', '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/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),
|
||||||
|
('/static/nginx.html', 'GET', range(0, 30_000, 5000), None),
|
||||||
|
]
|
143
scripts/graph.py
Normal file
143
scripts/graph.py
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from common import API_REQUESTS, FRAMEWORKS
|
||||||
|
from pylab import plot, show, savefig, xlim, figure, \
|
||||||
|
ylim, legend, boxplot, setp, axes
|
||||||
|
|
||||||
|
FRAMEWORKS_COLORS = [c for _, _, c in FRAMEWORKS]
|
||||||
|
FRAMEWORKS = [f for f, _, _ in FRAMEWORKS]
|
||||||
|
|
||||||
|
def setBoxColors(bp):
|
||||||
|
for i, box in enumerate(bp['boxes']):
|
||||||
|
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.xlabel(x_label)
|
||||||
|
plt.ylabel(y_label)
|
||||||
|
|
||||||
|
if y_lim:
|
||||||
|
plt.ylim(y_lim)
|
||||||
|
|
||||||
|
for i, framework in enumerate(FRAMEWORKS):
|
||||||
|
h, = plt.plot([], c=FRAMEWORKS_COLORS[i], label=framework)
|
||||||
|
# h.set_visible(False)
|
||||||
|
|
||||||
|
plt.legend()
|
||||||
|
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_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'
|
||||||
|
filename = filename.replace("?", "_")
|
||||||
|
generate_resource_graph(filename, framework_name, endpoint_name)
|
||||||
|
|
||||||
|
filename = f'data/req_ASP.NET_{endpoint_file}.csv'
|
||||||
|
filename = filename.replace("?", "_")
|
||||||
|
generate_req_graph(filename, framework_name, endpoint_name)
|
17
scripts/init.py
Normal file
17
scripts/init.py
Normal 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')
|
@@ -1,33 +1,145 @@
|
|||||||
import requests
|
import requests
|
||||||
|
import docker
|
||||||
import concurrent.futures
|
import concurrent.futures
|
||||||
import time
|
import time
|
||||||
|
import os
|
||||||
|
from math import floor
|
||||||
|
from init import init
|
||||||
|
from common import FRAMEWORKS, ENDPOINTS, API_REQUESTS, AVG_RUNS
|
||||||
|
|
||||||
URL_BASE = 'http://localhost:5100'
|
init()
|
||||||
|
|
||||||
num_requests = [1000, 5000, 10_000, 50_000, 100_000, 500_000, 1_000_000]
|
THREADS = 10
|
||||||
|
FRAMEWORK_NAME = ""
|
||||||
|
CONTAINER_NAME = ""
|
||||||
|
URL_BASE = 'http://localhost:9090'
|
||||||
|
|
||||||
def send_request(session, url):
|
def send_request(url, method = 'GET', payload = None):
|
||||||
response = session.get(url)
|
success = False
|
||||||
return response.status_code
|
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)
|
||||||
|
|
||||||
def main():
|
|
||||||
for num_request in num_requests:
|
for num_request in num_requests:
|
||||||
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
if num_request <= 0: continue
|
||||||
url = f'{URL_BASE}/status/ok'
|
|
||||||
|
for run in range(AVG_RUNS):
|
||||||
|
ok_responses = 0
|
||||||
|
bad_responses = 0
|
||||||
|
server_errors = 0
|
||||||
|
|
||||||
|
cpu, ram = 0, 0
|
||||||
|
|
||||||
|
with concurrent.futures.ThreadPoolExecutor(max_workers=THREADS) as executor:
|
||||||
|
url = f'{URL_BASE}{endpoint}'
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
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)]
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
concurrent.futures.wait(futures)
|
concurrent.futures.wait(futures)
|
||||||
|
|
||||||
elapsed_time = time.time() - start_time
|
elapsed_time = time.time() - start_time
|
||||||
|
|
||||||
successful_responses = sum(1 for future in futures if future.result() == 200)
|
for future in futures:
|
||||||
|
responses = future.result()
|
||||||
|
ok_responses += responses[2]
|
||||||
|
bad_responses += responses[4]
|
||||||
|
server_errors += responses[5]
|
||||||
|
|
||||||
print(f"All requests completed in {elapsed_time:.2f} seconds. {elapsed_time/num_request:.4f} seconds per request. {num_request/elapsed_time:.2f} requests per second.")
|
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}]]")
|
||||||
print(f"Successful responses: {successful_responses}/{num_request}")
|
|
||||||
|
|
||||||
main()
|
client = docker.from_env()
|
||||||
|
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)
|
||||||
|
|
||||||
|
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
1
tcc-express
Submodule
Submodule tcc-express added at 504b59278f
Reference in New Issue
Block a user