Module: Servel::Instrumentation

Included in:
Entry, EntryFactory, HamlContext, Index
Defined in:
lib/servel/instrumentation.rb

Instance Method Summary collapse

Instance Method Details

#instrument(*method_names) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/servel/instrumentation.rb', line 2

def instrument(*method_names)
  return unless ENV["DEBUG"] == "1"

  Array(method_names).each do |method_name|
    original_method_name = "#{method_name}_without_instrumentation"

    alias_method original_method_name, method_name

    cumulative_time_spent = 0

    define_method(method_name) do |*args|
      start = Time.now

      return_value = __send__(original_method_name, *args)

      finish = Time.now
      time_spent = ((finish - start) * 1000.0)
      cumulative_time_spent += time_spent
      puts "Running #{self.class.name}<#{object_id}>##{method_name}: #{time_spent.round(3)}ms (cumulative: #{cumulative_time_spent.round(3)}ms)"

      return_value
    end
  end
end