Skip to content

Commit

Permalink
examples: showcase on how to distribute load between multiple nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
niamster committed Feb 10, 2015
1 parent 5fad418 commit 91a38d8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
33 changes: 33 additions & 0 deletions examples/itchy-launcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env ruby

require 'optparse'

pids = Array.new
count = 1

OptionParser.new do |opts|
opts.banner = "Usage: itchy-launcher.rb [options]"

opts.on("--count COUNT", OptionParser::DecimalInteger, "Number of itchy instances") do |v|
count = v
end
end.parse!

Signal.trap(:INT) do
pids.each do |pid|
begin
Process.kill :TERM, pid if pid
rescue
end
end
end

count.times do |id|
pid = Process.spawn Gem.ruby, File.expand_path("./examples/itchy.rb"), "--id", "itchy-#{Process.pid}-#{id}"
unless pid
STDERR.print "ERROR: Couldn't start test node."
end
pids << pid
end

Process.waitall
23 changes: 22 additions & 1 deletion examples/itchy.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#!/usr/bin/env ruby

require 'dcell'
require 'optparse'
require_relative 'registry'

id = 'itchy'

OptionParser.new do |opts|
opts.banner = "Usage: itchy.rb [options]"

opts.on("--id ID", "Assign node ID") do |v|
id = v
end
end.parse!

class Itchy
include Celluloid

Expand All @@ -19,9 +31,18 @@ def fight
end
@n
end

def work(value)
res = 0
10000.times do
res += Math.log2(value + 1)
end
res
end
end
Itchy.supervise_as :itchy

DCell.start :id => "itchy", :registry => registry
puts "Starting itchy with ID '#{id}'"
DCell.start :id =>id, :registry => registry

sleep
23 changes: 23 additions & 0 deletions examples/massive-scratchy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env ruby
require 'dcell'
require_relative 'registry'

DCell.start :registry => registry

itchies = DCell[:itchy].cycle
puts "Making itchy work hard everywhere!"

start = Time.now
futures = Array.new
1000.times do |i|
itchy = itchies.next
futures << itchy.future.work(i)
end

res = 0
futures.each do |f|
res += f.value
end
puts "Test result #{res}"
stop = Time.now
puts "Test executed at #{stop-start}"

0 comments on commit 91a38d8

Please sign in to comment.