Created
April 29, 2017 16:41
-
-
Save anonymous/d5fa7a62ae1d3927878a9762287b1781 to your computer and use it in GitHub Desktop.
Hyper HTTP client test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "httpclient" | |
version = "0.1.0" | |
authors = [""] | |
[dependencies] | |
hyper = { git = "https://github.com/hyperium/hyper", rev = "master" } | |
futures = "*" | |
tokio-core = "*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate futures; | |
extern crate hyper; | |
extern crate tokio_core; | |
use futures::Future; | |
use hyper::client::Client; | |
use std::sync::mpsc::{Sender, Receiver}; | |
use std::sync::mpsc; | |
use std::time::SystemTime; | |
use std::thread; | |
static NTHREADS: i32 = 8; | |
static NREQ: i32 = 3000; | |
fn main() { | |
let (tx, rx): (Sender<i32>, Receiver<i32>) = mpsc::channel(); | |
for id in 0..NTHREADS { | |
let thread_tx = tx.clone(); | |
thread::spawn(move || { | |
let mut perf = Vec::with_capacity(NREQ as usize); | |
let mut core = tokio_core::reactor::Core::new().unwrap(); | |
let handle = core.handle(); | |
let client = Client::new(&handle); | |
let url = "http://localhost:8080/hello".parse::<hyper::Uri>().unwrap(); | |
for _ in 0..NREQ { | |
let now = SystemTime::now(); | |
let work = client.get(url.clone()); | |
core.run(work).unwrap(); | |
let sec = match now.elapsed() { | |
Ok(elapsed) => { (elapsed.as_secs() as f64 * 1000.0 ) + (elapsed.subsec_nanos() as f64 / 1000_000.0) } | |
Err(_e) => { 0.0 } | |
}; | |
perf.push(sec); | |
} | |
let sum: f64 = perf.iter().fold(0.0, |sum, &val| sum + val); | |
let performance: i32 = ((NREQ as f64) / (sum / 1000.0)).round() as i32; | |
println!("thread {} finished, performance: {} req/sec", id, performance); | |
thread_tx.send(performance).unwrap(); | |
}); | |
} | |
let mut ids = Vec::with_capacity(NTHREADS as usize); | |
for _ in 0..NTHREADS { | |
ids.push(rx.recv().unwrap()); | |
} | |
let overall: i32 = ids.iter().fold(0, |sum, &val| sum + val); | |
println!("The overall performance is: {:?}", overall); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment