Class: PerfCheck::TestCase
- Inherits:
-
Object
- Object
- PerfCheck::TestCase
- Defined in:
- lib/perf_check/test_case.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#controller ⇒ Object
Returns the value of attribute controller.
-
#cookie ⇒ Object
Returns the value of attribute cookie.
-
#format ⇒ Object
Returns the value of attribute format.
-
#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(route) ⇒ TestCase
constructor
A new instance of TestCase.
- #latency_difference ⇒ Object
- #latency_factor ⇒ Object
- #reference_latency ⇒ Object
- #response_diff ⇒ Object
- #run(server, options) ⇒ Object
- #switch_to_reference_context ⇒ Object
- #this_latency ⇒ Object
Constructor Details
#initialize(route) ⇒ TestCase
Returns a new instance of TestCase.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/perf_check/test_case.rb', line 11 def initialize(route) params = PerfCheck::Server.recognize_path(route) self.this_profiles = [] self.reference_profiles = [] self.controller = params[:controller].split('/')[-1] self.action = params[:action] self.format = params[:format] self.resource = route end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
7 8 9 |
# File 'lib/perf_check/test_case.rb', line 7 def action @action end |
#controller ⇒ Object
Returns the value of attribute controller.
7 8 9 |
# File 'lib/perf_check/test_case.rb', line 7 def controller @controller end |
#cookie ⇒ Object
Returns the value of attribute cookie.
8 9 10 |
# File 'lib/perf_check/test_case.rb', line 8 def @cookie end |
#format ⇒ Object
Returns the value of attribute format.
7 8 9 |
# File 'lib/perf_check/test_case.rb', line 7 def format @format end |
#reference_profiles ⇒ Object
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_response ⇒ Object
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 |
#resource ⇒ Object
Returns the value of attribute resource.
7 8 9 |
# File 'lib/perf_check/test_case.rb', line 7 def resource @resource end |
#this_profiles ⇒ Object
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_response ⇒ Object
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
116 117 118 |
# File 'lib/perf_check/test_case.rb', line 116 def eql?(test) resource == test.resource end |
#hash ⇒ Object
120 121 122 |
# File 'lib/perf_check/test_case.rb', line 120 def hash resource.hash end |
#latency_difference ⇒ Object
92 93 94 |
# File 'lib/perf_check/test_case.rb', line 92 def latency_difference this_latency - reference_latency end |
#latency_factor ⇒ Object
96 97 98 |
# File 'lib/perf_check/test_case.rb', line 96 def latency_factor reference_latency / this_latency end |
#reference_latency ⇒ Object
87 88 89 90 |
# File 'lib/perf_check/test_case.rb', line 87 def reference_latency return nil if reference_profiles.empty? reference_profiles.map(&:latency).inject(0.0, :+) / reference_profiles.size end |
#response_diff ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/perf_check/test_case.rb', line 100 def response_diff diff = Diffy::Diff.new(this_response, reference_response, :diff => PerfCheck.) 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
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 75 76 77 78 79 80 81 |
# File 'lib/perf_check/test_case.rb', line 27 def run(server, ) unless .diff print("\t"+'request #'.underline) print(" "+'latency'.underline) print(" "+'server rss'.underline) print(" "+'status'.underline) puts(" "+'profiler data'.underline) end profiles = (@context == :reference) ? reference_profiles : this_profiles headers = {'Cookie' => "#{}"} unless self.format headers['Accept'] = 'text/html,application/xhtml+xml,application/xml' end (.number_of_requests+1).times do |i| profile = server.profile do |http| http.get(resource, headers) end unless .http_statuses.include? profile.response_code if .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) puts(error.red.bold) puts("\t The server responded with a non-2xx status for this request.") puts("\t The response has been written to tmp/perf_check/failed_request.html") exit(1) end end next if i.zero? if .verify_responses if i == 1 if @context == :reference self.reference_response = profile.response_body else self.this_response = profile.response_body end end end unless .diff printf("\t%2i:\t %.1fms %4dMB\t %s\t %s\n", i, profile.latency, server.mem, profile.response_code, profile.profile_url) end profiles << profile end puts unless .diff # pretty! end |
#switch_to_reference_context ⇒ Object
23 24 25 |
# File 'lib/perf_check/test_case.rb', line 23 def switch_to_reference_context @context = :reference end |
#this_latency ⇒ Object
83 84 85 |
# File 'lib/perf_check/test_case.rb', line 83 def this_latency this_profiles.map(&:latency).inject(0.0, :+) / this_profiles.size end |