Browse Source

optimize db queries (#7020)

Nikolay Kim 3 years ago
parent
commit
70359e2d9e

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

@@ -37,12 +37,12 @@ tokio = ["ntex/tokio"]
 async-std = ["ntex/async-std"]
 
 [dependencies]
-ntex = "0.5.6"
+ntex = "0.5.8"
 mimalloc = { version = "0.1.25", default-features = false }
 snmalloc-rs = { version = "0.2.26", features = ["1mib", "native-cpu"] }
 yarte = { version = "0.15", features = ["bytes-buf", "json"] }
 env_logger = "0.9"
-nanorand = { version = "0.5", default-features = false, features = ["std", "wyrand"] }
+nanorand = { version = "0.6", default-features = false, features = ["std", "wyrand"] }
 atoi = "0.4"
 num_cpus = "1.13"
 futures = "0.3"

+ 18 - 20
frameworks/Rust/ntex/src/db.rs

@@ -1,8 +1,8 @@
-use std::{borrow::Cow, cell::RefCell, fmt::Write as FmtWrite};
+use std::{borrow::Cow, fmt::Write as FmtWrite};
 
 use futures::{Future, FutureExt};
-use nanorand::{WyRand, RNG};
-use ntex::util::{Bytes, BytesMut};
+use nanorand::{WyRand, Rng};
+use ntex::util::{join_all, Bytes, BytesMut};
 use smallvec::SmallVec;
 use tokio_postgres::types::ToSql;
 use tokio_postgres::{connect, Client, Statement};
@@ -30,7 +30,7 @@ pub struct PgConnection {
     cl: Client,
     fortune: Statement,
     world: Statement,
-    rng: RefCell<WyRand>,
+    rng: WyRand,
     updates: Vec<Statement>,
 }
 
@@ -67,14 +67,14 @@ impl PgConnection {
             fortune,
             world,
             updates,
-            rng: RefCell::new(WyRand::new()),
+            rng: WyRand::new(),
         }
     }
 }
 
 impl PgConnection {
     pub fn get_world(&self) -> impl Future<Output = Bytes> {
-        let random_id = (self.rng.borrow_mut().generate::<u32>() % 10_000 + 1) as i32;
+        let random_id = (self.rng.clone().generate::<u32>() % 10_000 + 1) as i32;
         self.cl.query(&self.world, &[&random_id]).map(|rows| {
             let rows = rows.unwrap();
             let mut body = BytesMut::new();
@@ -92,7 +92,7 @@ impl PgConnection {
 
     pub fn get_worlds(&self, num: u16) -> impl Future<Output = Vec<World>> {
         let mut futs = Vec::with_capacity(num as usize);
-        let mut rng = self.rng.borrow_mut();
+        let mut rng = self.rng.clone();
         for _ in 0..num {
             let w_id = (rng.generate::<u32>() % 10_000 + 1) as i32;
             futs.push(self.cl.query(&self.world, &[&w_id]));
@@ -100,8 +100,8 @@ impl PgConnection {
 
         async move {
             let mut worlds: Vec<World> = Vec::with_capacity(num as usize);
-            for q in futs {
-                let rows = q.await.unwrap();
+            for item in join_all(futs).await {
+                let rows = item.unwrap();
                 worlds.push(World {
                     id: rows[0].get(0),
                     randomnumber: rows[0].get(1),
@@ -113,25 +113,23 @@ impl PgConnection {
 
     pub fn update(&self, num: u16) -> impl Future<Output = Vec<World>> {
         let mut futs = Vec::with_capacity(num as usize);
-        let mut rng = self.rng.borrow_mut();
+        let mut rng = self.rng.clone();
         for _ in 0..num {
-            let id = (rng.generate::<u32>() % 10_000 + 1) as i32;
             let w_id = (rng.generate::<u32>() % 10_000 + 1) as i32;
-            futs.push(self.cl.query(&self.world, &[&w_id]).map(move |res| {
-                let rows = res.unwrap();
-                World {
-                    id: rows[0].get(0),
-                    randomnumber: id,
-                }
-            }));
+            futs.push(self.cl.query(&self.world, &[&w_id]));
         }
 
         let cl = self.cl.clone();
         let st = self.updates[(num as usize) - 1].clone();
         async move {
             let mut worlds: Vec<World> = Vec::with_capacity(num as usize);
-            for q in futs {
-                worlds.push(q.await);
+            for q in join_all(futs).await {
+                let q = q.unwrap();
+                let id = (rng.generate::<u32>() % 10_000 + 1) as i32;
+                worlds.push(World {
+                    id: q[0].get(0),
+                    randomnumber: id,
+                })
             }
 
             let mut params: Vec<&dyn ToSql> = Vec::with_capacity(num as usize * 3);

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

@@ -86,8 +86,8 @@ async fn main() -> io::Result<()> {
         .backlog(1024)
         .bind("techempower", "0.0.0.0:8080", |cfg| {
             cfg.memory_pool(PoolId::P1);
-            PoolId::P1.set_read_params(65535, 8192);
-            PoolId::P1.set_write_params(65535, 8192);
+            PoolId::P1.set_read_params(65535, 1024);
+            PoolId::P1.set_write_params(65535, 1024);
 
             fn_service(|io| App {
                 io,