Browse Source

Add single database query test for warp-rust (#5411)

Konrad Borowski 5 years ago
parent
commit
e2b0c5729d

+ 2 - 0
frameworks/Rust/warp-rust/Cargo.toml

@@ -5,6 +5,8 @@ authors = ["Konrad Borowski <[email protected]>"]
 edition = "2018"
 
 [dependencies]
+rand = "0.7.3"
 serde = { version = "1.0.103", features = ["derive"] }
 tokio = { version = "0.2.9", features = ["macros"] }
+tokio-postgres = "0.5.1"
 warp = "0.2.0"

+ 5 - 0
frameworks/Rust/warp-rust/README.md

@@ -8,6 +8,7 @@ warp is a composable web server framework based on hyper.
 
 * [JSON](src/main.rs)
 * [PLAINTEXT](src/main.rs)
+* [DB](src/main.rs)
 
 ## Test URLs
 ### JSON
@@ -17,3 +18,7 @@ http://localhost:8080/json
 ### PLAINTEXT
 
 http://localhost:8080/plaintext
+
+### DB
+
+http://localhost:8080/db

+ 3 - 2
frameworks/Rust/warp-rust/benchmark_config.json

@@ -5,14 +5,15 @@
       "default": {
         "json_url": "/json",
         "plaintext_url": "/plaintext",
+        "db_url": "/db",
         "port": 8080,
         "approach": "Realistic",
         "classification": "Micro",
-        "database": "None",
+        "database": "Postgres",
         "framework": "warp-rust",
         "language": "Rust",
         "flavor": "None",
-        "orm": "None",
+        "orm": "Raw",
         "platform": "Rust",
         "webserver": "Hyper",
         "os": "Linux",

+ 34 - 0
frameworks/Rust/warp-rust/src/db.rs

@@ -0,0 +1,34 @@
+use serde::Serialize;
+use tokio_postgres::{Client, Error, NoTls, Statement};
+
+const DATABASE_URL: &str = "postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";
+
+pub struct Database {
+    client: Client,
+    world: Statement,
+}
+
+#[derive(Serialize)]
+pub struct World {
+    pub id: i32,
+    pub randomnumber: i32,
+}
+
+impl Database {
+    pub async fn connect() -> Result<Self, Error> {
+        let (client, connection) = tokio_postgres::connect(DATABASE_URL, NoTls).await?;
+        tokio::spawn(async { connection.await.unwrap() });
+        let world = client
+            .prepare("SELECT id, randomnumber FROM world WHERE id=$1")
+            .await?;
+        Ok(Self { client, world })
+    }
+
+    pub async fn get_world_by_id(&self, id: i32) -> World {
+        let row = self.client.query_one(&self.world, &[&id]).await.unwrap();
+        World {
+            id: row.get(0),
+            randomnumber: row.get(1),
+        }
+    }
+}

+ 41 - 10
frameworks/Rust/warp-rust/src/main.rs

@@ -1,22 +1,53 @@
+mod db;
+
+use crate::db::Database;
+use rand::distributions::{Distribution, Uniform};
 use serde::Serialize;
 use warp::http::header;
-use warp::Filter;
+use warp::{Filter, Rejection, Reply};
 
 #[derive(Serialize)]
 struct Message {
     message: &'static str,
 }
 
-#[tokio::main]
-async fn main() {
-    let json = warp::path!("json").map(|| {
+fn json() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
+    warp::path!("json").map(|| {
         warp::reply::json(&Message {
             message: "Hello, world!",
         })
-    });
-    let plaintext = warp::path!("plaintext").map(|| "Hello, World!");
-    let routes = json
-        .or(plaintext)
-        .map(|reply| warp::reply::with_header(reply, header::SERVER, "warp"));
-    warp::serve(routes).run(([0, 0, 0, 0], 8080)).await;
+    })
+}
+
+fn plaintext() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
+    warp::path!("plaintext").map(|| "Hello, World!")
+}
+
+fn db(database: &'static Database) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
+    let between = Uniform::from(1..=10_000);
+    warp::path!("db").and_then(move || {
+        async move {
+            let id = between.sample(&mut rand::thread_rng());
+            let world = database.get_world_by_id(id).await;
+            Ok::<_, Rejection>(warp::reply::json(&world))
+        }
+    })
+}
+
+fn routes(
+    database: &'static Database,
+) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
+    json()
+        .or(plaintext())
+        .or(db(database))
+        .map(|reply| warp::reply::with_header(reply, header::SERVER, "warp"))
+}
+
+#[tokio::main]
+async fn main() -> Result<(), tokio_postgres::Error> {
+    let database = Box::leak(Box::new(Database::connect().await?));
+    warp::serve(routes(database))
+        .run(([0, 0, 0, 0], 8080))
+        .await;
+    Ok(())
 }

+ 1 - 1
frameworks/Rust/warp-rust/warp-rust.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.39
+FROM rust:1.40
 
 WORKDIR /warp-rust
 COPY src src