Move benchmark into tarpc_examples, allowing tarpc to test on stable

This commit is contained in:
Tim Kuehn
2016-02-14 20:33:42 -08:00
parent 09e8cd9af8
commit 1a0054a00f
5 changed files with 74 additions and 70 deletions

View File

@@ -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!()))
}

View File

@@ -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<Mutex<ServeHandle>> = {
let handle = serve("localhost:0", HelloServer, None).unwrap();
Arc::new(Mutex::new(handle))
};
static ref CLIENT: Arc<Mutex<AsyncClient>> = {
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();
}
}
});
}
}

View File

@@ -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();

View File

@@ -1,6 +1,9 @@
[package]
name = "tarpc_examples"
version = "0.1.0"
authors = ["Tim Kuehn <timothy.j.kuehn@gmail.com>"]
authors = ["Adam Wright <adam.austin.wright@gmail.com>", "Tim Kuehn <timothy.j.kuehn@gmail.com>"]
[dependencies]
[dev-dependencies]
tarpc = { path = "../tarpc" }
lazy_static = "^0.1.15"
env_logger = "^0.3.2"

View File

@@ -3,6 +3,71 @@
// Licensed under the MIT License, <LICENSE or http://opensource.org/licenses/MIT>.
// 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<Mutex<ServeHandle>> = {
let handle = serve("localhost:0", HelloServer, None).unwrap();
Arc::new(Mutex::new(handle))
};
static ref CLIENT: Arc<Mutex<AsyncClient>> = {
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();
}
}
});
}
}