Skip to content

Commit

Permalink
Salvo 0.38 and use moka for cache (TechEmpower#8110)
Browse files Browse the repository at this point in the history
* salvo-0.38

* salvo 0.38

* Use moka library for cache

* change server header TechEmpower#8060

* Update config file

* Use fixed rust version and remove diesel cache test

* remove rev = "f14f819"

* Use static HeaderValue

* Fix salvo-pg error
  • Loading branch information
chrislearn authored and franz1981 committed Jun 23, 2023
1 parent 0501e87 commit f938324
Show file tree
Hide file tree
Showing 14 changed files with 512 additions and 358 deletions.
23 changes: 14 additions & 9 deletions frameworks/Rust/salvo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ path = "src/main_diesel.rs"
name = "main-pg"
path = "src/main_pg.rs"

[[bin]]
name = "main-moka"
path = "src/main_moka.rs"

[dependencies]
anyhow = "1"
async-trait = "0.1"
bytes = "1"
diesel = { version = "1.4", features = ["postgres", "r2d2"] }
futures = "0.3"
diesel = { version = "2", features = ["postgres", "r2d2"] }
futures-util = "0.3"
moka = "0.10"
markup = "0.13"
# mimalloc = { version = "0.1", default-features = false }
mimalloc = { version = "0.1", default-features = false }
once_cell = "1"
rand = { version = "0.8", features = ["min_const_gen", "small_rng"] }
random-fast-rng = "0.1"
salvo = { version = "0.34", default-features = false, features = ["anyhow"] }
salvo = { version = "0.38", default-features = false, features = ["anyhow", "http1"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
smallvec = "1"
snmalloc-rs = { version = "0.3", features = ["native-cpu"] }
# smallvec = "1"
# snmalloc-rs = { version = "0.3", features = ["native-cpu"] }
tokio = { version = "1", features = ["macros", "rt"] }
tokio-postgres = "0.7"
v_htmlescape = "0.15"
Expand All @@ -40,5 +44,6 @@ lto = true
opt-level = 3
codegen-units = 1
panic = "abort"
debug = false
incremental = false

[patch.crates-io]
salvo = { git = "https://github.com/salvo-rs/salvo.git" }
21 changes: 18 additions & 3 deletions frameworks/Rust/salvo/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "Micro",
Expand All @@ -38,7 +37,7 @@
"webserver": "Hyper",
"os": "Linux",
"database_os": "Linux",
"display_name": "Salvo [Diesel]",
"display_name": "Salvo [diesel]",
"notes": "",
"versus": ""
},
Expand All @@ -47,6 +46,22 @@
"fortune_url": "/fortunes",
"query_url": "/queries?q=",
"update_url": "/updates?q=",
"port": 8080,
"approach": "Realistic",
"classification": "Micro",
"database": "Postgres",
"framework": "salvo",
"language": "Rust",
"orm": "Raw",
"platform": "Rust",
"webserver": "Hyper",
"os": "Linux",
"database_os": "Linux",
"display_name": "Salvo [pg]",
"notes": "",
"versus": ""
},
"moka": {
"cached_query_url": "/cached_queries?q=",
"port": 8080,
"approach": "Realistic",
Expand All @@ -59,7 +74,7 @@
"webserver": "Hyper",
"os": "Linux",
"database_os": "Linux",
"display_name": "Salvo [PG]",
"display_name": "Salvo [moka]",
"notes": "",
"versus": ""
}
Expand Down
8 changes: 3 additions & 5 deletions frameworks/Rust/salvo/salvo-diesel.dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
FROM rust:1.62.1

RUN apt-get update -yqq && apt-get install -yqq cmake g++
FROM rust:1.68.2

ADD ./ /salvo
WORKDIR /salvo

RUN cargo clean
RUN RUSTFLAGS="-C target-cpu=native" cargo build --release
ENV RUSTFLAGS "-C target-cpu=native"
RUN cargo build --release

EXPOSE 8080

Expand Down
11 changes: 11 additions & 0 deletions frameworks/Rust/salvo/salvo-moka.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM rust:1.68.2

ADD ./ /salvo
WORKDIR /salvo

ENV RUSTFLAGS "-C target-cpu=native"
RUN cargo build --release

EXPOSE 8080

CMD ./target/release/main-moka
8 changes: 3 additions & 5 deletions frameworks/Rust/salvo/salvo-pg.dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
FROM rust:1.62.1

RUN apt-get update -yqq && apt-get install -yqq cmake g++
FROM rust:1.68.2

ADD ./ /salvo
WORKDIR /salvo

RUN cargo clean
RUN RUSTFLAGS="-C target-cpu=native" cargo build --release
ENV RUSTFLAGS "-C target-cpu=native"
RUN cargo build --release

EXPOSE 8080

Expand Down
11 changes: 3 additions & 8 deletions frameworks/Rust/salvo/salvo.dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
FROM rust:1.62.1

# Disable simd at jsonescape
ENV CARGO_CFG_JSONESCAPE_DISABLE_AUTO_SIMD=

RUN apt-get update -yqq && apt-get install -yqq cmake g++
FROM rust:1.68.2

ADD ./ /salvo
WORKDIR /salvo

RUN cargo clean
RUN RUSTFLAGS="-C target-cpu=native" cargo build --release
ENV RUSTFLAGS "-C target-cpu=native"
RUN cargo build --release

EXPOSE 8080

Expand Down
57 changes: 38 additions & 19 deletions frameworks/Rust/salvo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// #[global_allocator]
// static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use std::sync::Arc;
use std::thread::available_parallelism;

use bytes::Bytes;
use salvo::conn::tcp::TcpAcceptor;
use salvo::http::body::ResBody;
use salvo::http::header::{self, HeaderValue};
use salvo::http::response::Body;
use salvo::prelude::*;
use serde::Serialize;

mod server;
mod utils;

static SERVER_HEADER: HeaderValue = HeaderValue::from_static("salvo");
static JSON_HEADER: HeaderValue = HeaderValue::from_static("application/json");
static PLAIN_HEADER: HeaderValue = HeaderValue::from_static("text/plain");

#[derive(Serialize)]
pub struct Message {
Expand All @@ -19,37 +25,50 @@ pub struct Message {
#[handler]
fn json(res: &mut Response) {
let headers = res.headers_mut();
headers.insert(header::SERVER, HeaderValue::from_static("S"));
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/json"),
);
headers.insert(header::SERVER, SERVER_HEADER.clone());
headers.insert(header::CONTENT_TYPE, JSON_HEADER.clone());
let data = serde_json::to_vec(&Message {
message: "Hello, World!",
})
.unwrap();
res.set_body(Body::Once(Bytes::from(data)));
res.set_body(ResBody::Once(Bytes::from(data)));
}

#[handler]
fn plaintext(res: &mut Response) {
let headers = res.headers_mut();
headers.insert(header::SERVER, HeaderValue::from_static("S"));
headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/plain"));
res.set_body(Body::Once(Bytes::from_static(b"Hello, world!")));
headers.insert(header::SERVER, SERVER_HEADER.clone());
headers.insert(header::CONTENT_TYPE, PLAIN_HEADER.clone());
res.set_body(ResBody::Once(Bytes::from_static(b"Hello, world!")));
}

#[tokio::main]
async fn main() {
fn main() {
let size = available_parallelism().map(|n| n.get()).unwrap_or(16);
let router = Arc::new(
Router::new()
.push(Router::with_path("plaintext").get(plaintext))
.push(Router::with_path("json").get(json)),
);

server::builder()
.http1_pipeline_flush(true)
.serve(Service::new(router))
.await
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
for _ in 1..size {
let router = router.clone();
std::thread::spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(serve(router));
});
}
println!("Started http server: 127.0.0.1:8080");
rt.block_on(serve(router));
}

async fn serve(router: Arc<Router>) {
let acceptor: TcpAcceptor = utils::reuse_listener().unwrap().try_into().unwrap();
Server::new(acceptor).serve(router).await
}

0 comments on commit f938324

Please sign in to comment.