class PerfCheck::TestCase

Attributes

error_backtrace[RW]
http_status[RW]
max_memory[RW]
perf_check[R]
reference_profiles[RW]
reference_response[RW]
resource[RW]
this_profiles[RW]
this_response[RW]

Public Class Methods

new(perf_check, route) click to toggle source
# 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

Public Instance Methods

eql?(test) click to toggle source
# File lib/perf_check/test_case.rb, line 109
def eql?(test)
  resource == test.resource
end
hash() click to toggle source
# File lib/perf_check/test_case.rb, line 113
def hash
  resource.hash
end
issue_request(server, options) click to toggle source
# File lib/perf_check/test_case.rb, line 117
def issue_request(server, options)
  server.profile do |http|
    http.get(resource, request_headers)
  end
end
latency_difference() click to toggle source
# File lib/perf_check/test_case.rb, line 85
def latency_difference
  this_latency - reference_latency
end
reference_latency() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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.options.headers)
end
response_diff() click to toggle source
# 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.options.diff_options)
  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) click to toggle source
# File lib/perf_check/test_case.rb, line 20
def run(server, options)
  unless options.diff
    perf_check.logger.info("\t"+['request', 'latency', 'server rss', 'status', 'queries', 'profiler data'].map(&:underline).join("   "))
  end

  (options.number_of_requests + 2).times do |i|
    profile = issue_request(server, options)
    next if i < 2 # first 2 requests warm up the server/db and get tossed

    if options.verify_no_diff && i == 2
      response_for_comparison(profile.response_body)
    end

    unless options.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 options.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 options.diff # pretty!
end
speedup_factor() click to toggle source
# 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() click to toggle source
# File lib/perf_check/test_case.rb, line 129
def switch_to_reference_context
  @context = :reference
end
this_latency() click to toggle source
# 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() click to toggle source
# File lib/perf_check/test_case.rb, line 76
def this_query_count
  this_profiles.map(&:query_count).inject(0, :+) / this_profiles.size
end

Private Instance Methods

context_profiles() click to toggle source
# File lib/perf_check/test_case.rb, line 135
def context_profiles
  (@context == :reference) ? reference_profiles : this_profiles
end
response_for_comparison(response_body) click to toggle source
# File lib/perf_check/test_case.rb, line 139
def response_for_comparison(response_body)
  if @context == :reference
    self.reference_response = response_body
  else
    self.this_response = response_body
  end
end