From e9c9d49552079a32956fd15ebe0412db8bc9ca5b Mon Sep 17 00:00:00 2001 From: Andrew James <59655451+andrew-james-dev@users.noreply.github.com> Date: Sat, 2 Nov 2024 22:43:06 +0000 Subject: [PATCH 1/7] perf: switch plaintext and json to one runtime per thread, improving performance. --- frameworks/Rust/axum/src/main.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/frameworks/Rust/axum/src/main.rs b/frameworks/Rust/axum/src/main.rs index 70858e27ddc..0b33fb7edcb 100644 --- a/frameworks/Rust/axum/src/main.rs +++ b/frameworks/Rust/axum/src/main.rs @@ -26,13 +26,16 @@ pub async fn json() -> impl IntoResponse { (StatusCode::OK, Json(message)) } -#[tokio::main] -async fn main() { +fn main() { dotenv().ok(); + server::start_tokio(serve_app) +} + +async fn serve_app() { let app = Router::new() .route("/plaintext", get(plaintext)) .route("/json", get(json)); server::serve_hyper(app, Some(8000)).await -} +} \ No newline at end of file From 2c692dc906479025688f19ba0c655d98b20a134c Mon Sep 17 00:00:00 2001 From: Andrew James <59655451+andrew-james-dev@users.noreply.github.com> Date: Sat, 2 Nov 2024 22:44:08 +0000 Subject: [PATCH 2/7] perf: remove need for additional vec --- frameworks/Rust/axum/src/pg/database.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/frameworks/Rust/axum/src/pg/database.rs b/frameworks/Rust/axum/src/pg/database.rs index bc78b2ba5c8..b9cff086c5a 100644 --- a/frameworks/Rust/axum/src/pg/database.rs +++ b/frameworks/Rust/axum/src/pg/database.rs @@ -90,21 +90,18 @@ impl PgConnection { } pub async fn update_worlds(&self, num: usize) -> Result, PgError> { - let worlds = self.fetch_random_worlds(num).await?; + let mut worlds = self.fetch_random_worlds(num).await?; // Update the worlds with new random numbers let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap(); let mut ids = Vec::with_capacity(num); let mut nids = Vec::with_capacity(num); - let worlds: Vec = worlds - .into_iter() - .map(|mut w| { - w.randomnumber = random_id(&mut rng); - ids.push(w.id); - nids.push(w.randomnumber); - w - }) - .collect(); + + for w in &mut worlds { + w.randomnumber = random_id(&mut rng); + ids.push(w.id); + nids.push(w.randomnumber); + } // Update the random worlds in the database. self.client From f2d62104bd0b2d095a518db3ec963e4f74f34cd4 Mon Sep 17 00:00:00 2001 From: Andrew James <59655451+andrew-james-dev@users.noreply.github.com> Date: Sat, 2 Nov 2024 22:45:39 +0000 Subject: [PATCH 3/7] perf: reduce length of query parameter --- frameworks/Rust/axum/src/common/utils.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/frameworks/Rust/axum/src/common/utils.rs b/frameworks/Rust/axum/src/common/utils.rs index 063411f3da7..0da86151e62 100644 --- a/frameworks/Rust/axum/src/common/utils.rs +++ b/frameworks/Rust/axum/src/common/utils.rs @@ -7,13 +7,14 @@ use serde::Deserialize; #[derive(Debug, Deserialize)] pub struct Params { - queries: Option, + q: Option, } #[allow(dead_code)] +#[inline(always)] pub fn parse_params(params: Params) -> usize { params - .queries + .q .and_then(|q| q.parse().ok()) .unwrap_or(1) .clamp(1, 500) From 700bb263ee1b36d7e813c004d07d7cfa221ecdf7 Mon Sep 17 00:00:00 2001 From: Andrew James <59655451+andrew-james-dev@users.noreply.github.com> Date: Sat, 2 Nov 2024 22:46:55 +0000 Subject: [PATCH 4/7] perf: increase strength of inlining hint --- frameworks/Rust/axum/src/common/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/Rust/axum/src/common/mod.rs b/frameworks/Rust/axum/src/common/mod.rs index d55958155f1..808b2a70eeb 100644 --- a/frameworks/Rust/axum/src/common/mod.rs +++ b/frameworks/Rust/axum/src/common/mod.rs @@ -36,14 +36,14 @@ where /// Generate a single integer in the range 1 to 10,000 (inclusive) #[allow(dead_code)] -#[inline] +#[inline(always)] pub fn random_id(rng: &mut SmallRng) -> i32 { rng.gen_range(1..10_001) } /// Generate vector of integers in the range 1 to 10,000 (inclusive) #[allow(dead_code)] -#[inline] +#[inline(always)] pub fn random_ids(rng: &mut SmallRng, count: usize) -> Vec { rng.sample_iter(Uniform::new(1, 10_001)) .take(count) From e1e7b9f14ac52777c72ce09ee437b2d3ac06878f Mon Sep 17 00:00:00 2001 From: Andrew James <59655451+andrew-james-dev@users.noreply.github.com> Date: Sat, 2 Nov 2024 22:54:34 +0000 Subject: [PATCH 5/7] perf: reduce query length --- frameworks/Rust/axum/benchmark_config.json | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/frameworks/Rust/axum/benchmark_config.json b/frameworks/Rust/axum/benchmark_config.json index ade60875389..8c5d6baab4b 100755 --- a/frameworks/Rust/axum/benchmark_config.json +++ b/frameworks/Rust/axum/benchmark_config.json @@ -28,7 +28,7 @@ "docker_cmd": "/app/axum-sqlx", "db_url": "/db", "fortune_url": "/fortunes", - "cached_query_url": "/cached-queries?queries=", + "cached_query_url": "/cached?q=", "port": 8000, "approach": "Realistic", "classification": "Fullstack", @@ -50,8 +50,8 @@ "docker_cmd": "/app/axum-pg", "db_url": "/db", "fortune_url": "/fortunes", - "query_url": "/queries?queries=", - "update_url": "/updates?queries=", + "query_url": "/queries?q=", + "update_url": "/updates?q=", "port": 8000, "approach": "Realistic", "classification": "Fullstack", @@ -72,8 +72,8 @@ "dockerfile": "axum.dockerfile", "docker_cmd": "/app/axum-pg-pool", "db_url": "/db", - "query_url": "/queries?queries=", - "update_url": "/updates?queries=", + "query_url": "/queries?q=", + "update_url": "/updates?q=", "fortune_url": "/fortunes", "port": 8000, "approach": "Realistic", @@ -95,9 +95,9 @@ "dockerfile": "axum.dockerfile", "docker_cmd": "/app/axum-mongo", "db_url": "/db", - "query_url": "/queries?queries=", + "query_url": "/queries?q=", "fortune_url": "/fortunes", - "update_url": "/updates?queries=", + "update_url": "/updates?q=", "port": 8000, "approach": "Realistic", "classification": "Fullstack", @@ -118,8 +118,8 @@ "dockerfile": "axum.dockerfile", "docker_cmd": "/app/axum-mongo-raw", "db_url": "/db", - "query_url": "/queries?queries=", - "update_url": "/updates?queries=", + "query_url": "/queries?q=", + "update_url": "/updates?q=", "port": 8000, "approach": "Realistic", "classification": "Fullstack", From 2d67f902e6faa1a8ff52d190d11429d94895465f Mon Sep 17 00:00:00 2001 From: Andrew James <59655451+andrew-james-dev@users.noreply.github.com> Date: Sun, 3 Nov 2024 11:59:08 +0000 Subject: [PATCH 6/7] perf: shorten path and use references --- frameworks/Rust/axum/src/main_sqlx.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/frameworks/Rust/axum/src/main_sqlx.rs b/frameworks/Rust/axum/src/main_sqlx.rs index 163372b83d8..6fa60c036e5 100644 --- a/frameworks/Rust/axum/src/main_sqlx.rs +++ b/frameworks/Rust/axum/src/main_sqlx.rs @@ -1,6 +1,8 @@ mod common; mod sqlx; +use std::sync::Arc; + use ::sqlx::PgPool; use axum::{ extract::{Query, State}, @@ -56,7 +58,7 @@ async fn queries( let ids = random_ids(&mut rng, count); let mut worlds: Vec = Vec::with_capacity(count); - for id in ids { + for id in &ids { let world: World = ::sqlx::query_as(common::SELECT_WORLD_BY_ID) .bind(id) .fetch_one(&mut *db.acquire().await.unwrap()) @@ -96,7 +98,7 @@ async fn cache( ) -> impl IntoResponse { let count = parse_params(params); let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap(); - let mut worlds: Vec> = Vec::with_capacity(count); + let mut worlds: Vec>> = Vec::with_capacity(count); for id in random_ids(&mut rng, count) { worlds.push(cache.get(&id).await); @@ -113,7 +115,7 @@ async fn preload_cache(AppState { db, cache }: &AppState) { .expect("error loading worlds"); for world in worlds { - cache.insert(world.id, world).await; + cache.insert(world.id, Arc::new(world)).await; } } @@ -121,7 +123,7 @@ async fn preload_cache(AppState { db, cache }: &AppState) { #[derive(Clone)] struct AppState { db: PgPool, - cache: Cache, + cache: Cache>, } #[tokio::main] @@ -147,7 +149,7 @@ async fn main() { .route("/fortunes", get(fortunes)) .route("/db", get(db)) .route("/queries", get(queries)) - .route("/cached-queries", get(cache)) + .route("/cached", get(cache)) .with_state(state); server::serve_hyper(app, Some(8000)).await From a70af95ef104a264b7216acb3bea3afc3bbfe00d Mon Sep 17 00:00:00 2001 From: Andrew James <59655451+andrew-james-dev@users.noreply.github.com> Date: Sun, 3 Nov 2024 12:22:30 +0000 Subject: [PATCH 7/7] bug: increased length of route in line with requirements --- frameworks/Rust/axum/benchmark_config.json | 2 +- frameworks/Rust/axum/src/main_sqlx.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/Rust/axum/benchmark_config.json b/frameworks/Rust/axum/benchmark_config.json index 8c5d6baab4b..c33a1edad78 100755 --- a/frameworks/Rust/axum/benchmark_config.json +++ b/frameworks/Rust/axum/benchmark_config.json @@ -28,7 +28,7 @@ "docker_cmd": "/app/axum-sqlx", "db_url": "/db", "fortune_url": "/fortunes", - "cached_query_url": "/cached?q=", + "cached_query_url": "/cached-queries?q=", "port": 8000, "approach": "Realistic", "classification": "Fullstack", diff --git a/frameworks/Rust/axum/src/main_sqlx.rs b/frameworks/Rust/axum/src/main_sqlx.rs index 6fa60c036e5..b3a7937841b 100644 --- a/frameworks/Rust/axum/src/main_sqlx.rs +++ b/frameworks/Rust/axum/src/main_sqlx.rs @@ -149,7 +149,7 @@ async fn main() { .route("/fortunes", get(fortunes)) .route("/db", get(db)) .route("/queries", get(queries)) - .route("/cached", get(cache)) + .route("/cached-queries", get(cache)) .with_state(state); server::serve_hyper(app, Some(8000)).await