Class: Oink::ActiveRecordInstantiationReporter

Inherits:
Base
  • Object
show all
Defined in:
lib/oink/active_record_instantiation_reporter.rb

Constant Summary

Constants inherited from Base

Base::FORMATS, Base::FORMAT_ALIASES, Base::HODEL_LOG_FORMAT_REGEX, Base::VERSION

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Oink::Base

Instance Method Details



10
11
12
13
14
15
16
17
18
19
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
# File 'lib/oink/active_record_instantiation_reporter.rb', line 10

def print(output)
  output.puts "---- OINK FOR ACTIVERECORD ----"
  output.puts "THRESHOLD: #{@threshold} Active Record objects per request\n"

  output.puts "\n-- REQUESTS --\n" if @format == :verbose

  @inputs.each do |input|
    input.each_line do |line|
      line = line.strip
      
       # Skip this line since we're only interested in the Hodel 3000 compliant lines
      next unless line =~ HODEL_LOG_FORMAT_REGEX

      if line =~ /rails\[(\d+)\]/
        pid = $1
        @pids[pid] ||= { :buffer => [], :ar_count => -1, :action => "", :request_finished => true }
        @pids[pid][:buffer] << line
      end

      if line =~ /Processing ((\w+)#(\w+)) /

        @pids[pid][:action] = $1
        unless @pids[pid][:request_finished]
          @pids[pid][:buffer] = [line]
        end
        @pids[pid][:request_finished] = false
  
      elsif line =~ /Instantiation Breakdown: Total: (\d+)/

        @pids[pid][:ar_count] = $1.to_i
  
      elsif line =~ /Completed in/

        if @pids[pid][:ar_count] > @threshold
          @bad_actions[@pids[pid][:action]] ||= 0
          @bad_actions[@pids[pid][:action]] = @bad_actions[@pids[pid][:action]] + 1
          date = HODEL_LOG_FORMAT_REGEX.match(line).captures[0]
          @bad_requests.push(OinkedARRequest.new(@pids[pid][:action], date, @pids[pid][:buffer], @pids[pid][:ar_count]))
          if @format == :verbose
            @pids[pid][:buffer].each { |b| output.puts b } 
            output.puts "---------------------------------------------------------------------"
          end
        end
      
        @pids[pid][:request_finished] = true
        @pids[pid][:buffer] = []
        @pids[pid][:ar_count] = -1
    
      end # end elsif
    end # end each_line
  end # end each input

  print_summary(output)

end