File: ring.rb

package info (click to toggle)
ruby-celluloid 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 848 kB
  • sloc: ruby: 7,579; makefile: 10
file content (62 lines) | stat: -rwxr-xr-x 1,062 bytes parent folder | download
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
#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../lib", __dir__)
require "celluloid/autostart"

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)
    raise ArgumentError, "I can't go around a negative number of times" if n < 0

    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 $PROGRAM_NAME == __FILE__
  require "benchmark"
  SIZE  = 512
  TIMES = 10
  ring = nil

  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