2012-07-24 00:14:43 -05:00
|
|
|
// Compare bounded and unbounded protocol performance.
|
|
|
|
|
|
|
|
// xfail-pretty
|
|
|
|
|
|
|
|
use std;
|
|
|
|
|
|
|
|
import pipes::{spawn_service, recv};
|
|
|
|
import std::time::precise_time_s;
|
|
|
|
|
|
|
|
proto! pingpong {
|
|
|
|
ping: send {
|
|
|
|
ping -> pong
|
|
|
|
}
|
2012-08-22 19:24:52 -05:00
|
|
|
|
2012-07-24 00:14:43 -05:00
|
|
|
pong: recv {
|
|
|
|
pong -> ping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proto! pingpong_unbounded {
|
|
|
|
ping: send {
|
|
|
|
ping -> pong
|
|
|
|
}
|
2012-08-22 19:24:52 -05:00
|
|
|
|
2012-07-24 00:14:43 -05:00
|
|
|
pong: recv {
|
|
|
|
pong -> ping
|
|
|
|
}
|
|
|
|
|
|
|
|
you_will_never_catch_me: send {
|
|
|
|
never_ever_ever -> you_will_never_catch_me
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This stuff should go in libcore::pipes
|
2012-08-06 15:17:35 -05:00
|
|
|
macro_rules! move_it {
|
|
|
|
{ $x:expr } => { let t <- *ptr::addr_of($x); t }
|
2012-07-24 00:14:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! follow {
|
2012-08-22 19:24:52 -05:00
|
|
|
{
|
2012-07-24 00:14:43 -05:00
|
|
|
$($message:path($($x: ident),+) -> $next:ident $e:expr)+
|
|
|
|
} => (
|
2012-08-06 14:34:08 -05:00
|
|
|
|m| match move m {
|
2012-08-06 15:17:35 -05:00
|
|
|
$(some($message($($x,)* next)) => {
|
|
|
|
// FIXME (#2329) use regular move here once move out of
|
|
|
|
// enums is supported.
|
|
|
|
let $next = unsafe { move_it!(next) };
|
|
|
|
$e })+
|
|
|
|
_ => { fail }
|
2012-07-24 00:14:43 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
{
|
2012-07-24 00:14:43 -05:00
|
|
|
$($message:path -> $next:ident $e:expr)+
|
|
|
|
} => (
|
2012-08-06 14:34:08 -05:00
|
|
|
|m| match move m {
|
2012-08-06 15:17:35 -05:00
|
|
|
$(some($message(next)) => {
|
|
|
|
// FIXME (#2329) use regular move here once move out of
|
|
|
|
// enums is supported.
|
|
|
|
let $next = unsafe { move_it!(next) };
|
2012-07-24 00:14:43 -05:00
|
|
|
$e })+
|
2012-08-06 15:17:35 -05:00
|
|
|
_ => { fail }
|
|
|
|
}
|
2012-07-24 00:14:43 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn switch<T: send, Tb: send, U>(+endp: pipes::recv_packet_buffered<T, Tb>,
|
|
|
|
f: fn(+option<T>) -> U) -> U {
|
|
|
|
f(pipes::try_recv(endp))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here's the benchmark
|
|
|
|
|
|
|
|
fn bounded(count: uint) {
|
|
|
|
import pingpong::*;
|
|
|
|
|
|
|
|
let mut ch = do spawn_service(init) |ch| {
|
|
|
|
let mut count = count;
|
|
|
|
let mut ch = ch;
|
|
|
|
while count > 0 {
|
2012-08-22 19:24:52 -05:00
|
|
|
ch = switch(ch, follow! (
|
2012-07-24 00:14:43 -05:00
|
|
|
ping -> next { server::pong(next) }
|
2012-08-22 19:24:52 -05:00
|
|
|
));
|
2012-07-24 00:14:43 -05:00
|
|
|
|
|
|
|
count -= 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut count = count;
|
|
|
|
while count > 0 {
|
|
|
|
let ch_ = client::ping(ch);
|
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
ch = switch(ch_, follow! (
|
2012-07-24 00:14:43 -05:00
|
|
|
pong -> next { next }
|
2012-08-22 19:24:52 -05:00
|
|
|
));
|
2012-07-24 00:14:43 -05:00
|
|
|
|
|
|
|
count -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unbounded(count: uint) {
|
|
|
|
import pingpong_unbounded::*;
|
|
|
|
|
|
|
|
let mut ch = do spawn_service(init) |ch| {
|
|
|
|
let mut count = count;
|
|
|
|
let mut ch = ch;
|
|
|
|
while count > 0 {
|
2012-08-22 19:24:52 -05:00
|
|
|
ch = switch(ch, follow! (
|
2012-07-24 00:14:43 -05:00
|
|
|
ping -> next { server::pong(next) }
|
2012-08-22 19:24:52 -05:00
|
|
|
));
|
2012-07-24 00:14:43 -05:00
|
|
|
|
|
|
|
count -= 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut count = count;
|
|
|
|
while count > 0 {
|
|
|
|
let ch_ = client::ping(ch);
|
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
ch = switch(ch_, follow! (
|
2012-07-24 00:14:43 -05:00
|
|
|
pong -> next { next }
|
2012-08-22 19:24:52 -05:00
|
|
|
));
|
2012-07-24 00:14:43 -05:00
|
|
|
|
|
|
|
count -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn timeit(f: fn()) -> float {
|
|
|
|
let start = precise_time_s();
|
|
|
|
f();
|
|
|
|
let stop = precise_time_s();
|
|
|
|
stop - start
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2012-08-06 15:17:35 -05:00
|
|
|
let count = if os::getenv(~"RUST_BENCH").is_some() {
|
|
|
|
250000
|
|
|
|
} else {
|
|
|
|
100
|
|
|
|
};
|
2012-07-24 00:14:43 -05:00
|
|
|
let bounded = do timeit { bounded(count) };
|
|
|
|
let unbounded = do timeit { unbounded(count) };
|
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
io::println(fmt!("count: %?\n", count));
|
|
|
|
io::println(fmt!("bounded: %? s\t(%? μs/message)",
|
|
|
|
bounded, bounded * 1000000. / (count as float)));
|
|
|
|
io::println(fmt!("unbounded: %? s\t(%? μs/message)",
|
|
|
|
unbounded, unbounded * 1000000. / (count as float)));
|
2012-07-24 00:14:43 -05:00
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
io::println(fmt!("\n\
|
2012-07-24 00:14:43 -05:00
|
|
|
bounded is %?%% faster",
|
2012-08-22 19:24:52 -05:00
|
|
|
(unbounded - bounded) / bounded * 100.));
|
2012-07-24 00:14:43 -05:00
|
|
|
}
|