Class: EventMachine::HttpTest::LoadTest

Inherits:
Object
  • Object
show all
Includes:
EM::Deferrable
Defined in:
lib/em-http-test/load-test.rb

Overview

Run the specified test in the specified concurrency

Instance Method Summary collapse

Constructor Details

#initialize(concurrency, &block) ⇒ LoadTest

Returns a new instance of LoadTest.



13
14
15
16
17
18
19
20
21
22
# File 'lib/em-http-test/load-test.rb', line 13

def initialize(concurrency, &block)
	@concurrency = concurrency
	@runners = [] # list of currently running tests
	@failureCB = [] # list of failure callbacks
	@successCB = [] # list of success callbacks
	@test = block
	@running = true
	
	@concurrency.times { self.startTest }
end

Instance Method Details

#abortObject

stop the test and don’t execute any more test runners



25
26
27
28
# File 'lib/em-http-test/load-test.rb', line 25

def abort
	@running = false
	@runners.each { |r| r.abort }
end

#countObject



40
41
42
# File 'lib/em-http-test/load-test.rb', line 40

def count
	return @runners.size
end

#onfailure(&block) ⇒ Object

register a failure handler



31
32
33
# File 'lib/em-http-test/load-test.rb', line 31

def onfailure(&block)
	@failureCB << block
end

#onsuccess(&block) ⇒ Object

register a success handler



36
37
38
# File 'lib/em-http-test/load-test.rb', line 36

def onsuccess(&block)
	@successCB << block
end

#startTestObject

Add a new test runner



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/em-http-test/load-test.rb', line 45

def startTest
	return unless @running # don't start new tests if the EventMachine is stopping
	startAt = Time.new
	# create a new runner and add it to the queue
	runner = TestRunner.new(Fiber.new { @test.call })
	@runners << runner
	runner.errback do |e| # if the runner failed
		@runners.delete(runner) # remove it from the queue
		@failureCB.each { |b| b.call(Time.new - startAt, e) } # notify the failure handler
		self.tick # then tick the load test
	end
	runner.callback do # if the runnner completed
		@runners.delete(runner) # remove it from the queue
		@successCB.each { |b| b.call(Time.new - startAt) } # notify the success handler
		self.tick # then tick the load test
	end
end

#tickObject

Replenish test runners



64
65
66
67
68
69
70
71
# File 'lib/em-http-test/load-test.rb', line 64

def tick
	if !@running and @runners.count < 1
		EM.stop
		return
	end
	missing = @concurrency - @runners.count
	missing.times { self.startTest }
end