Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up axum benchmarks #7484

Merged
merged 10 commits into from
Jul 29, 2022
Merged
821 changes: 267 additions & 554 deletions frameworks/Rust/axum/Cargo.lock

Large diffs are not rendered by default.

31 changes: 13 additions & 18 deletions frameworks/Rust/axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,26 @@ name = "axum-pg"
path = "src/main_pg.rs"

[dependencies]
num_cpus = "1.13.1"
rand = { version = "0.8.5", features = ["small_rng"] }
yarte = "0.15.6"
async-stream = "0.3.3"
async-trait = "0.1.53"
async-std = "1.11.0"
axum = { version = "0.5.1", default-features = false, features = ["json", "query"] }
deadpool = { version = "0.9.3", features = ["rt_tokio_1", "serde", "async-trait", "managed" ] }
deadpool-postgres = "0.10.2"
dotenv = "0.15.0"
futures = "0.3.21"
futures-util = "0.3.21"
dotenv = "0.15.0"
hyper = { version = "0.14.18", features = ["http1"] }
mongodb = { version = "2.2.1", features = ["zstd-compression", "snappy-compression", "zlib-compression"] }
num_cpus = "1.13.1"
rand = { version = "0.8.5", features = ["small_rng"] }
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
serde_derive = "1.0.136"
axum = "0.5.1"
sqlx = { version = "0.6.0", features = ["postgres", "macros", "runtime-tokio-native-tls"] }
tokio = { version = "1.17.0", features = ["full"] }
hyper = "0.14.18"
tower = { version = "0.4.12", features = ["util"] }
tower-http = { version = "0.2.5", features = ["set-header"] }
sqlx = { version = "0.5.12", features = [ "postgres", "macros", "runtime-tokio-native-tls" ] }
tokio-postgres = "0.7.5"
tokio-pg-mapper = "0.2.0"
tokio-pg-mapper-derive = "0.2.0"
mongodb = { version = "2.2.1", features = [ "zstd-compression", "snappy-compression", "zlib-compression" ] }
axum-core = "0.2.2"
deadpool = { version = "0.9.3", features = ["rt_tokio_1", "serde", "async-trait", "managed" ] }
deadpool-postgres = "0.10.2"
tokio-postgres = "0.7.5"
tower = { version = "0.4.12", features = ["util"] }
tower-http = { version = "0.3.4", features = ["set-header"] }
yarte = "0.15.6"

[profile.release]
lto = true
Expand Down
1 change: 1 addition & 0 deletions frameworks/Rust/axum/src/database_mongo.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use axum::async_trait;
use axum::extract::{Extension, FromRequest, RequestParts};
use axum::http::StatusCode;
use futures_util::stream::FuturesUnordered;
Expand Down
18 changes: 15 additions & 3 deletions frameworks/Rust/axum/src/database_mongo_raw.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use axum::async_trait;
use axum::extract::{Extension, FromRequest, RequestParts};
use axum::http::StatusCode;
use futures_util::stream::FuturesUnordered;
use futures_util::TryStreamExt;
use std::io;

use crate::utils::internal_error;
use crate::{World};
use crate::World;
use mongodb::bson::{doc, RawDocumentBuf};
use mongodb::Database;

Expand Down Expand Up @@ -57,8 +58,19 @@ pub async fn find_world_by_id(db: Database, id: i32) -> Result<World, MongoError
.expect("expected world, found none");

Ok(World {
id: raw.get("id").expect("expected to parse world id").expect("could not get world id").as_f64().expect("could not extract world id") as f32,
random_number: raw.get("randomNumber").expect("expected to parse world randomNumber").expect("expected to get world randomNumber").as_f64().expect("could not extract world randomNumber") as f32,
id: raw
.get("id")
.expect("expected to parse world id")
.expect("could not get world id")
.as_f64()
.expect("could not extract world id") as f32,
random_number: raw
.get("randomNumber")
.expect("expected to parse world randomNumber")
.expect("expected to get world randomNumber")
.as_f64()
.expect("could not extract world randomNumber")
as f32,
})
}

Expand Down
5 changes: 2 additions & 3 deletions frameworks/Rust/axum/src/database_pg.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use axum::async_trait;
use axum::extract::{Extension, FromRequest, RequestParts};
use axum::http::StatusCode;
use futures::{
Expand Down Expand Up @@ -200,8 +201,6 @@ where
.await
.map_err(internal_error)?;

let conn = pg_connection.clone();

Ok(Self(conn))
Ok(Self(pg_connection))
}
}
1 change: 1 addition & 0 deletions frameworks/Rust/axum/src/database_pg_pool.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use axum::async_trait;
use axum::extract::{Extension, FromRequest, RequestParts};
use axum::http::StatusCode;
use deadpool_postgres::{Client, Manager, ManagerConfig, RecyclingMethod};
Expand Down
3 changes: 1 addition & 2 deletions frameworks/Rust/axum/src/database_sqlx.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use axum::async_trait;
use axum::extract::{Extension, FromRequest, RequestParts};
use axum::http::StatusCode;
use std::io;
Expand Down Expand Up @@ -70,7 +71,6 @@ pub async fn fetch_world(
sqlx::query_as_with("SELECT id, randomnumber FROM World WHERE id = $1", args)
.fetch_one(&mut conn)
.await
.ok()
.expect("error loading world");
Ok(world)
}
Expand All @@ -81,7 +81,6 @@ pub async fn fetch_fortunes(
let fortunes: Vec<Fortune> = sqlx::query_as("SELECT * FROM Fortune")
.fetch_all(&mut conn)
.await
.ok()
.expect("error loading Fortunes");
Ok(fortunes)
}
6 changes: 0 additions & 6 deletions frameworks/Rust/axum/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
extern crate async_trait;
extern crate dotenv;
extern crate serde_derive;
extern crate tokio_pg_mapper;
extern crate tokio_pg_mapper_derive;

