From 787b344d05fc3502a318826efb6efbceffe109f2 Mon Sep 17 00:00:00 2001 From: Pooya Eimandar Date: Wed, 25 Jun 2025 19:53:35 +0400 Subject: [PATCH 1/2] Add benchmark tests for the Sib --- frameworks/Rust/sib/Cargo.toml | 24 ++++++ frameworks/Rust/sib/README.md | 16 ++++ frameworks/Rust/sib/benchmark_config.json | 24 ++++++ frameworks/Rust/sib/sib.dockerfile | 11 +++ frameworks/Rust/sib/src/main.rs | 91 +++++++++++++++++++++++ 5 files changed, 166 insertions(+) create mode 100644 frameworks/Rust/sib/Cargo.toml create mode 100644 frameworks/Rust/sib/README.md create mode 100644 frameworks/Rust/sib/benchmark_config.json create mode 100644 frameworks/Rust/sib/sib.dockerfile create mode 100644 frameworks/Rust/sib/src/main.rs diff --git a/frameworks/Rust/sib/Cargo.toml b/frameworks/Rust/sib/Cargo.toml new file mode 100644 index 00000000000..15dc4aa0b09 --- /dev/null +++ b/frameworks/Rust/sib/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "sib-techempower" +version = "0.0.1" +authors = ["mail@pooya.ai"] +description = "A high-performance, secure, and cross-platform modules optimized for efficiency, scalability, and reliability." +documentation = "https://docs.rs/sib" +edition = "2024" +keywords = ["sib", "networking", "real-time", "streaming", "web"] +license = "Apache-2.0" +repository = "https://github.com/PooyaEimandar/sib" +categories = ["development-tools"] +readme = "README.md" + +[dependencies] +sib = { git = "https://github.com/PooyaEimandar/sib", default-features = false, features = [ + "net-h1-server", +] } +bytes = { version = "1.10.1", default-features = false } +mimalloc = { version = "0.1.47", features = ["secure"] } +num_cpus = { version = "1.16" } + +[profile.release] +opt-level = 3 +codegen-units = 1 diff --git a/frameworks/Rust/sib/README.md b/frameworks/Rust/sib/README.md new file mode 100644 index 00000000000..d211c71acf7 --- /dev/null +++ b/frameworks/Rust/sib/README.md @@ -0,0 +1,16 @@ +# [SIB](https://github.com/pooyaeimandar/sib) web framework + +## Description + +A high-performance, secure, and cross-platform modules optimized for efficiency, scalability, and reliability. +SIB supports HTTP/1.1 as well as HTTP/3, and uses coroutine-based concurrency via [may](https://github.com/Xudong-Huang/may) along with zero-copy request/response handling, aiming for the lowest possible overhead. + +## Test URLs + +### Test 1: JSON Encoding + + http://localhost:8080/json + +### Test 2: Plaintext + + http://localhost:8080/plaintext diff --git a/frameworks/Rust/sib/benchmark_config.json b/frameworks/Rust/sib/benchmark_config.json new file mode 100644 index 00000000000..91c8ce06b13 --- /dev/null +++ b/frameworks/Rust/sib/benchmark_config.json @@ -0,0 +1,24 @@ +{ + "framework": "sib", + "tests": [ + { + "default": { + "json_url": "/json", + "plaintext_url": "/plaintext", + "port": 8080, + "approach": "Realistic", + "classification": "Fullstack", + "framework": "may", + "language": "Rust", + "flavor": "None", + "platform": "None", + "webserver": "None", + "os": "Linux", + "database_os": "Linux", + "display_name": "sib", + "notes": "", + "versus": "None" + } + } + ] +} diff --git a/frameworks/Rust/sib/sib.dockerfile b/frameworks/Rust/sib/sib.dockerfile new file mode 100644 index 00000000000..e4cf3f2530c --- /dev/null +++ b/frameworks/Rust/sib/sib.dockerfile @@ -0,0 +1,11 @@ +FROM rust:latest + +ADD ./ /sib +WORKDIR /sib + +RUN cargo clean +RUN RUSTFLAGS="-C target-cpu=native" cargo build --release + +EXPOSE 8080 + +CMD ["./target/release/sib-techempower"] diff --git a/frameworks/Rust/sib/src/main.rs b/frameworks/Rust/sib/src/main.rs new file mode 100644 index 00000000000..8aa798e277d --- /dev/null +++ b/frameworks/Rust/sib/src/main.rs @@ -0,0 +1,91 @@ +use bytes::Bytes; +use sib::network::http::{ + h1::{H1Service, H1ServiceFactory}, + message::Status, + session::Session, +}; +use std::{ + fs, + io::{Read, Write}, +}; + +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + +struct H1Server(pub T); + +struct HService; + +impl H1Service for HService { + fn call(&mut self, session: &mut Session) -> std::io::Result<()> { + if session.req_path() == Some("/json") { + // Respond with JSON + session + .status_code(Status::Ok) + .header_str("Content-Type", "application/json")? + .header_str("Content-Length", "27")? + .body(&Bytes::from_static(b"{\"message\":\"Hello, World!\"}")) + .eom(); + return Ok(()); + } + session + .status_code(Status::Ok) + .header_str("Content-Type", "text/plain")? + .header_str("Content-Length", "13")? + .body(&Bytes::from_static(b"Hello, World!")) + .eom(); + Ok(()) + } +} + +impl H1ServiceFactory for H1Server { + type Service = HService; + + fn service(&self, _id: usize) -> HService { + HService + } +} + +fn main() { + // Print number of CPU cores + let cpus = num_cpus::get(); + println!("CPU cores: {}", cpus); + + // Print total RAM in MB + if let Ok(meminfo) = fs::read_to_string("/proc/meminfo") { + for line in meminfo.lines() { + if line.starts_with("MemTotal:") { + let parts: Vec<&str> = line.split_whitespace().collect(); + if parts.len() >= 2 { + if let Ok(kb) = parts[1].parse::() { + let mb = kb / 1024; + println!("Total RAM: {} MB", mb); + } + } + break; + } + } + } + + // Pick a port and start the server + let addr = "0.0.0.0:8080"; + let mut threads = Vec::with_capacity(cpus); + + for _ in 0..cpus { + let handle = std::thread::spawn(move || { + let id = std::thread::current().id(); + println!("Listening {} on thread: {:?}", addr, id); + H1Server(HService) + .start(addr, cpus, 0, None) + .expect(&format!("h1 server failed to start for thread {:?}", id)) + .join() + .expect(&format!("h1 server failed to joining thread {:?}", id)); + }); + threads.push(handle); + } + + // Wait for all threads to complete (they won’t unless crashed) + for handle in threads { + handle.join().expect("Thread panicked"); + } +} From ff019d01ea40d0bcc2065783879a45254076760a Mon Sep 17 00:00:00 2001 From: Pooya Eimandar Date: Mon, 30 Jun 2025 20:46:00 +0400 Subject: [PATCH 2/2] fix: json --- frameworks/Rust/sib/Cargo.toml | 2 ++ frameworks/Rust/sib/src/main.rs | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/frameworks/Rust/sib/Cargo.toml b/frameworks/Rust/sib/Cargo.toml index 15dc4aa0b09..b27977b5957 100644 --- a/frameworks/Rust/sib/Cargo.toml +++ b/frameworks/Rust/sib/Cargo.toml @@ -18,6 +18,8 @@ sib = { git = "https://github.com/PooyaEimandar/sib", default-features = false, bytes = { version = "1.10.1", default-features = false } mimalloc = { version = "0.1.47", features = ["secure"] } num_cpus = { version = "1.16" } +serde = { version = "1.0.219", default-features = false, features = ["derive"] } +serde_json = { version = "1.0.140" } [profile.release] opt-level = 3 diff --git a/frameworks/Rust/sib/src/main.rs b/frameworks/Rust/sib/src/main.rs index 8aa798e277d..c546157762d 100644 --- a/frameworks/Rust/sib/src/main.rs +++ b/frameworks/Rust/sib/src/main.rs @@ -12,6 +12,19 @@ use std::{ #[global_allocator] static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; +#[derive(serde::Serialize)] +struct JsonMessage<'a> { + message: &'a str, +} + +impl Default for JsonMessage<'_> { + fn default() -> Self { + JsonMessage { + message: "Hello, World!", + } + } +} + struct H1Server(pub T); struct HService; @@ -20,11 +33,12 @@ impl H1Service for HService { fn call(&mut self, session: &mut Session) -> std::io::Result<()> { if session.req_path() == Some("/json") { // Respond with JSON + let json = serde_json::to_vec(&JsonMessage::default())?; session .status_code(Status::Ok) .header_str("Content-Type", "application/json")? - .header_str("Content-Length", "27")? - .body(&Bytes::from_static(b"{\"message\":\"Hello, World!\"}")) + .header_str("Content-Length", &json.len().to_string())? + .body(&Bytes::from(json)) .eom(); return Ok(()); }