Class: PerfCheck::TestCase

Inherits:
Object show all
Defined in:
lib/perf_check/test_case.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route) ⇒ TestCase

Returns a new instance of TestCase.



11
12
13
14
15
# File 'lib/perf_check/test_case.rb', line 11

def initialize(route)
  self.this_profiles = []
  self.reference_profiles = []
  self.resource = route
end

Instance Attribute Details

Returns the value of attribute cookie.



8
9
10
# File 'lib/perf_check/test_case.rb', line 8

def cookie
  @cookie
end

#reference_profilesObject

Returns the value of attribute reference_profiles.



9
10
11
# File 'lib/perf_check/test_case.rb', line 9

def reference_profiles
  @reference_profiles
end

#reference_responseObject

Returns the value of attribute reference_response.



8
9
10
# File 'lib/perf_check/test_case.rb', line 8

def reference_response
  @reference_response
end

#resourceObject

Returns the value of attribute resource.



7
8
9
# File 'lib/perf_check/test_case.rb', line 7

def resource
  @resource
end

#this_profilesObject

Returns the value of attribute this_profiles.



9
10
11
# File 'lib/perf_check/test_case.rb', line 9

def this_profiles
  @this_profiles
end

#this_responseObject

Returns the value of attribute this_response.



8
9
10
# File 'lib/perf_check/test_case.rb', line 8

def this_response
  @this_response
end

Instance Method Details

#eql?(test) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/perf_check/test_case.rb', line 118

def eql?(test)
  resource == test.resource
end

#hashObject



122
123
124
# File 'lib/perf_check/test_case.rb', line 122

def hash
  resource.hash
end

#latency_differenceObject



94
95
96
# File 'lib/perf_check/test_case.rb', line 94

def latency_difference
  this_latency - reference_latency
end

#latency_factorObject



98
99
100
# File 'lib/perf_check/test_case.rb', line 98

def latency_factor
  reference_latency / this_latency
end

#reference_latencyObject



80
81
82
83
# File 'lib/perf_check/test_case.rb', line 80

def reference_latency
  return nil if reference_profiles.empty?
  reference_profiles.map(&:latency).inject(0.0, :+) / reference_profiles.size
end

#reference_query_countObject



89
90
91
92
# File 'lib/perf_check/test_case.rb', line 89

def reference_query_count
  return nil if reference_profiles.empty?
  reference_profiles.map(&:query_count).inject(0, :+) / reference_profiles.size
end

#response_diffObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/perf_check/test_case.rb', line 102

def response_diff
  diff = Diffy::Diff.new(this_response, reference_response,
                         :diff => PerfCheck.diff_options)
  if diff.to_s.empty?
    OpenStruct.new(:changed? => false)
  else
    FileUtils.mkdir_p("#{Rails.root}/tmp/perf_check/diffs")
    file = `mktemp -u "#{Rails.root}/tmp/perf_check/diffs/XXXXXXXXXX"`.strip
    [:text, :html].each do |format|
      ext = {:text => 'diff', :html => 'html'}[format]
      File.open("#{file}.#{ext}", 'w'){ |f| f.write(diff.to_s(format)) }
    end
    OpenStruct.new(:changed? => true, :file => "#{file}.diff")
  end
end

#run(server, options) ⇒ Object



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
66
67
68
69
70
71
72
73
74
# File 'lib/perf_check/test_case.rb', line 21

def run(server, options)
  unless options.diff
    logger.info("\t"+['request', 'latency', 'server rss', 'status', 'queries', 'profiler data'].map(&:underline).join("   "))
  end

  profiles = (@context == :reference) ? reference_profiles : this_profiles

  headers = {'Cookie' => "#{cookie}".strip}
  headers['Accept'] = 'text/html,application/xhtml+xml,application/xml'

  (options.number_of_requests+1).times do |i|
    profile = server.profile do |http|
      http.get(resource, headers)
    end

    unless options.http_statuses.include? profile.response_code
      if options.fail_fast?
        File.open("tmp/perf_check/failed_request.html", 'w') do |error_dump|
          error_dump.write(profile.response_body)
        end
        error = sprintf("\t%2i:\tFAILED! (HTTP %d)", i, profile.response_code)
        logger.fatal(error.red.bold)
        logger.fatal("\t   The server responded with a non-2xx status for this request.")
        logger.fatal("\t   The response has been written to tmp/perf_check/failed_request.html")
        abort
      end
    end

    next if i.zero?

    if options.verify_responses
      if i == 1
        if @context == :reference
          self.reference_response = profile.response_body
        else
          self.this_response = profile.response_body
        end
      end
    end

    profile.server_memory = server.mem

    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)
      logger.info(row)
    end

    profiles << profile
  end

  logger.info '' unless options.diff # pretty!
end

#switch_to_reference_contextObject



17
18
19
# File 'lib/perf_check/test_case.rb', line 17

def switch_to_reference_context
  @context = :reference
end

#this_latencyObject



76
77
78
# File 'lib/perf_check/test_case.rb', line 76

def this_latency
  this_profiles.map(&:latency).inject(0.0, :+) / this_profiles.size
end

#this_query_countObject



85
86
87
# File 'lib/perf_check/test_case.rb', line 85

def this_query_count
  this_profiles.map(&:query_count).inject(0, :+) / this_profiles.size
end