Module: Rack::MiniProfiler::ProfilingMethods

Included in:
Rack::MiniProfiler
Defined in:
lib/mini_profiler/profiling_methods.rb

Instance Method Summary collapse

Instance Method Details

#profile_method(klass, method, &blk) ⇒ Object



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
65
66
67
68
69
70
# File 'lib/mini_profiler/profiling_methods.rb', line 40

def profile_method(klass, method, &blk)
  default_name = klass.to_s + " " + method.to_s
  with_profiling = (method.to_s + "_with_mini_profiler").intern
  without_profiling = (method.to_s + "_without_mini_profiler").intern
  
  if klass.send :method_defined?, with_profiling
    return # dont double profile
  end
    
  klass.send :alias_method, without_profiling, method
  klass.send :define_method, with_profiling do |*args, &orig|
    return self.send without_profiling, *args, &orig unless Rack::MiniProfiler.current

    name = default_name 
    name = blk.bind(self).call(*args) if blk
    
    parent_timer = Rack::MiniProfiler.current.current_timer
    page_struct = Rack::MiniProfiler.current.page_struct
    result = nil

    Rack::MiniProfiler.current.current_timer = current_timer = parent_timer.add_child(name) 
    begin 
      result = self.send without_profiling, *args, &orig
    ensure
      current_timer.record_time
      Rack::MiniProfiler.current.current_timer = parent_timer
    end 
    result 
  end
  klass.send :alias_method, method, with_profiling
end

#record_sql(query, elapsed_ms) ⇒ Object



5
6
7
8
9
# File 'lib/mini_profiler/profiling_methods.rb', line 5

def record_sql(query, elapsed_ms)
    c = current
    return unless c
 c.current_timer.add_sql(query, elapsed_ms, c.page_struct, c.skip_backtrace, c.full_backtrace) if (c && c.current_timer)
end

#step(name) ⇒ Object

perform a profiling step on given block



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mini_profiler/profiling_methods.rb', line 12

def step(name)
  if current
    parent_timer = current.current_timer
    result = nil
    current.current_timer = current_timer = current.current_timer.add_child(name) 
    begin 
      result = yield if block_given?
    ensure
      current_timer.record_time
      current.current_timer = parent_timer
    end
    result
  else
    yield if block_given?
  end
end

#unprofile_method(klass, method) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/mini_profiler/profiling_methods.rb', line 29

def unprofile_method(klass, method)
  with_profiling = (method.to_s + "_with_mini_profiler").intern
  without_profiling = (method.to_s + "_without_mini_profiler").intern
  
  if klass.send :method_defined?, with_profiling
    klass.send :alias_method, method, without_profiling
    klass.send :remove_method, with_profiling
    klass.send :remove_method, without_profiling
  end
end