Skip to content

Commit 5709a30

Browse files
authored
ntex: Update bench tweaks (#9958)
* ntex: Update bench tweaks * fix * wip * wip * wip * wip
1 parent f1e5b50 commit 5709a30

File tree

2 files changed

+28
-45
lines changed

2 files changed

+28
-45
lines changed

frameworks/Rust/ntex/Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ neon = ["ntex/neon"]
6767
neon-uring = ["ntex/neon-uring"]
6868

6969
[dependencies]
70-
ntex = "2.12"
71-
ntex-compio = "0.2"
72-
ntex-neon = "0.1.13"
73-
ntex-net = "2.5.9"
70+
ntex = "2.13"
71+
ntex-compio = "0.3"
72+
ntex-neon = "0.1.31"
73+
ntex-net = "2.5.27"
7474
ntex-bytes = { version = "0.1.21", features=["simd"] }
7575
mimalloc = { version = "0.1.25", default-features = false }
7676
snmalloc-rs = { version = "0.3.3", features = ["native-cpu"] }
@@ -80,7 +80,8 @@ env_logger = "0.11"
8080
nanorand = { version = "0.7", default-features = false, features = ["std", "wyrand", "tls"] }
8181
atoi = "2.0"
8282
futures = "0.3"
83-
sonic-rs = "0.4.0"
83+
sonic-rs = "0.5.1"
84+
smallvec = "1.15"
8485
serde = { version = "1", features = ["derive"] }
8586
serde_json = "1"
8687
log = { version = "0.4", features = ["release_max_level_off"] }

frameworks/Rust/ntex/src/db.rs

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
#![allow(clippy::uninit_vec)]
2-
use std::{borrow::Cow, cell::RefCell, fmt::Write as FmtWrite};
1+
use std::{borrow::Cow, cell::RefCell};
32

43
use nanorand::{Rng, WyRand};
54
use ntex::util::{Bytes, BytesMut};
6-
use tokio_postgres::types::ToSql;
5+
use smallvec::SmallVec;
76
use tokio_postgres::{connect, Client, Statement};
87
use yarte::TemplateBytesTrait;
98

@@ -33,7 +32,7 @@ pub struct PgConnection {
3332
fortune: Statement,
3433
world: Statement,
3534
rng: WyRand,
36-
updates: Vec<Statement>,
35+
updates: Statement,
3736
buf: RefCell<BytesMut>,
3837
fbuf: RefCell<Vec<Fortune>>,
3938
}
@@ -48,24 +47,7 @@ impl PgConnection {
4847
});
4948

5049
let fortune = cl.prepare("SELECT * FROM fortune").await.unwrap();
51-
let mut updates = Vec::new();
52-
for num in 1..=500u16 {
53-
let mut pl: u16 = 1;
54-
let mut q = String::new();
55-
q.push_str("UPDATE world SET randomnumber = CASE id ");
56-
for _ in 1..=num {
57-
let _ = write!(&mut q, "when ${} then ${} ", pl, pl + 1);
58-
pl += 2;
59-
}
60-
q.push_str("ELSE randomnumber END WHERE id IN (");
61-
for _ in 1..=num {
62-
let _ = write!(&mut q, "${},", pl);
63-
pl += 1;
64-
}
65-
q.pop();
66-
q.push(')');
67-
updates.push(cl.prepare(&q).await.unwrap());
68-
}
50+
let updates = cl.prepare("UPDATE world w SET randomnumber = u.new_val FROM (SELECT unnest($1::int[]) as id, unnest($2::int[]) as new_val) u WHERE w.id = u.id").await.unwrap();
6951
let world = cl.prepare("SELECT * FROM world WHERE id=$1").await.unwrap();
7052

7153
PgConnection {
@@ -124,30 +106,30 @@ impl PgConnection {
124106

125107
pub async fn update(&self, num: usize) -> Bytes {
126108
let mut rng = nanorand::tls_rng();
127-
let mut queries = Vec::with_capacity(num);
109+
let mut ids = Vec::with_capacity(num);
110+
let mut numbers = Vec::with_capacity(num);
111+
let mut worlds = SmallVec::<[_; 32]>::new();
112+
let mut queries = SmallVec::<[_; 32]>::new();
113+
128114
(0..num).for_each(|_| {
129115
let w_id = (rng.generate::<u32>() % 10_000 + 1) as i32;
130-
queries.push(self.cl.query_one(&self.world, &[&w_id]));
116+
ids.push(w_id);
117+
numbers.push((rng.generate::<u32>() % 10_000 + 1) as i32);
131118
});
119+
ids.sort();
132120

133-
let mut worlds = Vec::with_capacity(num);
134-
for fut in queries.into_iter() {
135-
let row = fut.await.unwrap();
121+
(0..num).for_each(|idx| {
136122
worlds.push(World {
137-
id: row.get(0),
138-
randomnumber: (rng.generate::<u32>() % 10_000 + 1) as i32,
123+
id: ids[idx],
124+
randomnumber: numbers[idx],
139125
});
140-
}
141-
142-
let mut params: Vec<&dyn ToSql> = Vec::with_capacity(num * 3);
143-
for w in &worlds {
144-
params.push(&w.id);
145-
params.push(&w.randomnumber);
146-
}
147-
for w in &worlds {
148-
params.push(&w.id);
149-
}
150-
let _ = self.cl.query(&self.updates[num - 1], &params).await;
126+
queries.push(self.cl.query_one(&self.world, &[&ids[idx]]));
127+
});
128+
let _ = self
129+
.cl
130+
.query(&self.updates, &[&ids, &numbers])
131+
.await
132+
.unwrap();
151133

152134
let mut body = self.buf.borrow_mut();
153135
utils::reserve(&mut body, 2 * 1024);

0 commit comments

Comments
 (0)