Skip to content

Commit 9fdd0f6

Browse files
Add benchmark tests for the Sib (#9969)
* Add benchmark tests for the Sib * fix: json
1 parent c0a2905 commit 9fdd0f6

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

frameworks/Rust/sib/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "sib-techempower"
3+
version = "0.0.1"
4+
authors = ["[email protected]"]
5+
description = "A high-performance, secure, and cross-platform modules optimized for efficiency, scalability, and reliability."
6+
documentation = "https://docs.rs/sib"
7+
edition = "2024"
8+
keywords = ["sib", "networking", "real-time", "streaming", "web"]
9+
license = "Apache-2.0"
10+
repository = "https://github.com/PooyaEimandar/sib"
11+
categories = ["development-tools"]
12+
readme = "README.md"
13+
14+
[dependencies]
15+
sib = { git = "https://github.com/PooyaEimandar/sib", default-features = false, features = [
16+
"net-h1-server",
17+
] }
18+
bytes = { version = "1.10.1", default-features = false }
19+
mimalloc = { version = "0.1.47", features = ["secure"] }
20+
num_cpus = { version = "1.16" }
21+
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
22+
serde_json = { version = "1.0.140" }
23+
24+
[profile.release]
25+
opt-level = 3
26+
codegen-units = 1

frameworks/Rust/sib/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# [SIB](https://github.com/pooyaeimandar/sib) web framework
2+
3+
## Description
4+
5+
A high-performance, secure, and cross-platform modules optimized for efficiency, scalability, and reliability.
6+
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.
7+
8+
## Test URLs
9+
10+
### Test 1: JSON Encoding
11+
12+
http://localhost:8080/json
13+
14+
### Test 2: Plaintext
15+
16+
http://localhost:8080/plaintext
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"framework": "sib",
3+
"tests": [
4+
{
5+
"default": {
6+
"json_url": "/json",
7+
"plaintext_url": "/plaintext",
8+
"port": 8080,
9+
"approach": "Realistic",
10+
"classification": "Fullstack",
11+
"framework": "may",
12+
"language": "Rust",
13+
"flavor": "None",
14+
"platform": "None",
15+
"webserver": "None",
16+
"os": "Linux",
17+
"database_os": "Linux",
18+
"display_name": "sib",
19+
"notes": "",
20+
"versus": "None"
21+
}
22+
}
23+
]
24+
}

frameworks/Rust/sib/sib.dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM rust:latest
2+
3+
ADD ./ /sib
4+
WORKDIR /sib
5+
6+
RUN cargo clean
7+
RUN RUSTFLAGS="-C target-cpu=native" cargo build --release
8+
9+
EXPOSE 8080
10+
11+
CMD ["./target/release/sib-techempower"]

frameworks/Rust/sib/src/main.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use bytes::Bytes;
2+
use sib::network::http::{
3+
h1::{H1Service, H1ServiceFactory},
4+
message::Status,
5+
session::Session,
6+
};
7+
use std::{
8+
fs,
9+
io::{Read, Write},
10+
};
11+
12+
#[global_allocator]
13+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
14+
15+
#[derive(serde::Serialize)]
16+
struct JsonMessage<'a> {
17+
message: &'a str,
18+
}
19+
20+
impl Default for JsonMessage<'_> {
21+
fn default() -> Self {
22+
JsonMessage {
23+
message: "Hello, World!",
24+
}
25+
}
26+
}
27+
28+
struct H1Server<T>(pub T);
29+
30+
struct HService;
31+
32+
impl H1Service for HService {
33+
fn call<S: Read + Write>(&mut self, session: &mut Session<S>) -> std::io::Result<()> {
34+
if session.req_path() == Some("/json") {
35+
// Respond with JSON
36+
let json = serde_json::to_vec(&JsonMessage::default())?;
37+
session
38+
.status_code(Status::Ok)
39+
.header_str("Content-Type", "application/json")?
40+
.header_str("Content-Length", &json.len().to_string())?
41+
.body(&Bytes::from(json))
42+
.eom();
43+
return Ok(());
44+
}
45+
session
46+
.status_code(Status::Ok)
47+
.header_str("Content-Type", "text/plain")?
48+
.header_str("Content-Length", "13")?
49+
.body(&Bytes::from_static(b"Hello, World!"))
50+
.eom();
51+
Ok(())
52+
}
53+
}
54+
55+
impl H1ServiceFactory for H1Server<HService> {
56+
type Service = HService;
57+
58+
fn service(&self, _id: usize) -> HService {
59+
HService
60+
}
61+
}
62+
63+
fn main() {
64+
// Print number of CPU cores
65+
let cpus = num_cpus::get();
66+
println!("CPU cores: {}", cpus);
67+
68+
// Print total RAM in MB
69+
if let Ok(meminfo) = fs::read_to_string("/proc/meminfo") {
70+
for line in meminfo.lines() {
71+
if line.starts_with("MemTotal:") {
72+
let parts: Vec<&str> = line.split_whitespace().collect();
73+
if parts.len() >= 2 {
74+
if let Ok(kb) = parts[1].parse::<u64>() {
75+
let mb = kb / 1024;
76+
println!("Total RAM: {} MB", mb);
77+
}
78+
}
79+
break;
80+
}
81+
}
82+
}
83+
84+
// Pick a port and start the server
85+
let addr = "0.0.0.0:8080";
86+
let mut threads = Vec::with_capacity(cpus);
87+
88+
for _ in 0..cpus {
89+
let handle = std::thread::spawn(move || {
90+
let id = std::thread::current().id();
91+
println!("Listening {} on thread: {:?}", addr, id);
92+
H1Server(HService)
93+
.start(addr, cpus, 0, None)
94+
.expect(&format!("h1 server failed to start for thread {:?}", id))
95+
.join()
96+
.expect(&format!("h1 server failed to joining thread {:?}", id));
97+
});
98+
threads.push(handle);
99+
}
100+
101+
// Wait for all threads to complete (they won’t unless crashed)
102+
for handle in threads {
103+
handle.join().expect("Thread panicked");
104+
}
105+
}

0 commit comments

Comments
 (0)