-
Notifications
You must be signed in to change notification settings - Fork 967
/
benches.rs
240 lines (206 loc) · 6.22 KB
/
benches.rs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use axum::{
extract::State,
routing::{get, post},
Extension, Json, Router, Server,
};
use hyper::server::conn::AddrIncoming;
use serde::{Deserialize, Serialize};
use std::{
io::BufRead,
process::{Command, Stdio},
};
fn main() {
if on_ci() {
install_rewrk();
} else {
ensure_rewrk_is_installed();
}
benchmark("minimal").run(Router::new);
benchmark("basic").run(|| Router::new().route("/", get(|| async { "Hello, World!" })));
benchmark("routing").path("/foo/bar/baz").run(|| {
let mut app = Router::new();
for a in 0..10 {
for b in 0..10 {
for c in 0..10 {
app = app.route(&format!("/foo-{}/bar-{}/baz-{}", a, b, c), get(|| async {}));
}
}
}
app.route("/foo/bar/baz", get(|| async {}))
});
benchmark("receive-json")
.method("post")
.headers(&[("content-type", "application/json")])
.body(r#"{"n": 123, "s": "hi there", "b": false}"#)
.run(|| Router::new().route("/", post(|_: Json<Payload>| async {})));
benchmark("send-json").run(|| {
Router::new().route(
"/",
get(|| async {
Json(Payload {
n: 123,
s: "hi there".to_owned(),
b: false,
})
}),
)
});
let state = AppState {
_string: "aaaaaaaaaaaaaaaaaa".to_owned(),
_vec: Vec::from([
"aaaaaaaaaaaaaaaaaa".to_owned(),
"bbbbbbbbbbbbbbbbbb".to_owned(),
"cccccccccccccccccc".to_owned(),
]),
};
benchmark("extension").run(|| {
Router::new()
.route("/", get(|_: Extension<AppState>| async {}))
.layer(Extension(state.clone()))
});
benchmark("state").run(|| {
Router::new()
.route("/", get(|_: State<AppState>| async {}))
.with_state(state.clone())
});
}
#[derive(Clone)]
struct AppState {
_string: String,
_vec: Vec<String>,
}
#[derive(Deserialize, Serialize)]
struct Payload {
n: u32,
s: String,
b: bool,
}
fn benchmark(name: &'static str) -> BenchmarkBuilder {
BenchmarkBuilder {
name,
path: None,
method: None,
headers: None,
body: None,
}
}
struct BenchmarkBuilder {
name: &'static str,
path: Option<&'static str>,
method: Option<&'static str>,
headers: Option<&'static [(&'static str, &'static str)]>,
body: Option<&'static str>,
}
macro_rules! config_method {
($name:ident, $ty:ty) => {
fn $name(mut self, $name: $ty) -> Self {
self.$name = Some($name);
self
}
};
}
impl BenchmarkBuilder {
config_method!(path, &'static str);
config_method!(method, &'static str);
config_method!(headers, &'static [(&'static str, &'static str)]);
config_method!(body, &'static str);
fn run<F>(self, f: F)
where
F: FnOnce() -> Router<()>,
{
// support only running some benchmarks with
// ```
// cargo bench -- routing send-json
// ```
let args = std::env::args().collect::<Vec<_>>();
if args.len() != 1 {
let names = &args[1..args.len() - 1];
if !names.is_empty() && !names.contains(&self.name.to_owned()) {
return;
}
}
let app = f();
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
let listener = rt
.block_on(tokio::net::TcpListener::bind("0.0.0.0:0"))
.unwrap();
let addr = listener.local_addr().unwrap();
std::thread::spawn(move || {
rt.block_on(async move {
let incoming = AddrIncoming::from_listener(listener).unwrap();
Server::builder(incoming)
.serve(app.into_make_service())
.await
.unwrap();
});
});
let mut cmd = Command::new("rewrk");
cmd.stdout(Stdio::piped());
cmd.arg("--host");
cmd.arg(format!("http://{}{}", addr, self.path.unwrap_or("")));
cmd.args(["--connections", "10"]);
cmd.args(["--threads", "10"]);
if on_ci() {
// don't slow down CI by running the benchmarks for too long
// but do run them for a bit
cmd.args(["--duration", "1s"]);
} else {
cmd.args(["--duration", "10s"]);
}
if let Some(method) = self.method {
cmd.args(["--method", method]);
}
for (key, value) in self.headers.into_iter().flatten() {
cmd.arg("--header");
cmd.arg(format!("{}: {}", key, value));
}
if let Some(body) = self.body {
cmd.args(["--body", body]);
}
eprintln!("Running {:?} benchmark", self.name);
// indent output from `rewrk` so its easier to read when running multiple benchmarks
let mut child = cmd.spawn().unwrap();
let stdout = child.stdout.take().unwrap();
let stdout = std::io::BufReader::new(stdout);
for line in stdout.lines() {
let line = line.unwrap();
println!(" {}", line);
}
let status = child.wait().unwrap();
if !status.success() {
eprintln!("`rewrk` command failed");
std::process::exit(status.code().unwrap());
}
}
}
fn install_rewrk() {
println!("installing rewrk");
let mut cmd = Command::new("cargo");
cmd.args([
"install",
"rewrk",
"--git",
"https://github.com/ChillFish8/rewrk.git",
]);
let status = cmd
.status()
.unwrap_or_else(|_| panic!("failed to install rewrk"));
if !status.success() {
panic!("failed to install rewrk");
}
}
fn ensure_rewrk_is_installed() {
let mut cmd = Command::new("rewrk");
cmd.arg("--help");
cmd.stdout(Stdio::null());
cmd.stderr(Stdio::null());
cmd.status().unwrap_or_else(|_| {
panic!("rewrk is not installed. See https://github.com/lnx-search/rewrk")
});
}
fn on_ci() -> bool {
std::env::var("GITHUB_ACTIONS").is_ok()
}