Files
hyper/benches/end_to_end.rs
Alex Crichton f45e9c8e4f refactor(server): expose Http that implements ServerProto
The main changes are:

* The entry point is how `Http`, the implementation of `ServerProto`.
  This type has a `new` constructor as well as builder methods to
  configure it.

* A high-level entry point of `Http::bind` was added which returns a
  `Server`. Binding a protocol to a port requires a socket address
  (where to bind) as well as the instance of `NewService`. Internally
  this creates a core and a TCP listener.

* The returned `Server` has a few methods to learn about itself, e.g.
  `local_addr` and `handle`, but mainly has two methods: `run` and
  `run_until`.

* The `Server::run` entry point will execute a server infinitely, never
  having it exit.

* The `Server::run_until` method is intended as a graceful shutdown
  mechanism. When the provided future resolves the server stops
  accepting connections immediately and then waits for a fixed period of
  time for all active connections to get torn down, after which the
  whole server is torn down anyway.

* Finally a `Http::bind_connection` method exists as a low-level entry
  point to spawning a server connection. This is used by `Server::run`
  as is intended for external use in other event loops if necessary or
  otherwise low-level needs.

BREAKING CHANGE: `Server` is no longer the pimary entry point. Instead,
  an `Http` type is created  and then either `bind` to receiver a `Server`,
  or it can be passed to other Tokio things.
2017-01-18 14:09:20 -08:00

107 lines
2.9 KiB
Rust

#![feature(test)]
#![deny(warnings)]
extern crate futures;
extern crate hyper;
extern crate pretty_env_logger;
extern crate test;
extern crate tokio_core;
use std::net::SocketAddr;
use futures::{Future, Stream};
use tokio_core::reactor::{Core, Handle};
use tokio_core::net::TcpListener;
use hyper::client;
use hyper::header::{ContentLength, ContentType};
use hyper::Method;
use hyper::server::{self, Service};
#[bench]
fn get_one_at_a_time(b: &mut test::Bencher) {
let _ = pretty_env_logger::init();
let mut core = Core::new().unwrap();
let handle = core.handle();
let addr = spawn_hello(&handle);
let client = hyper::Client::new(&handle);
let url: hyper::Url = format!("http://{}/get", addr).parse().unwrap();
b.bytes = 160 * 2 + PHRASE.len() as u64;
b.iter(move || {
let work = client.get(url.clone()).and_then(|res| {
res.body().for_each(|_chunk| {
Ok(())
})
});
core.run(work).unwrap();
});
}
#[bench]
fn post_one_at_a_time(b: &mut test::Bencher) {
let _ = pretty_env_logger::init();
let mut core = Core::new().unwrap();
let handle = core.handle();
let addr = spawn_hello(&handle);
let client = hyper::Client::new(&handle);
let url: hyper::Url = format!("http://{}/get", addr).parse().unwrap();
let post = "foo bar baz quux";
b.bytes = 180 * 2 + post.len() as u64 + PHRASE.len() as u64;
b.iter(move || {
let mut req = client::Request::new(Method::Post, url.clone());
req.headers_mut().set(ContentLength(post.len() as u64));
req.set_body(post);
let work = client.get(url.clone()).and_then(|res| {
res.body().for_each(|_chunk| {
Ok(())
})
});
core.run(work).unwrap();
});
}
static PHRASE: &'static [u8] = include_bytes!("../CHANGELOG.md"); //b"Hello, World!";
#[derive(Clone, Copy)]
struct Hello;
impl Service for Hello {
type Request = server::Request;
type Response = server::Response;
type Error = hyper::Error;
type Future = ::futures::Finished<Self::Response, hyper::Error>;
fn call(&self, _req: Self::Request) -> Self::Future {
::futures::finished(
server::Response::new()
.with_header(ContentLength(PHRASE.len() as u64))
.with_header(ContentType::plaintext())
.with_body(PHRASE)
)
}
}
fn spawn_hello(handle: &Handle) -> SocketAddr {
let addr = "127.0.0.1:0".parse().unwrap();
let listener = TcpListener::bind(&addr, handle).unwrap();
let addr = listener.local_addr().unwrap();
let handle2 = handle.clone();
handle.spawn(listener.incoming().for_each(move |(socket, addr)| {
let http = hyper::server::Http::new();
http.bind_connection(&handle2, socket, addr, Hello);
Ok(())
}).then(|_| Ok(())));
return addr
}