Module: Metrician::TimingMethodInterceptor

Defined in:
lib/metrician/reporters/method_timer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.already_timed_method?(klass, is_klass_method, timed_name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/metrician/reporters/method_timer.rb', line 29

def self.already_timed_method?(klass, is_klass_method, timed_name)
  is_klass_method ?
    klass.methods.include?(timed_name) :
    klass.method_defined?(timed_name)
end

.code_to_eval(is_klass_method, method_name, timed_name, untimed_name, metric_name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/metrician/reporters/method_timer.rb', line 35

def self.code_to_eval(is_klass_method, method_name, timed_name, untimed_name, metric_name)
  <<-EOD
    #{'class << self' if is_klass_method}
    def #{timed_name}(*args, &block)
      start_time = Time.now
      begin
        #{untimed_name}(*args, &block)
      ensure
        Metrician.gauge("#{metric_name}", (Time.now - start_time).to_f)
      end
    end
    alias :#{untimed_name} :#{method_name}
    alias :#{method_name} :#{timed_name}
    #{'end' if is_klass_method}
  EOD
end

.default_metric_name(klass, is_klass_method, method_name) ⇒ Object



17
18
19
20
21
# File 'lib/metrician/reporters/method_timer.rb', line 17

def self.default_metric_name(klass, is_klass_method, method_name)
  name = klass.name.underscore
  name = "#{name}.self" if is_klass_method
  "timer.#{name}.#{method_name}".downcase.tr_s("^a-zA-Z0-9.", "_")
end

.timeable_method?(klass, method_name) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/metrician/reporters/method_timer.rb', line 23

def self.timeable_method?(klass, method_name)
  klass.method_defined?(method_name) ||
    klass.private_method_defined?(method_name) ||
    klass.methods.include?(method_name)
end

Instance Method Details

#add_metrician_method_timer(method_name, metric_name = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/metrician/reporters/method_timer.rb', line 52

def add_metrician_method_timer(method_name, metric_name = nil)
  return false unless TimingMethodInterceptor.timeable_method?(self, method_name)

  is_klass_method = methods.include?(method_name)
  timed_name = "with_metrician_time_#{method_name}"
  return false if TimingMethodInterceptor.already_timed_method?(self, is_klass_method, timed_name)

  metric_name ||= TimingMethodInterceptor.default_metric_name(self, is_klass_method, method_name)
  untimed_name = "without_metrician_time_#{method_name}"

  timed_method_code =
    TimingMethodInterceptor.code_to_eval(is_klass_method, method_name, timed_name,
                                          untimed_name, metric_name)
  class_eval(timed_method_code, __FILE__, __LINE__)
end