forked from TechEmpower/FrameworkBenchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
95 lines (82 loc) · 2.7 KB
/
main.rs
File metadata and controls
95 lines (82 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// TechEmpower benchmark tests for Sib with the `net-h1-server` feature enabled
use bytes::Bytes;
use http::StatusCode;
use sib::network::http::{
server::{H1Config, HFactory},
session::{HService, Session},
};
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
const PLAINTEXT_BODY: &[u8] = b"Hello, World!";
const PLAINTEXT_CONTENT_LENGTH: &str = "13";
#[derive(serde::Serialize)]
struct JsonMessage<'a> {
message: &'a str,
}
impl Default for JsonMessage<'_> {
fn default() -> Self {
JsonMessage {
message: "Hello, World!",
}
}
}
struct Server;
impl HService for Server {
fn call<S: Session>(&mut self, session: &mut S) -> std::io::Result<()> {
match session.req_path_bytes() {
b"/json" => {
let json = serde_json::to_vec(&JsonMessage::default())?;
let json_len = json.len().to_string();
session
.status_code(StatusCode::OK)
.header_str("Content-Type", "application/json")?
.header_str("Content-Length", json_len.as_str())?
.body(json.into())
.eom()
}
_ => session
.status_code(StatusCode::OK)
.header_str("Content-Type", "text/plain")?
.header_str("Content-Length", PLAINTEXT_CONTENT_LENGTH)?
.body(Bytes::from_static(PLAINTEXT_BODY))
.eom(),
}
}
}
impl HFactory for Server {
type Service = Server;
fn service(&self, _id: usize) -> Server {
Server
}
}
fn main() {
let stack_size = 2 * 1024; // 2 KB stack
let cpus = num_cpus::get();
sib::init_global_poller(cpus, stack_size);
// 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();
tracing::info!("Listening {addr} on thread: {id:?}");
Server
.start_h1(
addr,
H1Config {
io_timeout: std::time::Duration::from_secs(15),
stack_size,
..Default::default()
},
)
.unwrap_or_else(|_| panic!("H1 server failed to start for thread {id:?}"))
.join()
.unwrap_or_else(|_| panic!("H1 server failed to join thread {id:?}"));
});
threads.push(handle);
}
// Wait for all threads to complete
for handle in threads {
handle.join().expect("Thread panicked");
}
}