Class: CelluloidBenchmark::Visitor

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Celluloid, DataSources, CelluloidBenchmark::Visitors::HTTPMethods
Defined in:
lib/celluloid_benchmark/visitor.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CelluloidBenchmark::Visitors::HTTPMethods

#get, #get_json, #post, #post_json, #put, #put_json

Methods included from DataSources

#data_source, #data_sources, #data_sources=, #empty?, #raise_empty, #random_data

Instance Attribute Details

#benchmark_runObject

Returns the value of attribute benchmark_run.



19
20
21
# File 'lib/celluloid_benchmark/visitor.rb', line 19

def benchmark_run
  @benchmark_run
end

#browserObject

Returns the value of attribute browser.



20
21
22
# File 'lib/celluloid_benchmark/visitor.rb', line 20

def browser
  @browser
end

#current_request_labelObject

Returns the value of attribute current_request_label.



21
22
23
# File 'lib/celluloid_benchmark/visitor.rb', line 21

def current_request_label
  @current_request_label
end

#current_request_thresholdObject

Returns the value of attribute current_request_threshold.



22
23
24
# File 'lib/celluloid_benchmark/visitor.rb', line 22

def current_request_threshold
  @current_request_threshold
end

#request_end_timeObject

Returns the value of attribute request_end_time.



24
25
26
# File 'lib/celluloid_benchmark/visitor.rb', line 24

def request_end_time
  @request_end_time
end

#request_start_timeObject

Returns the value of attribute request_start_time.



23
24
25
# File 'lib/celluloid_benchmark/visitor.rb', line 23

def request_start_time
  @request_start_time
end

#targetObject (readonly)

Returns the value of attribute target.



25
26
27
# File 'lib/celluloid_benchmark/visitor.rb', line 25

def target
  @target
end

Instance Method Details

#add_new_browserObject



48
49
50
51
52
53
54
# 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) ⇒ Object



64
65
66
67
# 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) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# 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) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# 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_loggerObject



56
57
58
59
60
61
62
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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