1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
// 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.
// Compare bounded and unbounded protocol performance.
// xfail-pretty
extern mod std;
use core::cell::Cell;
use core::pipes::*;
use std::time::precise_time_s;
proto! pingpong (
ping: send {
ping -> pong
}
pong: recv {
pong -> ping
}
)
proto! pingpong_unbounded (
ping: send {
ping -> pong
}
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
macro_rules! move_it (
{ $x:expr } => { let t = *ptr::to_unsafe_ptr(&($x)); t }
)
macro_rules! follow (
{
$($message:path($($x: ident),+) -> $next:ident $e:expr)+
} => (
|m| match m {
$(Some($message($($x,)* next)) => {
let $next = next;
$e })+
_ => { fail!() }
}
);
{
$($message:path -> $next:ident $e:expr)+
} => (
|m| match m {
$(Some($message(next)) => {
let $next = next;
$e })+
_ => { fail!() }
}
)
)
/** Spawn a task to provide a service.
It takes an initialization function that produces a send and receive
endpoint. The send endpoint is returned to the caller and the receive
endpoint is passed to the new task.
*/
pub fn spawn_service<T:Owned,Tb:Owned>(
init: extern fn() -> (SendPacketBuffered<T, Tb>,
RecvPacketBuffered<T, Tb>),
service: ~fn(v: RecvPacketBuffered<T, Tb>))
-> SendPacketBuffered<T, Tb> {
let (client, server) = init();
// This is some nasty gymnastics required to safely move the pipe
// into a new task.
let server = Cell(server);
do task::spawn {
service(server.take());
}
client
}
/** Like `spawn_service_recv`, but for protocols that start in the
receive state.
*/
pub fn spawn_service_recv<T:Owned,Tb:Owned>(
init: extern fn() -> (RecvPacketBuffered<T, Tb>,
SendPacketBuffered<T, Tb>),
service: ~fn(v: SendPacketBuffered<T, Tb>))
-> RecvPacketBuffered<T, Tb> {
let (client, server) = init();
// This is some nasty gymnastics required to safely move the pipe
// into a new task.
let server = Cell(server);
do task::spawn {
service(server.take())
}
client
}
fn switch<T:Owned,Tb:Owned,U>(+endp: core::pipes::RecvPacketBuffered<T, Tb>,
f: &fn(+v: Option<T>) -> U) -> U {
f(core::pipes::try_recv(endp))
}
// Here's the benchmark
fn bounded(count: uint) {
use pingpong::*;
let mut ch = do spawn_service(init) |ch| {
let mut count = count;
let mut ch = ch;
while count > 0 {
ch = switch(ch, follow! (
ping -> next { server::pong(next) }
));
count -= 1;
}
};
let mut count = count;
while count > 0 {
let ch_ = client::ping(ch);
ch = switch(ch_, follow! (
pong -> next { next }
));
count -= 1;
}
}
fn unbounded(count: uint) {
use pingpong_unbounded::*;
let mut ch = do spawn_service(init) |ch| {
let mut count = count;
let mut ch = ch;
while count > 0 {
ch = switch(ch, follow! (
ping -> next { server::pong(next) }
));
count -= 1;
}
};
let mut count = count;
while count > 0 {
let ch_ = client::ping(ch);
ch = switch(ch_, follow! (
pong -> next { next }
));
count -= 1;
}
}
fn timeit(f: &fn()) -> float {
let start = precise_time_s();
f();
let stop = precise_time_s();
stop - start
}
fn main() {
let count = if os::getenv(~"RUST_BENCH").is_some() {
250000
} else {
100
};
let bounded = do timeit { bounded(count) };
let unbounded = do timeit { unbounded(count) };
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)));
io::println(fmt!("\n\
bounded is %?%% faster",
(unbounded - bounded) / bounded * 100.));
}
|