mod models_common;
mod server;

Expand Down
7 changes: 0 additions & 7 deletions frameworks/Rust/axum/src/main_mongo.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
extern crate dotenv;
extern crate serde_derive;
#[macro_use]
extern crate async_trait;

mod database_mongo;
mod models_common;
mod models_mongo;
Expand Down Expand Up @@ -187,5 +182,3 @@ async fn serve() {
.await
.unwrap();
}


15 changes: 3 additions & 12 deletions frameworks/Rust/axum/src/main_mongo_raw.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
extern crate dotenv;
extern crate serde_derive;
#[macro_use]
extern crate async_trait;

mod database_mongo_raw;
mod models_common;
mod models_mongo;
Expand All @@ -21,12 +16,10 @@ use rand::{rngs::SmallRng, thread_rng, Rng, SeedableRng};
use std::time::Duration;
use tower_http::set_header::SetResponseHeaderLayer;

use database_mongo_raw::{
find_world_by_id, find_worlds, update_worlds,
};
use utils::get_environment_variable;
use database_mongo_raw::DatabaseConnection;
use models_mongo::{World};
use database_mongo_raw::{find_world_by_id, find_worlds, update_worlds};
use models_mongo::World;
use utils::get_environment_variable;
use utils::{parse_params, Params};

async fn db(DatabaseConnection(db): DatabaseConnection) -> impl IntoResponse {
Expand Down Expand Up @@ -158,5 +151,3 @@ async fn serve() {
.await
.unwrap();
}


5 changes: 0 additions & 5 deletions frameworks/Rust/axum/src/main_pg.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
extern crate dotenv;
extern crate serde_derive;
#[macro_use]
extern crate async_trait;

mod database_pg;
mod models_common;
mod models_pg;
Expand Down
7 changes: 0 additions & 7 deletions frameworks/Rust/axum/src/main_pg_pool.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
extern crate dotenv;
extern crate serde_derive;
#[macro_use]
extern crate async_trait;

mod database_pg_pool;
mod models_common;
mod models_pg_pool;
Expand Down Expand Up @@ -167,5 +162,3 @@ async fn serve() {
.await
.unwrap();
}


11 changes: 3 additions & 8 deletions frameworks/Rust/axum/src/main_sqlx.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
extern crate dotenv;
extern crate serde_derive;
#[macro_use]
extern crate async_trait;

mod database_sqlx;
mod models_common;
mod models_sqlx;
Expand Down Expand Up @@ -94,6 +89,6 @@ async fn router(pool: PgPool) -> Router {
.layer(Extension(pool))
.layer(SetResponseHeaderLayer::if_not_present(
header::SERVER,
server_header_value)
)
}
server_header_value,
))
}
34 changes: 7 additions & 27 deletions frameworks/Rust/axum/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use axum::body::{Bytes, Full};
use axum::http::{header, HeaderValue, StatusCode};
use axum_core::response::IntoResponse;
use axum_core::response::Response;
use axum::response::{IntoResponse, Response};
use rand::rngs::SmallRng;
use rand::Rng;
use serde::Deserialize;
Expand All @@ -15,9 +14,7 @@ where
<T as FromStr>::Err: Debug,
{
T::from_str(
&*env::var(key)
.ok()
.expect(&*format!("{} environment variable was not set", key)),
&*env::var(key).expect(&*format!("{} environment variable was not set", key)),
)
.expect(&*format!("could not parse {}", key))
}
Expand All @@ -34,28 +31,11 @@ pub fn random_number(rng: &mut SmallRng) -> i32 {

#[allow(dead_code)]
pub fn parse_params(params: Params) -> i32 {
let mut q = 0;

if params.queries.is_some() {
let queries = params.queries.ok_or("could not get value").unwrap();

let queries_as_int = queries.parse::<i32>();

match queries_as_int {
Ok(_ok) => q = queries_as_int.unwrap(),
Err(_e) => q = 1,
}
}

let q = if q == 0 {
1
} else if q > 500 {
500
} else {
q
};

q
params
.queries
.and_then(|q| q.parse().ok())
.unwrap_or(1)
.clamp(1, 500)
}

/// Utility function for mapping any error into a `500 Internal Server Error`
Expand Down