Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions frameworks/Rust/axum/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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-queries?q=",
"port": 8000,
"approach": "Realistic",
"classification": "Fullstack",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions frameworks/Rust/axum/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32> {
rng.sample_iter(Uniform::new(1, 10_001))
.take(count)
Expand Down
5 changes: 3 additions & 2 deletions frameworks/Rust/axum/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct Params {
queries: Option<String>,
q: Option<String>,
}

#[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)
Expand Down
9 changes: 6 additions & 3 deletions frameworks/Rust/axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
10 changes: 6 additions & 4 deletions frameworks/Rust/axum/src/main_sqlx.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod common;
mod sqlx;

use std::sync::Arc;

use ::sqlx::PgPool;
use axum::{
extract::{Query, State},
Expand Down Expand Up @@ -56,7 +58,7 @@ async fn queries(
let ids = random_ids(&mut rng, count);
let mut worlds: Vec<World> = 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())
Expand Down Expand Up @@ -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<Option<World>> = Vec::with_capacity(count);
let mut worlds: Vec<Option<Arc<World>>> = Vec::with_capacity(count);

for id in random_ids(&mut rng, count) {
worlds.push(cache.get(&id).await);
Expand All @@ -113,15 +115,15 @@ 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;
}
}

/// Application state
#[derive(Clone)]
struct AppState {
db: PgPool,
cache: Cache<i32, World>,
cache: Cache<i32, Arc<World>>,
}

#[tokio::main]
Expand Down
17 changes: 7 additions & 10 deletions frameworks/Rust/axum/src/pg/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,18 @@ impl PgConnection {
}

pub async fn update_worlds(&self, num: usize) -> Result<Vec<World>, 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<World> = 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
Expand Down
Loading