-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
end_to_end.rs
302 lines (259 loc) · 7.33 KB
/
end_to_end.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#![feature(test)]
#![deny(warnings)]
extern crate test;
use std::net::SocketAddr;
use futures_util::future::join_all;
use tokio::runtime::current_thread::Runtime;
use hyper::{Body, Method, Request, Response, Server};
use hyper::client::HttpConnector;
// HTTP1
#[bench]
fn http1_get(b: &mut test::Bencher) {
opts()
.bench(b)
}
#[bench]
fn http1_post(b: &mut test::Bencher) {
opts()
.method(Method::POST)
.request_body(b"foo bar baz quux")
.bench(b)
}
#[bench]
fn http1_body_both_100kb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 100];
opts()
.method(Method::POST)
.request_body(body)
.response_body(body)
.bench(b)
}
#[bench]
fn http1_body_both_10mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 10];
opts()
.method(Method::POST)
.request_body(body)
.response_body(body)
.bench(b)
}
#[bench]
fn http1_parallel_x10_empty(b: &mut test::Bencher) {
opts()
.parallel(10)
.bench(b)
}
#[bench]
fn http1_parallel_x10_req_10mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 10];
opts()
.parallel(10)
.method(Method::POST)
.request_body(body)
.bench(b)
}
#[bench]
fn http1_parallel_x10_res_1mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 1];
opts()
.parallel(10)
.response_body(body)
.bench(b)
}
#[bench]
fn http1_parallel_x10_res_10mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 10];
opts()
.parallel(10)
.response_body(body)
.bench(b)
}
// HTTP2
const HTTP2_MAX_WINDOW: u32 = std::u32::MAX >> 1;
#[bench]
fn http2_get(b: &mut test::Bencher) {
opts()
.http2()
.bench(b)
}
#[bench]
fn http2_post(b: &mut test::Bencher) {
opts()
.http2()
.method(Method::POST)
.request_body(b"foo bar baz quux")
.bench(b)
}
#[bench]
fn http2_req_100kb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 100];
opts()
.http2()
.method(Method::POST)
.request_body(body)
.bench(b)
}
#[bench]
fn http2_parallel_x10_empty(b: &mut test::Bencher) {
opts()
.http2()
.parallel(10)
.bench(b)
}
#[bench]
fn http2_parallel_x10_req_10mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 10];
opts()
.http2()
.parallel(10)
.method(Method::POST)
.request_body(body)
//.http2_stream_window(HTTP2_MAX_WINDOW)
//.http2_conn_window(HTTP2_MAX_WINDOW)
.bench(b)
}
#[bench]
fn http2_parallel_x10_res_1mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 1];
opts()
.http2()
.parallel(10)
.response_body(body)
.http2_stream_window(HTTP2_MAX_WINDOW)
.http2_conn_window(HTTP2_MAX_WINDOW)
.bench(b)
}
#[bench]
fn http2_parallel_x10_res_10mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 10];
opts()
.http2()
.parallel(10)
.response_body(body)
.http2_stream_window(HTTP2_MAX_WINDOW)
.http2_conn_window(HTTP2_MAX_WINDOW)
.bench(b)
}
// ==== Benchmark Options =====
struct Opts {
http2: bool,
http2_stream_window: Option<u32>,
http2_conn_window: Option<u32>,
parallel_cnt: u32,
request_method: Method,
request_body: Option<&'static [u8]>,
response_body: &'static [u8],
}
fn opts() -> Opts {
Opts {
http2: false,
http2_stream_window: None,
http2_conn_window: None,
parallel_cnt: 1,
request_method: Method::GET,
request_body: None,
response_body: b"Hello",
}
}
impl Opts {
fn http2(mut self) -> Self {
self.http2 = true;
self
}
fn http2_stream_window(mut self, sz: impl Into<Option<u32>>) -> Self {
self.http2_stream_window = sz.into();
self
}
fn http2_conn_window(mut self, sz: impl Into<Option<u32>>) -> Self {
self.http2_conn_window = sz.into();
self
}
fn method(mut self, m: Method) -> Self {
self.request_method = m;
self
}
fn request_body(mut self, body: &'static [u8]) -> Self {
self.request_body = Some(body);
self
}
fn response_body(mut self, body: &'static [u8]) -> Self {
self.response_body = body;
self
}
fn parallel(mut self, cnt: u32) -> Self {
assert!(cnt > 0, "parallel count must be larger than 0");
self.parallel_cnt = cnt;
self
}
fn bench(self, b: &mut test::Bencher) {
let _ = pretty_env_logger::try_init();
// Create a runtime of current thread.
let mut rt = Runtime::new().unwrap();
b.bytes = self.response_body.len() as u64 + self.request_body.map(|b| b.len()).unwrap_or(0) as u64;
let addr = spawn_server(&mut rt, &self);
let connector = HttpConnector::new();
let client = hyper::Client::builder()
.http2_only(self.http2)
.http2_initial_stream_window_size(self.http2_stream_window)
.http2_initial_connection_window_size(self.http2_conn_window)
.build::<_, Body>(connector);
let url: hyper::Uri = format!("http://{}/hello", addr).parse().unwrap();
let make_request = || {
let body = self
.request_body
.map(Body::from)
.unwrap_or_else(|| Body::empty());
let mut req = Request::new(body);
*req.method_mut() = self.request_method.clone();
*req.uri_mut() = url.clone();
req
};
let send_request = |req: Request<Body>| {
let fut = client.request(req);
async {
let res = fut.await.expect("client wait");
let mut body = res.into_body();
while let Some(_chunk) = body.next().await {}
}
};
if self.parallel_cnt == 1 {
b.iter(|| {
let req = make_request();
rt.block_on(send_request(req));
});
} else {
b.iter(|| {
let futs = (0..self.parallel_cnt)
.map(|_| {
let req = make_request();
send_request(req)
});
// Await all spawned futures becoming completed.
rt.block_on(join_all(futs));
});
}
}
}
fn spawn_server(rt: &mut Runtime, opts: &Opts) -> SocketAddr {
use hyper::service::{make_service_fn, service_fn};
let addr = "127.0.0.1:0".parse().unwrap();
let body = opts.response_body;
let srv = Server::bind(&addr)
.http2_only(opts.http2)
.http2_initial_stream_window_size(opts.http2_stream_window)
.http2_initial_connection_window_size(opts.http2_conn_window)
.serve(make_service_fn( move |_| async move {
Ok::<_, hyper::Error>(service_fn(move |req: Request<Body>| async move {
let mut req_body = req.into_body();
while let Some(_chunk) = req_body.next().await {}
Ok::<_, hyper::Error>(Response::new(Body::from(body)))
}))
}));
let addr = srv.local_addr();
rt.spawn(async {
if let Err(err) = srv.await {
panic!("server error: {}", err);
}
});
return addr
}