Parcourir la source

Clean up axum benchmarks (#7484)

* Reduce axum's activated feature set

Some of the default features are not free in terms of runtime, but not
used in the simple benchmark applications.

* axum: Sort dependencies

Automated with cargo-sort.

* axum: Upgrade dependencies

* axum: Fix inconsistent Cargo.toml formatting

* axum: Fix clippy lints

* axum: Rewrite parse_params

* axum: Run `cargo fmt`

* axum: Remove unnecessary extern crate's and direct dependency on async-trait

* axum: Remove unused dependencies

Found partially with cargo-udeps.

* axum: Remove direct dependency on axum-core
Jonas Platte il y a 3 ans
Parent
commit
485bc45d06

Fichier diff supprimé car celui-ci est trop grand
+ 131 - 371
frameworks/Rust/axum/Cargo.lock


+ 13 - 18
frameworks/Rust/axum/Cargo.toml

@@ -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

+ 1 - 0
frameworks/Rust/axum/src/database_mongo.rs

@@ -1,3 +1,4 @@
+use axum::async_trait;
 use axum::extract::{Extension, FromRequest, RequestParts};
 use axum::http::StatusCode;
 use futures_util::stream::FuturesUnordered;

+ 15 - 3
frameworks/Rust/axum/src/database_mongo_raw.rs

@@ -1,3 +1,4 @@
+use axum::async_trait;
 use axum::extract::{Extension, FromRequest, RequestParts};
 use axum::http::StatusCode;
 use futures_util::stream::FuturesUnordered;
@@ -5,7 +6,7 @@ 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;
 
@@ -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,
     })
 }
 

+ 2 - 3
frameworks/Rust/axum/src/database_pg.rs

@@ -1,3 +1,4 @@
+use axum::async_trait;
 use axum::extract::{Extension, FromRequest, RequestParts};
 use axum::http::StatusCode;
 use futures::{
@@ -200,8 +201,6 @@ where
             .await
             .map_err(internal_error)?;
 
-        let conn = pg_connection.clone();
-
-        Ok(Self(conn))
+        Ok(Self(pg_connection))
     }
 }

+ 1 - 0
frameworks/Rust/axum/src/database_pg_pool.rs

@@ -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};

+ 1 - 2
frameworks/Rust/axum/src/database_sqlx.rs

@@ -1,3 +1,4 @@
+use axum::async_trait;
 use axum::extract::{Extension, FromRequest, RequestParts};
 use axum::http::StatusCode;
 use std::io;
@@ -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)
 }
@@ -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)
 }

+ 0 - 6
frameworks/Rust/axum/src/main.rs

@@ -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;
 

+ 0 - 7
frameworks/Rust/axum/src/main_mongo.rs

@@ -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;
@@ -187,5 +182,3 @@ async fn serve() {
         .await
         .unwrap();
 }
-
-

+ 3 - 12
frameworks/Rust/axum/src/main_mongo_raw.rs

@@ -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;
@@ -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 {
@@ -158,5 +151,3 @@ async fn serve() {
         .await
         .unwrap();
 }
-
-

+ 0 - 5
frameworks/Rust/axum/src/main_pg.rs

@@ -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;

+ 0 - 7
frameworks/Rust/axum/src/main_pg_pool.rs

@@ -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;
@@ -167,5 +162,3 @@ async fn serve() {
         .await
         .unwrap();
 }
-
-

+ 3 - 8
frameworks/Rust/axum/src/main_sqlx.rs

@@ -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;
@@ -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,
+        ))
+}

+ 7 - 27
frameworks/Rust/axum/src/utils.rs

@@ -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;
@@ -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))
 }
@@ -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`

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff