Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 117 lines (96 sloc) 3.185 kb
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
1 // This test creates a bunch of tasks that simultaneously send to each
2 // other in a ring. The messages should all be basically
3 // independent.
4 // This is like msgsend-ring-pipes but adapted to use ARCs.
5
6 // This also serves as a pipes test, because ARCs are implemented with pipes.
7
8 // xfail-pretty
9
f686896 @pcwalton test: "import" -> "use"
pcwalton authored
10 use future::future;
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
11
ea01ee2 @brson Convert 'use' to 'extern mod'. Remove old 'use' syntax
brson authored
12 extern mod std;
f686896 @pcwalton test: "import" -> "use"
pcwalton authored
13 use std::time;
14 use std::arc;
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
15
16 // A poor man's pipe.
3303116 @bblum fix bench test for arc::arc camelcase
bblum authored
17 type pipe = arc::RWARC<~[uint]>;
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
18
19 fn send(p: &pipe, msg: uint) {
20 do p.write_cond |state, cond| {
67a8e71 @nikomatsakis Demode vec::push (and convert to method)
nikomatsakis authored
21 state.push(msg);
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
22 cond.signal();
23 }
24 }
25 fn recv(p: &pipe) -> uint {
26 do p.write_cond |state, cond| {
27 while vec::is_empty(*state) {
28 cond.wait();
29 }
21519bc @nikomatsakis demode vec
nikomatsakis authored
30 state.pop()
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
31 }
32 }
33
34 fn init() -> (pipe,pipe) {
aab4d6b @brson std: Camel case some constructors
brson authored
35 let x = arc::RWARC(~[]);
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
36 ((&x).clone(), x)
37 }
38
39
40 fn thread_ring(i: uint,
41 count: uint,
42 +num_chan: pipe,
43 +num_port: pipe) {
8337fa1 @brson Camel case the option type
brson authored
44 let mut num_chan <- Some(num_chan);
45 let mut num_port <- Some(num_port);
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
46 // Send/Receive lots of messages.
47 for uint::range(0u, count) |j| {
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
48 //error!("task %?, iter %?", i, j);
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
49 let mut num_chan2 = option::swap_unwrap(&mut num_chan);
50 let mut num_port2 = option::swap_unwrap(&mut num_port);
51 send(&num_chan2, i * j);
8337fa1 @brson Camel case the option type
brson authored
52 num_chan = Some(num_chan2);
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
53 let _n = recv(&num_port2);
54 //log(error, _n);
8337fa1 @brson Camel case the option type
brson authored
55 num_port = Some(num_port2);
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
56 };
57 }
58
267ab11 @nikomatsakis use + mode for (almost) everything when not using legacy modes
nikomatsakis authored
59 fn main(++args: ~[~str]) {
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
60 let args = if os::getenv(~"RUST_BENCH").is_some() {
61 ~[~"", ~"100", ~"10000"]
62 } else if args.len() <= 1u {
63 ~[~"", ~"10", ~"100"]
64 } else {
65 copy args
66 };
67
4a78f9b @brson core: Demode option
brson authored
68 let num_tasks = uint::from_str(args[1]).get();
69 let msg_per_task = uint::from_str(args[2]).get();
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
70
71 let (num_chan, num_port) = init();
8337fa1 @brson Camel case the option type
brson authored
72 let mut num_chan = Some(num_chan);
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
73
74 let start = time::precise_time_s();
75
76 // create the ring
77 let mut futures = ~[];
78
79 for uint::range(1u, num_tasks) |i| {
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
80 //error!("spawning %?", i);
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
81 let (new_chan, num_port) = init();
8337fa1 @brson Camel case the option type
brson authored
82 let num_chan2 = ~mut None;
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
83 *num_chan2 <-> num_chan;
8337fa1 @brson Camel case the option type
brson authored
84 let num_port = ~mut Some(num_port);
8e86cd0 @olsonjeffery test: bunch of test cleanup from sendable_futures change
olsonjeffery authored
85 let new_future = do future::spawn
86 |move num_chan2, move num_port| {
8337fa1 @brson Camel case the option type
brson authored
87 let mut num_chan = None;
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
88 num_chan <-> *num_chan2;
8337fa1 @brson Camel case the option type
brson authored
89 let mut num_port1 = None;
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
90 num_port1 <-> *num_port;
91 thread_ring(i, msg_per_task,
92 option::unwrap(num_chan),
93 option::unwrap(num_port1))
8e86cd0 @olsonjeffery test: bunch of test cleanup from sendable_futures change
olsonjeffery authored
94 };
67a8e71 @nikomatsakis Demode vec::push (and convert to method)
nikomatsakis authored
95 futures.push(new_future);
8337fa1 @brson Camel case the option type
brson authored
96 num_chan = Some(new_chan);
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
97 };
98
99 // do our iteration
100 thread_ring(0u, msg_per_task, option::unwrap(num_chan), num_port);
101
102 // synchronize
cfed923 @nikomatsakis demode the each() method on vec and other iterables.
nikomatsakis authored
103 for futures.each |f| { future::get(f) };
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
104
105 let stop = time::precise_time_s();
106
107 // all done, report stats.
108 let num_msgs = num_tasks * msg_per_task;
109 let elapsed = (stop - start);
110 let rate = (num_msgs as float) / elapsed;
111
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
112 io::println(fmt!("Sent %? messages in %? seconds",
113 num_msgs, elapsed));
114 io::println(fmt!(" %? messages / second", rate));
115 io::println(fmt!(" %? μs / message", 1000000. / rate));
7fe1005 @bblum Add msgsend-ring-{rw,mutex}-arcs bench tests
bblum authored
116 }
Something went wrong with that request. Please try again.