From 06471f88abd30babecf10950d63dc9f2edd0cb10 Mon Sep 17 00:00:00 2001 From: Dmytro Milinevskyy Date: Thu, 12 Feb 2015 00:33:01 +0100 Subject: [PATCH] examples:massive scratchy: test futures and async --- examples/itchy.rb | 5 ++++ examples/massive-scratchy.rb | 54 +++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/examples/itchy.rb b/examples/itchy.rb index 1db1002..25aa903 100755 --- a/examples/itchy.rb +++ b/examples/itchy.rb @@ -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 @@ -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 diff --git a/examples/massive-scratchy.rb b/examples/massive-scratchy.rb index fbb40a7..6c3de4c 100755 --- a/examples/massive-scratchy.rb +++ b/examples/massive-scratchy.rb @@ -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"