Browse Source

Add Thruster benchmarks for Rust (#3888)

* Add Thruster examples

* Fix working directory for Thruster

* Remove bad copy clause from Dockerfile

* Add a README containing implemented tests

* Update to release version of Thruster
Isaac Whitfield 7 years ago
parent
commit
8aa6d33d70

+ 17 - 0
frameworks/Rust/thruster/Cargo.toml

@@ -0,0 +1,17 @@
+[package]
+name = "thruster_techempower"
+version = "0.1.0"
+authors = ["Isaac Whitfield <[email protected]>"]
+
+[dependencies]
+futures = "0.1"
+serde = "1.0"
+serde_derive = "1.0"
+serde_json = "1.0"
+thruster = "0.4"
+
+[profile.release]
+codegen-units = 1
+opt-level = 3
+lto = true
+

+ 15 - 0
frameworks/Rust/thruster/README.md

@@ -0,0 +1,15 @@
+# Thruster Benchmarking Test
+
+### Test Type Implementation Source Code
+
+* [JSON](src/main.rs)
+* [PLAINTEXT](src/main.rs)
+
+## Test URLs
+### JSON
+
+http://localhost:8080/json
+
+### PLAINTEXT
+
+http://localhost:8080/plaintext

+ 26 - 0
frameworks/Rust/thruster/benchmark_config.json

@@ -0,0 +1,26 @@
+{
+  "framework": "thruster",
+  "tests": [
+    {
+      "default": {
+        "json_url": "/json",
+        "plaintext_url": "/plaintext",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "Micro",
+        "database": "None",
+        "framework": "Thruster",
+        "language": "Rust",
+        "flavor": "None",
+        "orm": "None",
+        "platform": "Rust",
+        "webserver": "None",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "Thruster",
+        "notes": "",
+        "versus": "None"
+      }
+    }
+  ]
+}

+ 49 - 0
frameworks/Rust/thruster/src/context.rs

@@ -0,0 +1,49 @@
+use std::collections::HashMap;
+use thruster::{Context, Request, Response};
+
+pub struct Ctx {
+    pub body: String,
+    pub method: String,
+    pub path: String,
+    pub request_body: String,
+    pub params: HashMap<String, String>,
+    pub query_params: HashMap<String, String>,
+    pub headers: Vec<(String, String)>,
+    pub status_code: u32,
+}
+
+impl Context for Ctx {
+    fn get_response(&self) -> Response<String> {
+        let mut builder = Response::builder();
+        builder.status(200);
+
+        for header_pair in &self.headers {
+            let key: &str = &header_pair.0;
+            let val: &str = &header_pair.1;
+            builder.header(key, val);
+        }
+
+        builder.body(self.body.clone()).unwrap()
+    }
+
+    fn set_body(&mut self, body: String) {
+        self.body = body;
+    }
+}
+
+pub fn generate_context(request: Request) -> Ctx {
+    let method = request.method().to_owned();
+    let path = request.path().to_owned();
+    let request_body = request.raw_body().to_owned();
+
+    Ctx {
+        body: "".to_owned(),
+        method: method,
+        path: path,
+        params: request.params,
+        query_params: request.query_params,
+        request_body: request_body,
+        headers: Vec::new(),
+        status_code: 200,
+    }
+}

+ 61 - 0
frameworks/Rust/thruster/src/main.rs

@@ -0,0 +1,61 @@
+extern crate futures;
+extern crate serde;
+#[macro_use]
+extern crate serde_derive;
+extern crate serde_json;
+extern crate thruster;
+
+mod context;
+
+use futures::future;
+
+use context::{generate_context, Ctx};
+use thruster::{App, MiddlewareChain, MiddlewareReturnValue};
+
+#[derive(Serialize)]
+struct Message<'a> {
+    message: &'a str,
+}
+
+fn json(mut context: Ctx, _chain: &MiddlewareChain<Ctx>) -> MiddlewareReturnValue<Ctx> {
+    let json = Message {
+        message: "Hello, World!",
+    };
+
+    let val = serde_json::to_string(&json).unwrap();
+
+    context.body = val;
+    context
+        .headers
+        .push(("Server".to_owned(), "thruster".to_owned()));
+    context
+        .headers
+        .push(("Content-Type".to_owned(), "application/json".to_owned()));
+
+    Box::new(future::ok(context))
+}
+
+fn plaintext(mut context: Ctx, _chain: &MiddlewareChain<Ctx>) -> MiddlewareReturnValue<Ctx> {
+    let val = "Hello, world!".to_owned();
+
+    context.body = val;
+    context
+        .headers
+        .push(("Server".to_owned(), "thruster".to_owned()));
+    context
+        .headers
+        .push(("Content-Type".to_owned(), "text/plain".to_owned()));
+
+    Box::new(future::ok(context))
+}
+
+fn main() {
+    println!("Starting server...");
+
+    let mut app = App::create(generate_context);
+
+    app.get("/json", vec![json]);
+    app.get("/plaintext", vec![plaintext]);
+
+    App::start(app, "0.0.0.0", 8080);
+}

+ 10 - 0
frameworks/Rust/thruster/thruster.dockerfile

@@ -0,0 +1,10 @@
+FROM rust:1.26
+
+WORKDIR /thruster
+COPY ./src ./src
+COPY ./Cargo.toml ./Cargo.toml
+
+ENV RUSTFLAGS "-C target-cpu=native"
+RUN cargo build --release
+
+CMD ["./target/release/thruster_techempower"]