Skip to content

feat(client): add a new Client struct with super powers #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ Client:

```rust
fn main() {
// Creating an outgoing request.
let mut req = Request::get(Url::parse("http://www.gooogle.com/").unwrap()).unwrap();
// Create a client.
let mut client = Client::new();

// Setting a header.
req.headers_mut().set(Connection(vec![Close]));
// Creating an outgoing request.
let mut res = client.get("http://www.gooogle.com/")
// set a header
.header(Connection(vec![Close]))
// let 'er go!
.send();

// Start the Request, writing headers and starting streaming.
let res = req.start().unwrap()
// Send the Request.
.send().unwrap()
// Read the Response.
.read_to_string().unwrap();
// Read the Response.
let body = res.read_to_string().unwrap();

println!("Response: {}", res);
}
Expand All @@ -64,22 +64,20 @@ fn main() {
[Client Bench:](./benches/client.rs)

```

running 3 tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "running 2 tests"?

test bench_curl ... bench: 298416 ns/iter (+/- 132455)
test bench_http ... bench: 292725 ns/iter (+/- 167575)
test bench_hyper ... bench: 222819 ns/iter (+/- 86615)
test bench_curl ... bench: 400253 ns/iter (+/- 143539)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What made cURL slower?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be any number of things. We just always make sure to use a single run of benchmarks, as opposed to repeated runs.

test bench_hyper ... bench: 181703 ns/iter (+/- 46529)

test result: ok. 0 passed; 0 failed; 0 ignored; 3 measured
test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured
```

[Mock Client Bench:](./benches/client_mock_tcp.rs)

```
running 3 tests
test bench_mock_curl ... bench: 25254 ns/iter (+/- 2113)
test bench_mock_http ... bench: 43585 ns/iter (+/- 1206)
test bench_mock_hyper ... bench: 27153 ns/iter (+/- 2227)
test bench_mock_curl ... bench: 53987 ns/iter (+/- 1735)
test bench_mock_http ... bench: 43569 ns/iter (+/- 1409)
test bench_mock_hyper ... bench: 20996 ns/iter (+/- 1742)

test result: ok. 0 passed; 0 failed; 0 ignored; 3 measured
```
Expand Down
26 changes: 16 additions & 10 deletions benches/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}

Expand All @@ -41,7 +46,7 @@ fn bench_curl(b: &mut test::Bencher) {
.exec()
.unwrap()
});
listening.close().unwrap()
listening.close().unwrap();
}

#[deriving(Clone)]
Expand All @@ -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();
Expand All @@ -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()
}
*/
3 changes: 2 additions & 1 deletion benches/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
22 changes: 6 additions & 16 deletions examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::os;
use std::io::stdout;
use std::io::util::copy;

use hyper::Url;
use hyper::client::Request;
use hyper::Client;

fn main() {
let args = os::args();
Expand All @@ -17,26 +16,17 @@ fn main() {
}
};

let url = match Url::parse(args[1].as_slice()) {
Ok(url) => {
println!("GET {}...", url)
url
},
Err(e) => panic!("Invalid URL: {}", e)
};
let url = &*args[1];

let mut client = Client::new();

let req = match Request::get(url) {
Ok(req) => req,
let mut res = match client.get(url).send() {
Ok(res) => res,
Err(err) => panic!("Failed to connect: {}", err)
};

let mut res = req
.start().unwrap() // failure: Error writing Headers
.send().unwrap(); // failure: Error reading Response head.

println!("Response: {}", res.status);
println!("{}", res.headers);
println!("Headers:\n{}", res.headers);
match copy(&mut res, &mut stdout()) {
Ok(..) => (),
Err(e) => panic!("Stream failure: {}", e)
Expand Down
Loading