Skip to content

Commit

Permalink
ntex: use physical cores for worker count
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Nov 10, 2023
1 parent 7471784 commit b19e527
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 55 deletions.
5 changes: 2 additions & 3 deletions frameworks/Rust/ntex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ async-std = ["ntex/async-std"]
[dependencies]
ntex = "0.7.2"
ntex-bytes = { version = "0.1.19", features=["simd"] }
core_affinity = "0.8"
mimalloc = { version = "0.1.25", default-features = false }
snmalloc-rs = { version = "0.3.3", features = ["native-cpu"] }
yarte = { version = "0.15", features = ["bytes-buf", "json"] }
buf-min = { version = "0.7", features = ["ntex-bytes"] }
env_logger = "0.10"
nanorand = { version = "0.7", default-features = false, features = ["std", "wyrand", "tls"] }
atoi = "2.0"
num_cpus = "1.13"
smallvec = "1.6.1"
num_cpus = "1.16"
smallvec = "1.11"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
log = { version = "0.4", features = ["release_max_level_off"] }
Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/ntex/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl PgConnection {
let _ = conn.await;
});

let fortune = cl.prepare("SELECT id, message FROM fortune").await.unwrap();
let fortune = cl.prepare("SELECT * FROM fortune").await.unwrap();
let mut updates = Vec::new();
for num in 1..=500u16 {
let mut pl: u16 = 1;
Expand Down
18 changes: 1 addition & 17 deletions frameworks/Rust/ntex/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#[global_allocator]
static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;

use std::sync::{Arc, Mutex};

use ntex::http::header::{CONTENT_TYPE, SERVER};
use ntex::{http, time::Seconds, util::BytesMut, util::PoolId, web};
use yarte::Serialize;
Expand Down Expand Up @@ -47,23 +45,9 @@ async fn plaintext() -> web::HttpResponse {
async fn main() -> std::io::Result<()> {
println!("Started http server: 127.0.0.1:8080");

let cores = core_affinity::get_core_ids().unwrap();
let total_cores = cores.len();
let cores = Arc::new(Mutex::new(cores));

// start http server
ntex::server::build()
.backlog(1024)
.configure(move |cfg| {
let cores = cores.clone();
cfg.on_worker_start(move |_| {
if let Some(core) = cores.lock().unwrap().pop() {
// Pin this worker to a single CPU core.
core_affinity::set_for_current(core);
}
std::future::ready(Ok::<_, &'static str>(()))
})
})?
.bind("techempower", "0.0.0.0:8080", |cfg| {
cfg.memory_pool(PoolId::P1);
PoolId::P1.set_read_params(65535, 2048);
Expand All @@ -74,7 +58,7 @@ async fn main() -> std::io::Result<()> {
.client_timeout(Seconds(0))
.h1(web::App::new().service(json).service(plaintext).finish())
})?
.workers(total_cores)
.workers(num_cpus::get_physical())
.run()
.await
}
20 changes: 2 additions & 18 deletions frameworks/Rust/ntex/src/main_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
// static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use std::sync::{Arc, Mutex};

use ntex::http::header::{CONTENT_TYPE, SERVER};
use ntex::http::{HttpService, KeepAlive, Request, Response, StatusCode};
use ntex::service::{Service, ServiceFactory, ServiceCtx};
use ntex::service::{Service, ServiceCtx, ServiceFactory};
use ntex::web::{Error, HttpResponse};
use ntex::{time::Seconds, util::BoxFuture, util::PoolId};

Expand Down Expand Up @@ -89,22 +87,8 @@ impl ServiceFactory<Request> for AppFactory {
async fn main() -> std::io::Result<()> {
println!("Starting http server: 127.0.0.1:8080");

let cores = core_affinity::get_core_ids().unwrap();
let total_cores = cores.len();
let cores = Arc::new(Mutex::new(cores));

ntex::server::build()
.backlog(1024)
.configure(move |cfg| {
let cores = cores.clone();
cfg.on_worker_start(move |_| {
if let Some(core) = cores.lock().unwrap().pop() {
// Pin this worker to a single CPU core.
core_affinity::set_for_current(core);
}
std::future::ready(Ok::<_, &'static str>(()))
})
})?
.bind("techempower", "0.0.0.0:8080", |cfg| {
cfg.memory_pool(PoolId::P1);
PoolId::P1.set_read_params(65535, 2048);
Expand All @@ -115,7 +99,7 @@ async fn main() -> std::io::Result<()> {
.client_timeout(Seconds(0))
.h1(AppFactory)
})?
.workers(total_cores)
.workers(num_cpus::get_physical())
.run()
.await
}
18 changes: 2 additions & 16 deletions frameworks/Rust/ntex/src/main_plt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[global_allocator]
static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
use std::{future::Future, io, pin::Pin, sync::Arc, sync::Mutex, task::Context, task::Poll};
use std::{future::Future, io, pin::Pin, task::Context, task::Poll};

use ntex::{fn_service, http::h1, io::Io, io::RecvError, util::ready, util::PoolId};
use yarte::Serialize;
Expand Down Expand Up @@ -74,23 +74,9 @@ impl Future for App {
async fn main() -> io::Result<()> {
println!("Started http server: 127.0.0.1:8080");

let cores = core_affinity::get_core_ids().unwrap();
let total_cores = cores.len();
let cores = Arc::new(Mutex::new(cores));

// start http server
ntex::server::build()
.backlog(1024)
.configure(move |cfg| {
let cores = cores.clone();
cfg.on_worker_start(move |_| {
if let Some(core) = cores.lock().unwrap().pop() {
// Pin this worker to a single CPU core.
core_affinity::set_for_current(core);
}
std::future::ready(Ok::<_, &'static str>(()))
})
})?
.bind("techempower", "0.0.0.0:8080", |cfg| {
cfg.memory_pool(PoolId::P1);
PoolId::P1.set_read_params(65535, 2048);
Expand All @@ -101,7 +87,7 @@ async fn main() -> io::Result<()> {
codec: h1::Codec::default(),
})
})?
.workers(total_cores)
.workers(num_cpus::get_physical())
.run()
.await
}

0 comments on commit b19e527

Please sign in to comment.