2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-24 00:14:43 -05:00
|
|
|
// Compare bounded and unbounded protocol performance.
|
|
|
|
|
2013-01-28 20:36:15 -06:00
|
|
|
// xfail-pretty
|
|
|
|
|
2012-09-11 19:46:20 -05:00
|
|
|
extern mod std;
|
2012-07-24 00:14:43 -05:00
|
|
|
|
2013-02-02 05:10:12 -06:00
|
|
|
use core::pipes::{spawn_service, recv};
|
2012-09-05 14:32:05 -05:00
|
|
|
use std::time::precise_time_s;
|
2012-07-24 00:14:43 -05:00
|
|
|
|
2012-08-22 20:10:48 -05:00
|
|
|
proto! pingpong (
|
2012-07-24 00:14:43 -05:00
|
|
|
ping: send {
|
|
|
|
ping -> pong
|
|
|
|
}
|
2012-08-22 19:24:52 -05:00
|
|
|
|
2012-07-24 00:14:43 -05:00
|
|
|
pong: recv {
|
|
|
|
pong -> ping
|
|
|
|
}
|
2012-08-22 20:10:48 -05:00
|
|
|
)
|
2012-07-24 00:14:43 -05:00
|
|
|
|
2012-08-22 20:10:48 -05:00
|
|
|
proto! pingpong_unbounded (
|
2012-07-24 00:14:43 -05:00
|
|
|
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
|
|
|
|
}
|
2012-08-22 20:10:48 -05:00
|
|
|
)
|
2012-07-24 00:14:43 -05:00
|
|
|
|
|
|
|
// This stuff should go in libcore::pipes
|
2012-08-22 19:47:11 -05:00
|
|
|
macro_rules! move_it (
|
2013-02-15 04:44:18 -06:00
|
|
|
{ $x:expr } => { let t = *ptr::addr_of(&($x)); t }
|
2012-08-22 19:47:11 -05:00
|
|
|
)
|
2012-07-24 00:14:43 -05:00
|
|
|
|
2012-08-22 19:47:11 -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)+
|
|
|
|
} => (
|
2013-02-15 04:44:18 -06:00
|
|
|
|m| match m {
|
|
|
|
$(Some($message($($x,)* next)) => {
|
|
|
|
let $next = next;
|
|
|
|
$e })+
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => { 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)+
|
|
|
|
} => (
|
2013-02-15 04:44:18 -06:00
|
|
|
|m| match m {
|
|
|
|
$(Some($message(next)) => {
|
|
|
|
let $next = next;
|
|
|
|
$e })+
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => { fail!() }
|
2012-08-06 15:17:35 -05:00
|
|
|
}
|
2012-07-24 00:14:43 -05:00
|
|
|
)
|
2012-08-22 19:47:11 -05:00
|
|
|
)
|
2012-07-24 00:14:43 -05:00
|
|
|
|
2013-02-02 05:10:12 -06:00
|
|
|
fn switch<T:Owned,Tb:Owned,U>(+endp: core::pipes::RecvPacketBuffered<T, Tb>,
|
2013-03-07 16:38:38 -06:00
|
|
|
f: &fn(+v: Option<T>) -> U) -> U {
|
2013-02-02 05:10:12 -06:00
|
|
|
f(core::pipes::try_recv(endp))
|
2012-07-24 00:14:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Here's the benchmark
|
|
|
|
|
|
|
|
fn bounded(count: uint) {
|
2012-09-07 20:08:21 -05:00
|
|
|
use pingpong::*;
|
2012-07-24 00:14:43 -05:00
|
|
|
|
|
|
|
let mut ch = do spawn_service(init) |ch| {
|
|
|
|
let mut count = count;
|
2013-02-15 04:44:18 -06:00
|
|
|
let mut ch = ch;
|
2012-07-24 00:14:43 -05:00
|
|
|
while count > 0 {
|
2013-02-15 04:44:18 -06:00
|
|
|
ch = switch(ch, follow! (
|
|
|
|
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 {
|
2013-02-15 04:44:18 -06:00
|
|
|
let ch_ = client::ping(ch);
|
2012-07-24 00:14:43 -05:00
|
|
|
|
2013-02-15 04:44:18 -06:00
|
|
|
ch = switch(ch_, follow! (
|
|
|
|
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) {
|
2012-09-07 20:08:21 -05:00
|
|
|
use pingpong_unbounded::*;
|
2012-07-24 00:14:43 -05:00
|
|
|
|
|
|
|
let mut ch = do spawn_service(init) |ch| {
|
|
|
|
let mut count = count;
|
2013-02-15 04:44:18 -06:00
|
|
|
let mut ch = ch;
|
2012-07-24 00:14:43 -05:00
|
|
|
while count > 0 {
|
2013-02-15 04:44:18 -06:00
|
|
|
ch = switch(ch, follow! (
|
|
|
|
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 {
|
2013-02-15 04:44:18 -06:00
|
|
|
let ch_ = client::ping(ch);
|
2012-07-24 00:14:43 -05:00
|
|
|
|
2013-02-15 04:44:18 -06:00
|
|
|
ch = switch(ch_, follow! (
|
|
|
|
pong -> next { next }
|
2012-08-22 19:24:52 -05:00
|
|
|
));
|
2012-07-24 00:14:43 -05:00
|
|
|
|
|
|
|
count -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 16:38:38 -06:00
|
|
|
fn timeit(f: &fn()) -> float {
|
2012-07-24 00:14:43 -05:00
|
|
|
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
|
|
|
}
|