Browse Source

[Rust] viz 0.4.9 (#8050)

* chore: clippy

* chore: remove cached queries

* chore: update deps

* chore: clean unused

* fix: build

* chore: viz 0.4.9

* chore: remove unused diesel
Fangdun Tsai 2 years ago
parent
commit
3fb684d140

+ 6 - 8
frameworks/Rust/viz/Cargo.toml

@@ -24,18 +24,17 @@ path = "src/main_diesel.rs"
 required-features = ["diesel", "diesel-async", "sailfish"]
 
 [dependencies]
-viz = "0.4.8"
-hyper = "0.14.24"
+viz = "0.4.9"
+hyper = "0.14.25"
 atoi = "2.0"
-once_cell = "1.17.1"
 serde = { version = "1.0", features = ["derive"] }
 nanorand = "0.7"
 thiserror = "1.0"
-futures-util = "0.3.26"
+futures-util = "0.3.27"
 
-tokio = { version = "1.25.0", features = ["full"] }
+tokio = { version = "1.26", features = ["full"] }
 tokio-postgres = { version = "0.7.7", optional = true }
-sqlx = { version = "0.6.2", features = [
+sqlx = { version = "0.6.3", features = [
   "postgres",
   "macros",
   "runtime-tokio-native-tls",
@@ -43,11 +42,10 @@ sqlx = { version = "0.6.2", features = [
 diesel = { version = "2.0.3", default-features = false, features = [
   "i-implement-a-third-party-backend-and-opt-into-breaking-changes",
 ], optional = true }
-diesel-async = { version = "0.2.0", default-features = false, features = [
+diesel-async = { version = "0.2.1", default-features = false, features = [
   "postgres",
   "bb8",
 ], optional = true }
-# diesel = { version = "1.4.8", features = ["postgres"], optional = true }
 
 yarte = { version = "0.15.7", features = ["bytes-buf", "json"], optional = true }
 markup = { version = "0.13.1", optional = true }

+ 0 - 3
frameworks/Rust/viz/benchmark_config.json

@@ -26,7 +26,6 @@
         "fortune_url": "/fortunes",
         "query_url": "/queries?q=",
         "update_url": "/updates?q=",
-        "cached_query_url": "/cached_queries?q=",
         "port": 8080,
         "approach": "Realistic",
         "classification": "Fullstack",
@@ -48,7 +47,6 @@
         "fortune_url": "/fortunes",
         "query_url": "/queries?q=",
         "update_url": "/updates?q=",
-        "cached_query_url": "/cached_queries?q=",
         "port": 8080,
         "approach": "Realistic",
         "classification": "Fullstack",
@@ -70,7 +68,6 @@
         "fortune_url": "/fortunes",
         "query_url": "/queries?q=",
         "update_url": "/updates?q=",
-        "cached_query_url": "/cached_queries?q=",
         "port": 8080,
         "approach": "Realistic",
         "classification": "Fullstack",

+ 0 - 12
frameworks/Rust/viz/src/db_diesel.rs

@@ -38,18 +38,6 @@ impl IntoResponse for PgError {
     }
 }
 
-pub async fn get_worlds_by_limit(
-    pool: Pool<AsyncPgConnection>,
-    limit: i64,
-) -> Result<Vec<World>, PgError> {
-    let mut conn = pool.get().await?;
-    let worlds = world::table
-        .limit(limit)
-        .get_results::<World>(&mut conn)
-        .await?;
-    Ok(worlds)
-}
-
 async fn _get_world(conn: &mut AsyncPgConnection, id: i32) -> Result<World, PgError> {
     let world = world::table.find(id).first(conn).await?;
     Ok(world)

+ 0 - 13
frameworks/Rust/viz/src/db_pg.rs

@@ -122,19 +122,6 @@ impl PgConnection {
         worlds.try_collect().await
     }
 
-    pub async fn get_worlds_by_limit(&self, limit: i64) -> Result<Vec<World>, PgError> {
-        Ok(self
-            .client
-            .query("SELECT * FROM world LIMIT $1", &[&limit])
-            .await?
-            .iter()
-            .map(|row| World {
-                id: row.get(0),
-                randomnumber: row.get(1),
-            })
-            .collect())
-    }
-
     pub async fn update(&self, num: u16) -> Result<Vec<World>, PgError> {
         let mut rng = self.rng.clone();
 

+ 0 - 11
frameworks/Rust/viz/src/db_sqlx.rs

@@ -62,17 +62,6 @@ impl FromRequest for Counter {
     }
 }
 
-pub async fn get_worlds_by_limit(
-    mut conn: PoolConnection<Postgres>,
-    limit: i64,
-) -> Result<Vec<World>, PgError> {
-    let worlds = sqlx::query_as("SELECT * FROM World LIMIT $1")
-        .bind(limit)
-        .fetch_all(&mut conn)
-        .await?;
-    Ok(worlds)
-}
-
 pub async fn get_world(
     conn: &mut PoolConnection<Postgres>,
     id: i32,

+ 2 - 0
frameworks/Rust/viz/src/main.rs

@@ -1,3 +1,5 @@
+#![allow(clippy::unused_async)]
+
 use serde::Serialize;
 use viz::{
     header::{HeaderValue, SERVER},

+ 1 - 33
frameworks/Rust/viz/src/main_diesel.rs

@@ -1,14 +1,13 @@
 #[macro_use]
 extern crate diesel;
 
-use std::{convert::identity, thread::available_parallelism};
+use std::thread::available_parallelism;
 
 use diesel_async::{
     pooled_connection::{bb8::Pool, AsyncDieselConnectionManager},
     AsyncPgConnection,
 };
 use nanorand::{Rng, WyRand};
-use once_cell::sync::OnceCell;
 use viz::{
     header::{HeaderValue, SERVER},
     types::State,
@@ -22,12 +21,10 @@ mod server;
 mod utils;
 
 use db_diesel::*;
-use models_diesel::World;
 use utils::RANGE;
 
 const DB_URL: &str =
     "postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";
-static CACHED: OnceCell<Vec<World>> = OnceCell::new();
 
 async fn db(req: Request) -> Result<Response> {
     let mut rng = req.state::<WyRand>().unwrap();
@@ -67,24 +64,6 @@ async fn queries(req: Request) -> Result<Response> {
     Ok(res)
 }
 
-async fn cached_queries(req: Request) -> Result<Response> {
-    let count = utils::get_query_param(req.query_string());
-    let mut rng = WyRand::new();
-
-    let worlds = (0..count)
-        .map(|_| {
-            let id = rng.generate_range(RANGE) as usize;
-            CACHED.get()?.get(id)
-        })
-        .filter_map(identity)
-        .collect::<Vec<_>>();
-
-    let mut res = Response::json(worlds)?;
-    res.headers_mut()
-        .insert(SERVER, HeaderValue::from_static("Viz"));
-    Ok(res)
-}
-
 async fn updates(req: Request) -> Result<Response> {
     let rng = req.state::<WyRand>().unwrap();
     let pool = req.state::<Pool<AsyncPgConnection>>().unwrap();
@@ -98,12 +77,6 @@ async fn updates(req: Request) -> Result<Response> {
     Ok(res)
 }
 
-async fn populate_cache(pool: Pool<AsyncPgConnection>) -> Result<()> {
-    let worlds = get_worlds_by_limit(pool, 10_000).await?;
-    CACHED.set(worlds).unwrap();
-    Ok(())
-}
-
 #[tokio::main]
 async fn main() {
     let max = available_parallelism().map(|n| n.get()).unwrap_or(16) as u32;
@@ -114,10 +87,6 @@ async fn main() {
         .idle_timeout(None)
         .build_unchecked(AsyncDieselConnectionManager::new(DB_URL));
 
-    populate_cache(pool.clone())
-        .await
-        .expect("cache insert failed");
-
     let rng = WyRand::new();
 
     let service = ServiceMaker::from(
@@ -127,7 +96,6 @@ async fn main() {
             .get("/queries", queries)
             .get("/updates", updates)
             .with(State::new(pool))
-            .get("/cached_queries", cached_queries)
             .with(State::new(rng)),
     );
 

+ 1 - 34
frameworks/Rust/viz/src/main_pg.rs

@@ -1,11 +1,8 @@
 use std::{
-    convert::identity,
     sync::Arc,
     thread::{available_parallelism, spawn},
 };
 
-use nanorand::{Rng, WyRand};
-use once_cell::sync::OnceCell;
 use viz::{
     header::{HeaderValue, SERVER},
     types::State,
@@ -19,11 +16,9 @@ mod server;
 mod utils;
 
 use db_pg::{get_conn, PgConnection};
-use utils::RANGE;
 
 const DB_URL: &str =
     "postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";
-static CACHED: OnceCell<Vec<models::World>> = OnceCell::new();
 
 async fn db(req: Request) -> Result<Response> {
     let conn = get_conn(req.state::<Arc<PgConnection>>())?;
@@ -62,24 +57,6 @@ async fn queries(req: Request) -> Result<Response> {
     Ok(res)
 }
 
-async fn cached_queries(req: Request) -> Result<Response> {
-    let count = utils::get_query_param(req.query_string());
-    let mut rng = WyRand::new();
-
-    let worlds = (0..count)
-        .map(|_| {
-            let id = rng.generate_range(RANGE) as usize;
-            CACHED.get()?.get(id)
-        })
-        .filter_map(identity)
-        .collect::<Vec<_>>();
-
-    let mut res = Response::json(worlds)?;
-    res.headers_mut()
-        .insert(SERVER, HeaderValue::from_static("Viz"));
-    Ok(res)
-}
-
 async fn updates(req: Request) -> Result<Response> {
     let count = utils::get_query_param(req.query_string());
     let conn = get_conn(req.state::<Arc<PgConnection>>())?;
@@ -92,21 +69,12 @@ async fn updates(req: Request) -> Result<Response> {
     Ok(res)
 }
 
-async fn populate_cache() -> Result<()> {
-    let conn = PgConnection::connect(DB_URL).await;
-    let worlds = conn.get_worlds_by_limit(10_000).await?;
-    CACHED.set(worlds).unwrap();
-    Ok(())
-}
-
 fn main() {
     let rt = tokio::runtime::Builder::new_current_thread()
         .enable_all()
         .build()
         .unwrap();
 
-    rt.block_on(populate_cache()).expect("cache insert failed");
-
     for _ in 1..available_parallelism().map(|n| n.get()).unwrap_or(16) {
         spawn(move || {
             let rt = tokio::runtime::Builder::new_current_thread()
@@ -128,8 +96,7 @@ async fn serve() {
         .get("/fortunes", fortunes)
         .get("/queries", queries)
         .get("/updates", updates)
-        .with(State::new(Arc::new(conn)))
-        .get("/cached_queries", cached_queries);
+        .with(State::new(Arc::new(conn)));
 
     server::builder()
         .serve(ServiceMaker::from(app))

+ 1 - 33
frameworks/Rust/viz/src/main_sqlx.rs

@@ -1,8 +1,6 @@
-use std::{convert::identity, thread::available_parallelism};
+use std::thread::available_parallelism;
 
 use nanorand::{Rng, WyRand};
-use once_cell::sync::OnceCell;
-use sqlx::Pool;
 use viz::{
     header::{HeaderValue, SERVER},
     types::State,
@@ -21,7 +19,6 @@ use utils::RANGE;
 
 const DB_URL: &str =
     "postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";
-static CACHED: OnceCell<Vec<World>> = OnceCell::new();
 
 async fn db(mut req: Request) -> Result<Response> {
     let (State(mut rng), DatabaseConnection(mut conn)) =
@@ -70,24 +67,6 @@ async fn queries(mut req: Request) -> Result<Response> {
     Ok(res)
 }
 
-async fn cached_queries(mut req: Request) -> Result<Response> {
-    let (Counter(count), State(mut rng)) =
-        req.extract::<(Counter, State<WyRand>)>().await?;
-
-    let worlds = (0..count)
-        .map(|_| {
-            let id = rng.generate_range(RANGE) as usize;
-            CACHED.get()?.get(id)
-        })
-        .filter_map(identity)
-        .collect::<Vec<_>>();
-
-    let mut res = Response::json(worlds)?;
-    res.headers_mut()
-        .insert(SERVER, HeaderValue::from_static("Viz"));
-    Ok(res)
-}
-
 async fn updates(mut req: Request) -> Result<Response> {
     let (Counter(count), State(rng), DatabaseConnection(conn)) = req
         .extract::<(Counter, State<WyRand>, DatabaseConnection)>()
@@ -101,14 +80,6 @@ async fn updates(mut req: Request) -> Result<Response> {
     Ok(res)
 }
 
-async fn populate_cache(pool: Pool<Postgres>) -> Result<()> {
-    let conn = pool.acquire().await.map_err(Error::normal)?;
-    let worlds = get_worlds_by_limit(conn, 10_000).await?;
-    CACHED
-        .set(worlds)
-        .map_err(|_| PgError::from(sqlx::Error::RowNotFound).into())
-}
-
 #[tokio::main]
 async fn main() -> Result<()> {
     let max = available_parallelism().map(|n| n.get()).unwrap_or(16) as u32;
@@ -120,8 +91,6 @@ async fn main() -> Result<()> {
         .await
         .map_err(PgError)?;
 
-    populate_cache(pool.clone()).await?;
-
     let rng = WyRand::new();
 
     let app = Router::new()
@@ -130,7 +99,6 @@ async fn main() -> Result<()> {
         .get("/queries", queries)
         .get("/updates", updates)
         .with(State::new(pool))
-        .get("/cached_queries", cached_queries)
         .with(State::new(rng));
 
     server::builder()

+ 1 - 2
frameworks/Rust/viz/src/utils.rs

@@ -12,6 +12,5 @@ pub fn get_query_param(query: Option<&str>) -> u16 {
             s.find('q')
                 .map(|p| u16::from_radix_10(s.split_at(p + 2).1.as_ref()).0)
         })
-        .map(|n| n.clamp(1, 500))
-        .unwrap_or(1)
+        .map_or(1, |n| n.clamp(1, 500))
 }