Class: VisitBench

Inherits:
Object
  • Object
show all
Defined in:
lib/visit_bench.rb

Overview

Visitbench stresstests your server by simulation real user sessions.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ VisitBench

Returns a new instance of VisitBench.

Yields:

  • (_self)

Yield Parameters:

  • _self (VisitBench)

    the object that the method was called on



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/visit_bench.rb', line 11

def initialize(options = {})
  @host = options[:host] || 'localhost'
  @browser = Mechanize.new 
  @browser.log = Logger.new(STDOUT) if options[:debug]
  options[:headers] ||= []
  options[:headers].each do |k,v|
    @browser.request_headers[k] = v
  end
  
  @browser.basic_auth(options[:username], options[:password]) if options[:username]
  @sessions = options[:sessions] || []
  yield self
end

Class Method Details

.benchmark(&block) ⇒ Object



53
54
55
56
57
# File 'lib/visit_bench.rb', line 53

def benchmark(&block)
  begin_time = Time.now
  yield
  return Time.now - begin_time 
end

Instance Method Details

#add_session {|s| ... } ⇒ Object

add a session with preinitialized values

Yields:

  • (s)


26
27
28
29
30
# File 'lib/visit_bench.rb', line 26

def add_session
  s = Session.new(:request_options => {:host => @host, :browser => @browser})
  yield s
  @sessions << s
end

#run(options = {}) ⇒ Object

run the sessions multiple times and run them in parallel



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/visit_bench.rb', line 33

def run(options = {})
  amt_times = options[:amt_times] || 10
  concurrent_users = options[:concurrent_users] || 3
  run_times = []
  amt_times.times do |i|
      trs = []
      concurrent_users.times do
        puts "Starting thread"
        trs << Thread.new do      
          session = @sessions[(@sessions.length - 1) * rand]
          run_times << session.run
        end
      end
      trs.each {|x| x.join }
  end
  run_times
end