class CelluloidBenchmark::Visitor

Actor that models a person using a web browser. Runs a test scenario. Delegates web browsing to instance of a Mechanize Agent.

Attributes

benchmark_run[RW]
browser[RW]
current_request_label[RW]
current_request_threshold[RW]
request_end_time[RW]
request_start_time[RW]
target[R]

Public Instance Methods

add_new_browser() click to toggle source
# File lib/celluloid_benchmark/visitor.rb, line 48
def add_new_browser
  @browser = Mechanize.new
  add_browser_timing_hooks
  @browser.user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14"
  @browser.log = mechanize_logger
  @browser
end
benchmark(label, threshold = 0.5) click to toggle source
# File lib/celluloid_benchmark/visitor.rb, line 64
def benchmark(label, threshold = 0.5)
  self.current_request_label = label
  self.current_request_threshold = threshold
end
browser_type(value) click to toggle source
# File lib/celluloid_benchmark/visitor.rb, line 69
def browser_type(value)
  case value
  when :iphone
    browser.user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25"
  when :android
    browser.user_agent = "Mozilla/5.0 (Linux; Android 4.4; Nexus 5 Build/BuildID) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36"
  when :ipad
    browser.user_agent = "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25"
  else
    browser.user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14"
  end
end
log_network_error(error) click to toggle source
# File lib/celluloid_benchmark/visitor.rb, line 108
def log_network_error(error)
  self.request_end_time = Time.now
  benchmark_run.async.log(
    500,
    request_start_time,
    request_end_time,
    server_response_time(browser.current_page),
    current_request_label,
    current_request_threshold
  )
  true
end
log_response(page) click to toggle source
# File lib/celluloid_benchmark/visitor.rb, line 82
def log_response(page)
  benchmark_run.async.log(
    page.code,
    request_start_time,
    request_end_time,
    server_response_time(page),
    current_request_label,
    current_request_threshold
  )

  true
end
log_response_code_error(error) click to toggle source
# File lib/celluloid_benchmark/visitor.rb, line 95
def log_response_code_error(error)
  self.request_end_time = Time.now
  benchmark_run.async.log(
    error.response_code,
    request_start_time,
    request_end_time,
    server_response_time(browser.current_page),
    current_request_label,
    current_request_threshold
  )
  true
end
mechanize_logger() click to toggle source
# File lib/celluloid_benchmark/visitor.rb, line 56
def mechanize_logger
  @mechanize_logger ||= (
    logger = ::Logger.new("log/mechanize.log")
    logger.level = ::Logger::INFO
    logger
  )
end
run_session(benchmark_run, duration, target = nil) click to toggle source
# File lib/celluloid_benchmark/visitor.rb, line 27
def run_session(benchmark_run, duration, target = nil)
  @benchmark_run = benchmark_run
  @target = target

  elapsed_time = 0
  started_at = benchmark_run.started_at
  until elapsed_time >= duration
    begin
      add_new_browser
      Session.run self
    rescue Mechanize::ResponseCodeError => e
      log_response_code_error e
    rescue Errno::ETIMEDOUT, Net::ReadTimeout, Timeout::Error => e
      log_network_error e
    end

    elapsed_time = Time.now - started_at
  end
  elapsed_time
end

Private Instance Methods

add_browser_timing_hooks() click to toggle source
# File lib/celluloid_benchmark/visitor.rb, line 124
def add_browser_timing_hooks
  browser.pre_connect_hooks << proc do |agent, request|
    self.request_start_time = Time.now
  end

  browser.post_connect_hooks << proc do |agent, uri, response, body|
    self.request_end_time = Time.now
  end
end
server_response_time(page) click to toggle source
# File lib/celluloid_benchmark/visitor.rb, line 134
def server_response_time(page)
  if page && page["x-runtime"]
    page["x-runtime"].to_f
  end
end