Browse Source

ntex: use physical cores for worker count (#8535)

Nikolay Kim 1 year ago
parent
commit
184bed0a08

+ 2 - 3
frameworks/Rust/ntex/Cargo.toml

@@ -39,7 +39,6 @@ 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"] }
@@ -47,8 +46,8 @@ 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"] }

+ 1 - 1
frameworks/Rust/ntex/src/db.rs

@@ -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;

+ 1 - 17
frameworks/Rust/ntex/src/main.rs

@@ -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;
@@ -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);
@@ -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
 }

+ 2 - 18
frameworks/Rust/ntex/src/main_db.rs

@@ -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};
 
@@ -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);
@@ -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
 }

+ 2 - 16
frameworks/Rust/ntex/src/main_plt.rs

@@ -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;
@@ -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);
@@ -101,7 +87,7 @@ async fn main() -> io::Result<()> {
                 codec: h1::Codec::default(),
             })
         })?
-        .workers(total_cores)
+        .workers(num_cpus::get_physical())
         .run()
         .await
 }