Skip to content

Commit 992475c

Browse files
Axum perf improvements (#9379)
* perf: switch plaintext and json to one runtime per thread, improving performance. * perf: remove need for additional vec * perf: reduce length of query parameter * perf: increase strength of inlining hint * perf: reduce query length * perf: shorten path and use references * bug: increased length of route in line with requirements
1 parent aecc2a4 commit 992475c

File tree

6 files changed

+33
-30
lines changed

6 files changed

+33
-30
lines changed

frameworks/Rust/axum/benchmark_config.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"docker_cmd": "/app/axum-sqlx",
2929
"db_url": "/db",
3030
"fortune_url": "/fortunes",
31-
"cached_query_url": "/cached-queries?queries=",
31+
"cached_query_url": "/cached-queries?q=",
3232
"port": 8000,
3333
"approach": "Realistic",
3434
"classification": "Fullstack",
@@ -50,8 +50,8 @@
5050
"docker_cmd": "/app/axum-pg",
5151
"db_url": "/db",
5252
"fortune_url": "/fortunes",
53-
"query_url": "/queries?queries=",
54-
"update_url": "/updates?queries=",
53+
"query_url": "/queries?q=",
54+
"update_url": "/updates?q=",
5555
"port": 8000,
5656
"approach": "Realistic",
5757
"classification": "Fullstack",
@@ -72,8 +72,8 @@
7272
"dockerfile": "axum.dockerfile",
7373
"docker_cmd": "/app/axum-pg-pool",
7474
"db_url": "/db",
75-
"query_url": "/queries?queries=",
76-
"update_url": "/updates?queries=",
75+
"query_url": "/queries?q=",
76+
"update_url": "/updates?q=",
7777
"fortune_url": "/fortunes",
7878
"port": 8000,
7979
"approach": "Realistic",
@@ -95,9 +95,9 @@
9595
"dockerfile": "axum.dockerfile",
9696
"docker_cmd": "/app/axum-mongo",
9797
"db_url": "/db",
98-
"query_url": "/queries?queries=",
98+
"query_url": "/queries?q=",
9999
"fortune_url": "/fortunes",
100-
"update_url": "/updates?queries=",
100+
"update_url": "/updates?q=",
101101
"port": 8000,
102102
"approach": "Realistic",
103103
"classification": "Fullstack",
@@ -118,8 +118,8 @@
118118
"dockerfile": "axum.dockerfile",
119119
"docker_cmd": "/app/axum-mongo-raw",
120120
"db_url": "/db",
121-
"query_url": "/queries?queries=",
122-
"update_url": "/updates?queries=",
121+
"query_url": "/queries?q=",
122+
"update_url": "/updates?q=",
123123
"port": 8000,
124124
"approach": "Realistic",
125125
"classification": "Fullstack",

frameworks/Rust/axum/src/common/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ where
3636

3737
/// Generate a single integer in the range 1 to 10,000 (inclusive)
3838
#[allow(dead_code)]
39-
#[inline]
39+
#[inline(always)]
4040
pub fn random_id(rng: &mut SmallRng) -> i32 {
4141
rng.gen_range(1..10_001)
4242
}
4343

4444
/// Generate vector of integers in the range 1 to 10,000 (inclusive)
4545
#[allow(dead_code)]
46-
#[inline]
46+
#[inline(always)]
4747
pub fn random_ids(rng: &mut SmallRng, count: usize) -> Vec<i32> {
4848
rng.sample_iter(Uniform::new(1, 10_001))
4949
.take(count)

frameworks/Rust/axum/src/common/utils.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ use serde::Deserialize;
77

88
#[derive(Debug, Deserialize)]
99
pub struct Params {
10-
queries: Option<String>,
10+
q: Option<String>,
1111
}
1212

1313
#[allow(dead_code)]
14+
#[inline(always)]
1415
pub fn parse_params(params: Params) -> usize {
1516
params
16-
.queries
17+
.q
1718
.and_then(|q| q.parse().ok())
1819
.unwrap_or(1)
1920
.clamp(1, 500)

frameworks/Rust/axum/src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ pub async fn json() -> impl IntoResponse {
2626
(StatusCode::OK, Json(message))
2727
}
2828

29-
#[tokio::main]
30-
async fn main() {
29+
fn main() {
3130
dotenv().ok();
31+
server::start_tokio(serve_app)
32+
}
33+
34+
async fn serve_app() {
3235

3336
let app = Router::new()
3437
.route("/plaintext", get(plaintext))
3538
.route("/json", get(json));
3639

3740
server::serve_hyper(app, Some(8000)).await
38-
}
41+
}

frameworks/Rust/axum/src/main_sqlx.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
mod common;
22
mod sqlx;
33

4+
use std::sync::Arc;
5+
46
use ::sqlx::PgPool;
57
use axum::{
68
extract::{Query, State},
@@ -56,7 +58,7 @@ async fn queries(
5658
let ids = random_ids(&mut rng, count);
5759
let mut worlds: Vec<World> = Vec::with_capacity(count);
5860

59-
for id in ids {
61+
for id in &ids {
6062
let world: World = ::sqlx::query_as(common::SELECT_WORLD_BY_ID)
6163
.bind(id)
6264
.fetch_one(&mut *db.acquire().await.unwrap())
@@ -96,7 +98,7 @@ async fn cache(
9698
) -> impl IntoResponse {
9799
let count = parse_params(params);
98100
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
99-
let mut worlds: Vec<Option<World>> = Vec::with_capacity(count);
101+
let mut worlds: Vec<Option<Arc<World>>> = Vec::with_capacity(count);
100102

101103
for id in random_ids(&mut rng, count) {
102104
worlds.push(cache.get(&id).await);
@@ -113,15 +115,15 @@ async fn preload_cache(AppState { db, cache }: &AppState) {
113115
.expect("error loading worlds");
114116

115117
for world in worlds {
116-
cache.insert(world.id, world).await;
118+
cache.insert(world.id, Arc::new(world)).await;
117119
}
118120
}
119121

120122
/// Application state
121123
#[derive(Clone)]
122124
struct AppState {
123125
db: PgPool,
124-
cache: Cache<i32, World>,
126+
cache: Cache<i32, Arc<World>>,
125127
}
126128

127129
#[tokio::main]

frameworks/Rust/axum/src/pg/database.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,18 @@ impl PgConnection {
9090
}
9191

9292
pub async fn update_worlds(&self, num: usize) -> Result<Vec<World>, PgError> {
93-
let worlds = self.fetch_random_worlds(num).await?;
93+
let mut worlds = self.fetch_random_worlds(num).await?;
9494

9595
// Update the worlds with new random numbers
9696
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
9797
let mut ids = Vec::with_capacity(num);
9898
let mut nids = Vec::with_capacity(num);
99-
let worlds: Vec<World> = worlds
100-
.into_iter()
101-
.map(|mut w| {
102-
w.randomnumber = random_id(&mut rng);
103-
ids.push(w.id);
104-
nids.push(w.randomnumber);
105-
w
106-
})
107-
.collect();
99+
100+
for w in &mut worlds {
101+
w.randomnumber = random_id(&mut rng);
102+
ids.push(w.id);
103+
nids.push(w.randomnumber);
104+
}
108105

109106
// Update the random worlds in the database.
110107
self.client

0 commit comments

Comments
 (0)