Skip to content
Browse files

Added celluloid for async warriors

  • Loading branch information...
1 parent 49bb0dc commit dcfcf6d92dc42d67899242e9fb87acfcc5d20715 @yeahiii yeahiii committed Feb 17, 2012
View
1 Gemfile
@@ -2,6 +2,7 @@ source "http://rubygems.org"
gem "fog"
gem "sshkey"
+gem "celluloid"
group :test do
gem "rake"
View
4 config/credentials.yaml
@@ -7,5 +7,5 @@ rackspace:
key: "r_key"
amazonec2:
- id: "AKIAJAASZKI4AWGWBB5A"
- key: "3kIeESmXmgM5echsOhnJOZuhmv50OdJj0SvIsskP"
+ id: ""
+ key: ""
View
2 doc/dsl_prototype1.rb
@@ -13,7 +13,7 @@
@warrior = @bootcamp.new_warrior
@warrior.arm(Weapon.new(:apache_benchmark))
-@warrior.attack!(:target => 'heise.de', {:intensity=>500, :ramp_up=>50})
+@warrior.attack(:target => 'heise.de', {:intensity=>500, :ramp_up=>50})
csv = @warrior.collect_results
View
4 examples/single_warrior.rb
@@ -6,5 +6,5 @@
warrior = Warrior.new(:provider => :amazonec2)
weapon = Weapon.create(:ApacheBenchmark)
warrior.arm(weapon)
-warrior.attack!('http://blog.roothausen.de/', { :requests => 100, :clients => 20 })
-warrior.kill!
+warrior.attack('http://blog.roothausen.de/', { :requests => 100, :clients => 20 })
+warrior.kill
View
2 ext/boot_camps/amazon_ec2.rb
@@ -26,6 +26,8 @@ def connect!(env = {})
:public_key_path => '~/.ssh/id_rsa.pub',
:tags => {:sparta_session_id => Sparta::session_uuid}
)
+
+
Sparta::BootCamp.running_instances << @instance
@instance.id
View
40 lib/sparta/squad.rb
@@ -1,3 +1,8 @@
+require 'rubygems'
+require 'bundler/setup'
+
+require 'celluloid'
+
module Sparta
class Squad
attr_reader :warriors
@@ -8,30 +13,26 @@ def initialize(count, env = {})
end
def add_warriors(count, env = {})
- threads = []
+ futures = []
(0 ... count).each do |number|
- threads << Thread.new do
- retry_count = env[:max_retry] || 0
+ futures << Celluloid::Future.new { Warrior.new(env) }
+ end
+ Timeout::timeout(360) do
+ futures.each do |f|
begin
- @warriors << Warrior.new(env)
- rescue
- if retry_count > 0
- retry_count -= 1
- retry
- else
-
- raise "Warrior not reachable after #{env[:max_retry]} retries."
+ Timeout::timeout(8) do
+ if(f.value)
+ @warriors << f.value
+ end
end
+ rescue
+ sleep(2)
+ retry
end
end
end
-
- threads.each do |t|
- t.join
- end
-
end
def arm(weapon)
@@ -49,6 +50,7 @@ def is_armed?
end
def attack!(env = {})
+
if(env[:ramp_up])
ramp_up_time = env[:ramp_up][:period]
raise "Squad attack ramp up: period missing" unless ramp_up_time
@@ -57,15 +59,15 @@ def attack!(env = {})
if ramp_up_function == :linear
delays = Array.new(@warriors.size){ramp_up_time / @warriors.size}
- @warriors.each_with_index { |warrior,index| warrior.attack!('http://localhost/'); sleep(delays[index]); puts 'attacking' }
+ @warriors.each_with_index { |warrior,index| warrior.attack('http://localhost/'); sleep(delays[index]);}
end
else
- @warriors.each { |warrior| warrior.attack!('http://localhost') }
+ @warriors.each { |warrior| warrior.attack('http://localhost') }
end
end
def kill!
- @warriors.each { |warrior| warrior.kill! }
+ @warriors.each { |warrior| warrior.kill }
@warriors.clear
end
end
View
15 lib/sparta/warrior.rb
@@ -1,11 +1,13 @@
require 'rubygems'
require 'bundler/setup'
-require 'net/ssh'
+#require 'net/ssh'
require 'timeout'
+require 'celluloid'
module Sparta
class Warrior
+
attr_accessor :state
attr_accessor :weapon
attr_accessor :bootcamp
@@ -15,16 +17,17 @@ def initialize(bootcamp_params = {})
Timeout::timeout(360) do
@bootcamp = Sparta::BootCamp.create_instance(bootcamp_params)
@instance_id = @bootcamp.connect!
+ self
end
rescue
- raise "Warrior initialization failed"
+ raise "Warrior init failed"
end
end
-
+
def arm(weapon)
@weapon = weapon
@weapon.install(@bootcamp)
-
+
if(@weapon.is_working?)
begin
@bootcamp.add_tag('weapon', weapon.class.name)
@@ -39,13 +42,13 @@ def is_armed?
not @weapon.nil?
end
- def attack!(target, options={})
+ def attack(target, options={})
raise "need a target!" unless target
raise "need a connection!" unless self.bootcamp
@weapon.use(target, options)
end
- def kill!
+ def kill
@bootcamp.kill!
end
View
4 test/test_integration.rb
@@ -8,8 +8,8 @@ def test_warrior_and_bootcamp
warrior = Warrior.new(:provider => :localprovider)
weapon = Weapon.create(:ApacheBenchmark)
warrior.arm(weapon)
- warrior.attack!('http://blog.roothausen.de/', { :requests => 100, :clients => 20 })
- warrior.kill!
+ warrior.attack('http://blog.roothausen.de/', { :requests => 100, :clients => 20 })
+ warrior.kill
end
end
View
8 test/test_squad.rb
@@ -15,14 +15,6 @@ def test_creating_squad
assert_equal 3, squad.warriors.length
end
- def test_creating_squad_failing_warrior
- Sparta::Warrior.stubs(:new).raises(StandardError, 'warrior failed')
-
- assert_raise RuntimeError do
- squad = Sparta::Squad.new(3,{:provider => :localprovider})
- end
- end
-
def test_removing_squad
squad = Sparta::Squad.new(3,{:provider => :localprovider})
squad.kill!
View
2 test/test_warrior.rb
@@ -24,7 +24,7 @@ def test_warrior
@target = 'http://localhost/'
- @warrior.attack!(@target)
+ @warrior.attack(@target)
end
View
2 test/test_weapon.rb
@@ -49,7 +49,7 @@ def test_retrieve_asets
@warrior = Sparta::Warrior.new({:provider=>'localprovider'})
@weapon = Weapon.create(:ApacheBenchmark)
@warrior.arm(@weapon)
- @warrior.attack!("http://momo.brauchtman.net/")
+ @warrior.attack("http://momo.brauchtman.net/")
@weapon.retrieve_results
result = @weapon.result

0 comments on commit dcfcf6d

Please sign in to comment.
Something went wrong with that request. Please try again.