From 1a0054a00f33feaeff9b35dc4c6942a2d74b566d Mon Sep 17 00:00:00 2001 From: Tim Kuehn Date: Sun, 14 Feb 2016 20:33:42 -0800 Subject: [PATCH] Move benchmark into tarpc_examples, allowing tarpc to test on stable --- tarpc/src/lib.rs | 7 ---- tarpc/src/macros.rs | 59 +----------------------------- tarpc/src/protocol/packet.rs | 2 +- tarpc_examples/Cargo.toml | 7 ++-- tarpc_examples/src/lib.rs | 69 ++++++++++++++++++++++++++++++++++-- 5 files changed, 74 insertions(+), 70 deletions(-) diff --git a/tarpc/src/lib.rs b/tarpc/src/lib.rs index dd6055a..c46be08 100644 --- a/tarpc/src/lib.rs +++ b/tarpc/src/lib.rs @@ -45,7 +45,6 @@ //! ``` #![deny(missing_docs)] -#![cfg_attr(test, feature(test))] extern crate serde; extern crate bincode; @@ -53,12 +52,6 @@ extern crate bincode; extern crate log; extern crate scoped_pool; -#[cfg(test)] -#[macro_use] -extern crate lazy_static; -#[cfg(test)] -extern crate test; - macro_rules! pos { () => (concat!(file!(), ":", line!())) } diff --git a/tarpc/src/macros.rs b/tarpc/src/macros.rs index 4baa737..b25c0cf 100644 --- a/tarpc/src/macros.rs +++ b/tarpc/src/macros.rs @@ -525,8 +525,8 @@ mod functional_test { #[test] fn serde() { - let _ = env_logger::init(); use bincode; + let _ = env_logger::init(); let request = __Request::add((1, 2)); let ser = bincode::serde::serialize(&request, bincode::SizeLimit::Infinite).unwrap(); @@ -538,60 +538,3 @@ mod functional_test { } } } - -#[cfg(test)] -#[allow(dead_code)] // generated Client isn't used in this benchmark -mod benchmark { - extern crate env_logger; - use ServeHandle; - use std::sync::{Arc, Mutex}; - use test::Bencher; - - service! { - rpc hello(s: String) -> String; - } - - struct HelloServer; - impl Service for HelloServer { - fn hello(&self, s: String) -> String { - format!("Hello, {}!", s) - } - } - - // Prevents resource exhaustion when benching - lazy_static! { - static ref HANDLE: Arc> = { - let handle = serve("localhost:0", HelloServer, None).unwrap(); - Arc::new(Mutex::new(handle)) - }; - static ref CLIENT: Arc> = { - let addr = HANDLE.lock().unwrap().local_addr().clone(); - let client = AsyncClient::new(addr, None).unwrap(); - Arc::new(Mutex::new(client)) - }; - } - - #[bench] - fn hello(bencher: &mut Bencher) { - let _ = env_logger::init(); - let client = CLIENT.lock().unwrap(); - let concurrency = 100; - let mut futures = Vec::with_capacity(concurrency); - let mut count = 0; - bencher.iter(|| { - futures.push(client.hello("Bob".into())); - count += 1; - if count % concurrency == 0 { - // We can't block on each rpc call, otherwise we'd be - // benchmarking latency instead of throughput. It's also - // not ideal to call more than one rpc per iteration, because - // it makes the output of the bencher harder to parse (you have - // to mentally divide the number by `concurrency` to get - // the ns / iter for one rpc - for f in futures.drain(..) { - f.get().unwrap(); - } - } - }); - } -} diff --git a/tarpc/src/protocol/packet.rs b/tarpc/src/protocol/packet.rs index ae59472..63c351a 100644 --- a/tarpc/src/protocol/packet.rs +++ b/tarpc/src/protocol/packet.rs @@ -88,8 +88,8 @@ extern crate env_logger; #[test] fn serde() { - let _ = env_logger::init(); use bincode; + let _ = env_logger::init(); let packet = Packet { rpc_id: 1, message: () }; let ser = bincode::serde::serialize(&packet, bincode::SizeLimit::Infinite).unwrap(); diff --git a/tarpc_examples/Cargo.toml b/tarpc_examples/Cargo.toml index 89a9266..48082fc 100644 --- a/tarpc_examples/Cargo.toml +++ b/tarpc_examples/Cargo.toml @@ -1,6 +1,9 @@ [package] name = "tarpc_examples" version = "0.1.0" -authors = ["Tim Kuehn "] +authors = ["Adam Wright ", "Tim Kuehn "] -[dependencies] +[dev-dependencies] +tarpc = { path = "../tarpc" } +lazy_static = "^0.1.15" +env_logger = "^0.3.2" diff --git a/tarpc_examples/src/lib.rs b/tarpc_examples/src/lib.rs index bda8681..d2677d0 100644 --- a/tarpc_examples/src/lib.rs +++ b/tarpc_examples/src/lib.rs @@ -3,6 +3,71 @@ // Licensed under the MIT License, . // This file may not be copied, modified, or distributed except according to those terms. -#[test] -fn it_works() { +#![cfg_attr(test, feature(test))] + +#[cfg(test)] +#[macro_use] +extern crate lazy_static; + +#[cfg(test)] +#[macro_use] +extern crate tarpc; + +#[cfg(test)] +#[allow(dead_code)] // generated Client isn't used in this benchmark +mod benchmark { + extern crate env_logger; + extern crate test; + + use tarpc::ServeHandle; + use self::test::Bencher; + use std::sync::{Arc, Mutex}; + + service! { + rpc hello(s: String) -> String; + } + + struct HelloServer; + impl Service for HelloServer { + fn hello(&self, s: String) -> String { + format!("Hello, {}!", s) + } + } + + // Prevents resource exhaustion when benching + lazy_static! { + static ref HANDLE: Arc> = { + let handle = serve("localhost:0", HelloServer, None).unwrap(); + Arc::new(Mutex::new(handle)) + }; + static ref CLIENT: Arc> = { + let addr = HANDLE.lock().unwrap().local_addr().clone(); + let client = AsyncClient::new(addr, None).unwrap(); + Arc::new(Mutex::new(client)) + }; + } + + #[bench] + fn hello(bencher: &mut Bencher) { + let _ = env_logger::init(); + let client = CLIENT.lock().unwrap(); + let concurrency = 100; + let mut futures = Vec::with_capacity(concurrency); + let mut count = 0; + bencher.iter(|| { + futures.push(client.hello("Bob".into())); + count += 1; + if count % concurrency == 0 { + // We can't block on each rpc call, otherwise we'd be + // benchmarking latency instead of throughput. It's also + // not ideal to call more than one rpc per iteration, because + // it makes the output of the bencher harder to parse (you have + // to mentally divide the number by `concurrency` to get + // the ns / iter for one rpc + for f in futures.drain(..) { + f.get().unwrap(); + } + } + }); + } }