Module: Fiveruns::Tuneup::Instrumentation::Utilities

Included in:
Fiveruns::Tuneup
Defined in:
lib/fiveruns/tuneup/instrumentation/utilities.rb

Instance Method Summary collapse

Instance Method Details

#add_custom_methods(target, *methods) ⇒ Object



18
19
20
21
# File 'lib/fiveruns/tuneup/instrumentation/utilities.rb', line 18

def add_custom_methods(target, *methods)
  custom_methods[target] = [] unless custom_methods.key?(target)
  custom_methods[target].push(*methods)
end

#custom_methodsObject



14
15
16
# File 'lib/fiveruns/tuneup/instrumentation/utilities.rb', line 14

def custom_methods
  @custom_methods ||= {}
end

#excludeObject



29
30
31
32
33
# File 'lib/fiveruns/tuneup/instrumentation/utilities.rb', line 29

def exclude
  result = nil
  exclusion_stack[-1] += stopwatch { result = yield }
  result
end

#exclusion_stackObject



10
11
12
# File 'lib/fiveruns/tuneup/instrumentation/utilities.rb', line 10

def exclusion_stack
  @exclusion_stack ||= [0]
end

#handle_exclusions_in(step) ⇒ Object

Handle removal of excluded time from total for this step, and bubble the value up for removal from the parent step



61
62
63
64
65
66
67
# File 'lib/fiveruns/tuneup/instrumentation/utilities.rb', line 61

def handle_exclusions_in(step)
  exclusion_stack << 0
  yield # Must set +step.time+
  time_to_exclude = exclusion_stack.pop
  step.time -= time_to_exclude
  exclusion_stack[-1] += time_to_exclude unless exclusion_stack.blank?
end

#instrument(target, *mods) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fiveruns/tuneup/instrumentation/utilities.rb', line 69

def instrument(target, *mods)
  mods.each do |mod|
    # Change target for 'ClassMethods' module
    real_target = mod.name.demodulize == 'ClassMethods' ? (class << target; self; end) : target
    real_target.__send__(:include, mod)
    # Find all the instrumentation hooks and chain them in
    mod.instance_methods.each do |meth|
      name = meth.to_s.sub('_with_fiveruns_tuneup', '')
      real_target.alias_method_chain(name, :fiveruns_tuneup) rescue nil
    end
  end
end

#instrument_action_methods(controller) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/fiveruns/tuneup/instrumentation/utilities.rb', line 82

def instrument_action_methods(controller)
  klass = controller.class
  actions_for(klass).each do |meth|
    format = alias_format_for(meth)
    next if controller.respond_to?(format % :with, true)
    wrap(klass, format, meth, "Invoke #{meth} action", :controller)
  end
end

#instrument_custom_methodsObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fiveruns/tuneup/instrumentation/utilities.rb', line 100

def instrument_custom_methods
  custom_methods.each do |meth_target, meths|
    lineage = meth_target.ancestors
    layer = if lineage.include?(ActionController::Base)
      :controller
    elsif lineage.include?(ActiveRecord::Base)
      :model
    elsif lineage.include?(ActionView::Base)
      :view
    else
      :other
    end
    meths.each do |meth|
      format = alias_format_for(meth)
      wrap(meth_target, format, meth, "Method #{meth}", layer)
    end
  end
end

#instrument_filters(controller) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/fiveruns/tuneup/instrumentation/utilities.rb', line 91

def instrument_filters(controller)
  klass = controller.class
  filters_for(klass).each do |filter|
    format = alias_format_for(name_of_filter(filter))
    next if controller.respond_to?(format % :with, true)
    wrap(klass, format, name_of_filter(filter), "#{filter.type.to_s.titleize} filter #{name_of_filter(filter)}", :controller)
  end
end

#stackObject



6
7
8
# File 'lib/fiveruns/tuneup/instrumentation/utilities.rb', line 6

def stack
  @stack ||= []
end

#step(name, layer = nil, link = true, sql = nil, table_name = nil, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fiveruns/tuneup/instrumentation/utilities.rb', line 35

def step(name, layer=nil, link=true, sql=nil, table_name=nil, &block)
  if recording?
    result = nil
    caller_line = caller.detect { |l| l.include?(RAILS_ROOT) && l !~ /tuneup|vendor\/rails/ } if link
    file, line = caller_line ? caller_line.split(':')[0, 2] : [nil, nil]
    line = line.to_i if line
    returning ::Fiveruns::Tuneup::Step.new(name, layer, file, line, sql, &block) do |step|
      step.table_name = table_name
      stack.last << step
      stack << step
      begin
        handle_exclusions_in step do
          step.time = stopwatch { result = yield(sql) }
        end
      ensure
        stack.pop
      end
    end
    result
  else
    yield(sql)
  end
end

#stopwatchObject



23
24
25
26
27
# File 'lib/fiveruns/tuneup/instrumentation/utilities.rb', line 23

def stopwatch
  start = Time.now.to_f
  yield
  (Time.now.to_f - start) * 1000
end