Class: PerfCheck::TestCase
- Inherits:
-
Object
- Object
- PerfCheck::TestCase
- Defined in:
- lib/perf_check/test_case.rb
Instance Attribute Summary collapse
-
#cookie ⇒ Object
Returns the value of attribute cookie.
-
#error_backtrace ⇒ Object
Returns the value of attribute error_backtrace.
-
#http_status ⇒ Object
Returns the value of attribute http_status.
-
#max_memory ⇒ Object
Returns the value of attribute max_memory.
-
#perf_check ⇒ Object
readonly
Returns the value of attribute perf_check.
-
#reference_profiles ⇒ Object
Returns the value of attribute reference_profiles.
-
#reference_response ⇒ Object
Returns the value of attribute reference_response.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#this_profiles ⇒ Object
Returns the value of attribute this_profiles.
-
#this_response ⇒ Object
Returns the value of attribute this_response.
Instance Method Summary collapse
- #eql?(test) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(perf_check, route) ⇒ TestCase
constructor
A new instance of TestCase.
- #issue_request(server, options) ⇒ Object
- #latency_difference ⇒ Object
- #reference_latency ⇒ Object
- #reference_query_count ⇒ Object
- #request_headers ⇒ Object
- #response_diff ⇒ Object
- #run(server, options) ⇒ Object
- #speedup_factor ⇒ Object
- #switch_to_reference_context ⇒ Object
- #this_latency ⇒ Object
- #this_query_count ⇒ Object
Constructor Details
#initialize(perf_check, route) ⇒ TestCase
13 14 15 16 17 18 |
# File 'lib/perf_check/test_case.rb', line 13 def initialize(perf_check, route) @perf_check = perf_check self.this_profiles = [] self.reference_profiles = [] self.resource = route end |
Instance Attribute Details
#cookie ⇒ Object
Returns the value of attribute cookie.
9 10 11 |
# File 'lib/perf_check/test_case.rb', line 9 def end |
#error_backtrace ⇒ Object
Returns the value of attribute error_backtrace.
11 12 13 |
# File 'lib/perf_check/test_case.rb', line 11 def error_backtrace @error_backtrace end |
#http_status ⇒ Object
Returns the value of attribute http_status.
11 12 13 |
# File 'lib/perf_check/test_case.rb', line 11 def http_status @http_status end |
#max_memory ⇒ Object
Returns the value of attribute max_memory.
11 12 13 |
# File 'lib/perf_check/test_case.rb', line 11 def max_memory @max_memory end |
#perf_check ⇒ Object (readonly)
Returns the value of attribute perf_check.
7 8 9 |
# File 'lib/perf_check/test_case.rb', line 7 def perf_check @perf_check end |
#reference_profiles ⇒ Object
Returns the value of attribute reference_profiles.
10 11 12 |
# File 'lib/perf_check/test_case.rb', line 10 def reference_profiles @reference_profiles end |
#reference_response ⇒ Object
Returns the value of attribute reference_response.
9 10 11 |
# File 'lib/perf_check/test_case.rb', line 9 def reference_response @reference_response end |
#resource ⇒ Object
Returns the value of attribute resource.
8 9 10 |
# File 'lib/perf_check/test_case.rb', line 8 def resource @resource end |
#this_profiles ⇒ Object
Returns the value of attribute this_profiles.
10 11 12 |
# File 'lib/perf_check/test_case.rb', line 10 def this_profiles @this_profiles end |
#this_response ⇒ Object
Returns the value of attribute this_response.
9 10 11 |
# File 'lib/perf_check/test_case.rb', line 9 def this_response @this_response end |
Instance Method Details
#eql?(test) ⇒ Boolean
109 110 111 |
# File 'lib/perf_check/test_case.rb', line 109 def eql?(test) resource == test.resource end |
#hash ⇒ Object
113 114 115 |
# File 'lib/perf_check/test_case.rb', line 113 def hash resource.hash end |
#issue_request(server, options) ⇒ Object
117 118 119 120 121 |
# File 'lib/perf_check/test_case.rb', line 117 def issue_request(server, ) server.profile do |http| http.get(resource, request_headers) end end |
#latency_difference ⇒ Object
85 86 87 |
# File 'lib/perf_check/test_case.rb', line 85 def latency_difference this_latency - reference_latency end |
#reference_latency ⇒ Object
71 72 73 74 |
# File 'lib/perf_check/test_case.rb', line 71 def reference_latency return nil if reference_profiles.empty? reference_profiles.map(&:latency).inject(0.0, :+) / reference_profiles.size end |
#reference_query_count ⇒ Object
80 81 82 83 |
# File 'lib/perf_check/test_case.rb', line 80 def reference_query_count return nil if reference_profiles.empty? reference_profiles.map(&:query_count).inject(0, :+) / reference_profiles.size end |
#request_headers ⇒ Object
123 124 125 126 127 |
# File 'lib/perf_check/test_case.rb', line 123 def request_headers headers = {'Cookie' => "#{cookie}".strip} headers['Accept'] = 'text/html,application/xhtml+xml,application/xml' headers.merge!(perf_check..headers) end |
#response_diff ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/perf_check/test_case.rb', line 93 def response_diff diff = Diffy::Diff.new(reference_response, this_response, include_diff_info: true, diff: perf_check..) if diff.to_s(:text).lines.length < 3 OpenStruct.new(:changed? => false) else FileUtils.mkdir_p("#{perf_check.app_root}/tmp/perf_check/diffs") file = `mktemp -u "#{perf_check.app_root}/tmp/perf_check/diffs/XXXXXXXXX"`.strip File.open("#{file}.diff", "w") do |f| f.write(diff.to_s(:text).lines[2..-1].join) end OpenStruct.new(:changed? => true, :file => "#{file}.diff") end end |
#run(server, options) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/perf_check/test_case.rb', line 20 def run(server, ) unless .diff perf_check.logger.info("\t"+['request', 'latency', 'server rss', 'status', 'queries', 'profiler data'].map(&:underline).join(" ")) end (.number_of_requests + 2).times do |i| profile = issue_request(server, ) next if i < 2 # first 2 requests warm up the server/db and get tossed if .verify_no_diff && i == 2 response_for_comparison(profile.response_body) end unless .diff row = sprintf("\t%2i:\t %.1fms %4dMB\t %s\t %s\t %s", i, profile.latency, profile.server_memory, profile.response_code, profile.query_count, profile.profile_url) perf_check.logger.info(row) self.max_memory = profile.server_memory end context_profiles << profile self.http_status = '200' unless .http_statuses.include?(profile.response_code) self.http_status = profile.response_code error = sprintf("\t :\tFAILED! (HTTP %d)", profile.response_code) perf_check.logger.warn(error.red.bold) perf_check.logger.warn("\t The server responded with an invalid http code") if profile.backtrace perf_check.logger.warn("Backtrace found:") backtrace = [profile.backtrace[0], *profile.backtrace.grep(/#{perf_check.app_root}/)] self.error_backtrace = '' backtrace.each do |line| perf_check.logger.warn(" #{line}") self.error_backtrace += "#{line}\n" end end break end end perf_check.logger.info '' unless .diff # pretty! end |
#speedup_factor ⇒ Object
89 90 91 |
# File 'lib/perf_check/test_case.rb', line 89 def speedup_factor reference_latency.to_f / this_latency.to_f end |
#switch_to_reference_context ⇒ Object
129 130 131 |
# File 'lib/perf_check/test_case.rb', line 129 def switch_to_reference_context @context = :reference end |
#this_latency ⇒ Object
67 68 69 |
# File 'lib/perf_check/test_case.rb', line 67 def this_latency this_profiles.map(&:latency).inject(0.0, :+) / this_profiles.size end |
#this_query_count ⇒ Object
76 77 78 |
# File 'lib/perf_check/test_case.rb', line 76 def this_query_count this_profiles.map(&:query_count).inject(0, :+) / this_profiles.size end |