forked from hyperium/hyper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.rs
100 lines (87 loc) · 2.51 KB
/
client.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
#![feature(macro_rules)]
extern crate curl;
extern crate http;
extern crate hyper;
extern crate test;
use std::fmt::{mod, Show};
use std::io::net::ip::Ipv4Addr;
use hyper::server::{Request, Response, Server};
fn listen() -> hyper::server::Listening {
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 0);
server.listen(handle).unwrap()
}
macro_rules! try_return(
($e:expr) => {{
match $e {
Ok(v) => v,
Err(..) => return
}
}})
fn handle(_: Request, res: Response) {
let mut res = try_return!(res.start());
try_return!(res.write(b"Benchmarking hyper vs others!"))
try_return!(res.end());
}
#[bench]
fn bench_curl(b: &mut test::Bencher) {
let mut listening = listen();
let s = format!("http://{}/", listening.socket);
let url = s.as_slice();
b.iter(|| {
curl::http::handle()
.get(url)
.header("X-Foo", "Bar")
.exec()
.unwrap()
});
listening.close().unwrap()
}
#[deriving(Clone)]
struct Foo;
impl hyper::header::Header for Foo {
fn header_name(_: Option<Foo>) -> &'static str {
"x-foo"
}
fn parse_header(_: &[Vec<u8>]) -> Option<Foo> {
None
}
}
impl hyper::header::HeaderFormat for Foo {
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
"Bar".fmt(fmt)
}
}
#[bench]
fn bench_hyper(b: &mut test::Bencher) {
let mut listening = listen();
let s = format!("http://{}/", listening.socket);
let url = s.as_slice();
b.iter(|| {
let mut req = hyper::client::Request::get(hyper::Url::parse(url).unwrap()).unwrap();
req.headers_mut().set(Foo);
req.start().unwrap()
.send().unwrap()
.read_to_string().unwrap()
});
listening.close().unwrap()
}
#[bench]
fn bench_http(b: &mut test::Bencher) {
let mut listening = listen();
let s = format!("http://{}/", listening.socket);
let url = s.as_slice();
b.iter(|| {
let mut req: http::client::RequestWriter = http::client::RequestWriter::new(
http::method::Get,
hyper::Url::parse(url).unwrap()
).unwrap();
req.headers.extensions.insert("x-foo".to_string(), "Bar".to_string());
// cant unwrap because Err contains RequestWriter, which does not implement Show
let mut res = match req.read_response() {
Ok(res) => res,
Err(..) => panic!("http response failed")
};
res.read_to_string().unwrap();
});
listening.close().unwrap()
}