-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
ring.rb
executable file
·63 lines (50 loc) · 1.02 KB
/
ring.rb
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
#!/usr/bin/env ruby
$:.push File.expand_path('../../lib', __FILE__)
require 'celluloid'
class Ring
include Celluloid
class Node
include Celluloid
def initialize(link)
@link = link
end
def around(n)
@link.async.around n
end
end
def initialize(size)
@node = Node.new_link current_actor
size.times do
@node = Node.new_link @node
end
end
# Go around the ring the given number of times
def run(n)
if n < 0
raise ArgumentError, "I can't go around a negative number of times"
end
async.around n
wait :done
end
# Go around the ring the given number of times
def around(n)
if n.zero?
signal :done
else
@node.async.around n - 1
end
end
end
if $0 == __FILE__
require 'benchmark'
SIZE = 512
TIMES = 10
puts "*** Creating a #{SIZE} node ring..."
puts Benchmark.measure {
$ring = Ring.new(SIZE)
}
puts "*** Sending a message around #{TIMES} times"
puts Benchmark.measure {
$ring.run(TIMES)
}
end