Open
Description
Hello,
After some research, I decided to give a try to Futures to come up with a simple async API. The main idea is to keep everything very straightforward for the user (the event loop is created by the API).
The idea originates from here : #9
The source code is :
#![feature(conservative_impl_trait)]
extern crate futures;
extern crate hyper;
extern crate tokio_core;
extern crate serde_json;
use futures::Future;
use futures::future;
use futures::stream::Stream;
use hyper::Client;
use serde_json::Value;
struct Api {
core: tokio_core::reactor::Core,
}
impl Api {
pub fn new() -> Api {
Api {
core: tokio_core::reactor::Core::new().unwrap(),
}
}
pub fn time(&self) -> impl Future<Item = Value, Error = hyper::Error> {
let url = "http://date.jsontest.com";
let uri = url.parse::<hyper::Uri>().unwrap();
let handle = self.core.handle();
let client = Client::new(&handle);
client.get(uri).and_then(|res| {
res.body().concat2().and_then(move |body| {
let v: Value = serde_json::from_slice(&body).unwrap();
future::ok((v))
})
})
}
}
fn main() {
// create the API and the Tokio core
let mut api = Api::new();
// BENCHMARK THE SYNC VERSION
let work_1 = api.time(); // create the handle, the client and send request inside a Future
let res = api.core.run(work_1).unwrap();
let time_1 = res["milliseconds_since_epoch"].as_f64().unwrap();
let work_2 = api.time();
let res = api.core.run(work_2).unwrap();
let time_2 = res["milliseconds_since_epoch"].as_f64().unwrap();
println!("\nSync version takes {} milliseconds to execute.", time_2 - time_1);
// BENCHMARK THE ASYNC VERSION
let work_async = api.time().join(api.time());
let (res_1, res_2) = api.core.run(work_async).unwrap();
let time_1 = res_1["milliseconds_since_epoch"].as_f64().unwrap();
let time_2 = res_2["milliseconds_since_epoch"].as_f64().unwrap();
println!("\nAsync version takes {} milliseconds to execute.\n", (time_2 - time_1).abs());
}
Cargo.toml :
[dependencies]
futures = "0.1"
hyper = "0.11"
tokio-core = "0.1"
serde_json = "1.0.5"
The code below returns:
Sync version takes 198 milliseconds to execute.
Async version takes 5 milliseconds to execute.
What do you think? Since it's a huge rewrite it would be nice to have some feedbacks :)
Metadata
Assignees
Labels
No labels
Activity