Skip to content

Commit

Permalink
examples:massive scratchy: test futures and async
Browse files Browse the repository at this point in the history
  • Loading branch information
niamster committed Feb 11, 2015
1 parent c56dc14 commit 06471f8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
5 changes: 5 additions & 0 deletions examples/itchy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
class Itchy
include Celluloid

attr_accessor :res, :n

def initialize
puts "Ready for mayhem!"
@n = 0
@res = 0
end

def fight
Expand All @@ -33,10 +36,12 @@ def fight
end

def work(value)
@n += 1
res = 0
10000.times do
res += Math.log2(value + 1)
end
@res += res
res
end
end
Expand Down
54 changes: 41 additions & 13 deletions examples/massive-scratchy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,49 @@
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)
def reset(itchies)
itchies.each do |itchy|
itchy.res = 0
itchy.n = 0
end
end

def count(itchies)
itchies.reduce(0) {|sum, itchy| sum + itchy.n}
end

def test_future(itchies)
futures = Array.new
barrel = itchies.cycle
1000.times do |i|
itchy = barrel.next
futures << itchy.future.work(i)
end
futures.reduce(0) {|sum, f| sum + f.value}
end

res = 0
futures.each do |f|
res += f.value
def test_async(itchies)
barrel = itchies.cycle
1000.times do |i|
itchy = barrel.next
itchy.async.work(i)
end
itchies.reduce(0) {|sum, itchy| sum + itchy.res}
end
puts "Test result #{res}"
stop = Time.now
puts "Test executed at #{stop-start}"

def run_test(itchies, name, args=[])
puts "Running test: #{name}"
reset itchies
start = Time.now
res = send "test_#{name}".to_sym, itchies, *args
stop = Time.now
puts "Test result #{res}, count #{count itchies}"
puts "Test executed within #{stop-start}"
end

itchies = DCell[:itchy]

run_test itchies, "future"
run_test itchies, "async"

0 comments on commit 06471f8

Please sign in to comment.