Class: PerfCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/perf_check.rb,
lib/perf_check/git.rb,
lib/perf_check/server.rb,
lib/perf_check/railtie.rb,
lib/perf_check/test_case.rb

Defined Under Namespace

Classes: Git, Railtie, Server, TestCase

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePerfCheck

Returns a new instance of PerfCheck.



48
49
50
51
52
# File 'lib/perf_check.rb', line 48

def initialize
  self.options = OpenStruct.new
  self.server = Server.new
  self.test_cases = []
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#serverObject

Returns the value of attribute server.



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

def server
  @server
end

#test_casesObject

Returns the value of attribute test_cases.



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

def test_cases
  @test_cases
end

Class Method Details

.callbackObject



44
45
46
# File 'lib/perf_check.rb', line 44

def self.callback
  @when_finished_callback
end

.diff_optionsObject



14
15
16
17
# File 'lib/perf_check.rb', line 14

def self.diff_options
  @@diff_options ||=
    ['-U3', '--ignore-matching-lines=/mini-profiler-resources/includes.js']
end

.normalize_resource(resource) ⇒ Object



53
54
55
# File 'lib/perf_check/git.rb', line 53

def self.normalize_resource(resource)
  resource.sub(/^([^\/])/, '/\1')
end

.require_rails(options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/perf_check.rb', line 19

def self.require_rails(options)
  ENV['PERF_CHECK'] = '1'
  if options.verify_responses
    ENV['PERF_CHECK_VERIFICATION'] = '1'
  end
  if !options.caching
    ENV['PERF_CHECK_NOCACHING'] = '1'
  end

  app_root = Dir.pwd
  until app_root == '/' || File.exist?("#{app_root}/config/application.rb")
    app_root = File.dirname(app_root)
  end

  unless File.exist?("#{app_root}/config/application.rb")
    abort("perf_check should be run from a rails directory")
  end

  require "#{app_root}/config/environment"
end

.when_finished(&block) ⇒ Object



40
41
42
# File 'lib/perf_check.rb', line 40

def self.when_finished(&block)
  @when_finished_callback = block
end

Instance Method Details

#add_test_case(route) ⇒ Object



54
55
56
57
# File 'lib/perf_check.rb', line 54

def add_test_case(route)
  route = PerfCheck.normalize_resource(route)
  test_cases.push(TestCase.new(route))
end


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/perf_check.rb', line 118

def print_brief_results
  test_cases.each do |test|
    print(test.resource.ljust(40) + ': ')

    codes = (test.this_profiles+test.reference_profiles).map(&:response_code).uniq
    print("(HTTP "+codes.join(',')+") ")

    printf('%.1fms', test.this_latency)

    puts && next if test.reference_profiles.empty?

    print(sprintf(' (%+5.1fms)', test.latency_difference).bold)
    print_diff_results(test.response_diff) if options.verify_responses
    puts
  end
end


110
111
112
113
114
115
116
# File 'lib/perf_check.rb', line 110

def print_diff_results(diff)
  if diff.changed?
    print(" Diff: #{diff.file}".bold.light_red)
  else
    print(" Diff: Output is identical!".bold.light_green)
  end
end


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/perf_check.rb', line 135

def print_results
  puts("==== Results ====")
  test_cases.each do |test|
    puts(test.resource.bold)

    if test.reference_profiles.empty?
      printf("your branch: ".rjust(15)+"%.1fms\n", test.this_latency)
      next
    end

    master_latency = sprintf('%.1fms', test.reference_latency)
    this_latency = sprintf('%.1fms', test.this_latency)
    difference = sprintf('%+.1fms', test.latency_difference)

    if test.latency_difference < 0
      change_factor = test.reference_latency / test.this_latency
    else
      change_factor = test.this_latency / test.reference_latency
    end
    formatted_change = sprintf('%.1fx', change_factor)

    percent_change = 100*(test.latency_difference / test.reference_latency).abs
    if percent_change < 10
      formatted_change = "yours is about the same"
      color = :blue
    elsif test.latency_difference < 0
      formatted_change = "yours is #{formatted_change} faster!"
      color = :green
    else
      formatted_change = "yours is #{formatted_change} slower!!!"
      color = :light_red
    end
    formatted_change = difference + " (#{formatted_change})"

    puts("master: ".rjust(15)     + "#{master_latency}")
    puts("your branch: ".rjust(15)+ "#{this_latency}")
    puts(("change: ".rjust(15)     + "#{formatted_change}").bold.send(color))

    print_diff_results(test.response_diff) if options.verify_responses
  end
end

#runObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/perf_check.rb', line 74

def run
  (options.reference ? 2 : 1).times do |i|
    if i == 1
      Git.stash_if_needed
      Git.checkout_reference(options.reference)
      test_cases.each{ |x| x.switch_to_reference_context }
    end

    server.restart
    test_cases.each_with_index do |test, i|
      server.restart unless i.zero? || options.diff

      if options.
        test.cookie = server.(options., test)
      end

      if options.diff
        puts "Issuing #{test.resource}"
      else
        puts("\nBenchmarking #{test.resource}:")
      end
      test.run(server, options)
    end
  end
end

#sanity_checkObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/perf_check.rb', line 59

def sanity_check
  if ENV['RAILS_ENV'] == 'production'
    abort("perf_check cannot be run in the production environment")
  end

  if Git.current_branch == "master"
    puts("Yo, profiling master vs. master isn't too useful, but hey, we'll do it")
  end

  puts "="*77
  print "PERRRRF CHERRRK! Grab a ☕️  and don't touch your working tree "
  puts "(we automate git)"
  puts "="*77
end

#trigger_callbackObject



100
101
102
103
104
105
106
107
108
# File 'lib/perf_check.rb', line 100

def trigger_callback
  results = OpenStruct.new(:current_branch => PerfCheck::Git.current_branch)
  results[:ARGV] = ORIGINAL_ARGV
  if test_cases.size == 1
    results.current_latency = test_cases.first.this_latency
    results.reference_latency = test_cases.first.reference_latency
  end
  PerfCheck.callback.call(results)
end