feat(client): add a new Client struct with super powers
- Includes ergonomic traits like IntoUrl and IntoBody, allowing easy usage. - Client can have a RedirectPolicy. - Client can have a SslVerifier. Updated benchmarks for client. (Disabled rust-http client bench since it hangs.)
This commit is contained in:
@@ -8,6 +8,10 @@ extern crate test;
|
||||
use std::fmt::{mod, Show};
|
||||
use std::io::net::ip::Ipv4Addr;
|
||||
use hyper::server::{Request, Response, Server};
|
||||
use hyper::method::Method::Get;
|
||||
use hyper::header::Headers;
|
||||
use hyper::Client;
|
||||
use hyper::client::RequestBuilder;
|
||||
|
||||
fn listen() -> hyper::server::Listening {
|
||||
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 0);
|
||||
@@ -22,9 +26,10 @@ macro_rules! try_return(
|
||||
}
|
||||
}})
|
||||
|
||||
fn handle(_: Request, res: Response) {
|
||||
fn handle(_r: Request, res: Response) {
|
||||
static BODY: &'static [u8] = b"Benchmarking hyper vs others!";
|
||||
let mut res = try_return!(res.start());
|
||||
try_return!(res.write(b"Benchmarking hyper vs others!"))
|
||||
try_return!(res.write(BODY))
|
||||
try_return!(res.end());
|
||||
}
|
||||
|
||||
@@ -41,7 +46,7 @@ fn bench_curl(b: &mut test::Bencher) {
|
||||
.exec()
|
||||
.unwrap()
|
||||
});
|
||||
listening.close().unwrap()
|
||||
listening.close().unwrap();
|
||||
}
|
||||
|
||||
#[deriving(Clone)]
|
||||
@@ -67,17 +72,17 @@ fn bench_hyper(b: &mut test::Bencher) {
|
||||
let mut listening = listen();
|
||||
let s = format!("http://{}/", listening.socket);
|
||||
let url = s.as_slice();
|
||||
let mut client = Client::new();
|
||||
let mut headers = Headers::new();
|
||||
headers.set(Foo);
|
||||
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()
|
||||
client.get(url).header(Foo).send().unwrap().read_to_string().unwrap();
|
||||
});
|
||||
listening.close().unwrap()
|
||||
}
|
||||
|
||||
/*
|
||||
doesn't handle keep-alive properly...
|
||||
#[bench]
|
||||
fn bench_http(b: &mut test::Bencher) {
|
||||
let mut listening = listen();
|
||||
@@ -92,9 +97,10 @@ fn bench_http(b: &mut test::Bencher) {
|
||||
// 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")
|
||||
Err((_, ioe)) => panic!("http response failed = {}", ioe)
|
||||
};
|
||||
res.read_to_string().unwrap();
|
||||
});
|
||||
listening.close().unwrap()
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -9,12 +9,13 @@ use test::Bencher;
|
||||
use std::io::net::ip::{SocketAddr, Ipv4Addr};
|
||||
|
||||
use http::server::Server;
|
||||
use hyper::method::Method::Get;
|
||||
use hyper::server::{Request, Response};
|
||||
|
||||
static PHRASE: &'static [u8] = b"Benchmarking hyper vs others!";
|
||||
|
||||
fn request(url: hyper::Url) {
|
||||
let req = hyper::client::Request::get(url).unwrap();
|
||||
let req = hyper::client::Request::new(Get, url).unwrap();
|
||||
req.start().unwrap().send().unwrap().read_to_string().unwrap();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user