Class: Wildsight::Profiler::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/wildsight/profiler/profiler.rb

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Profiler

Returns a new instance of Profiler.



5
6
7
8
# File 'lib/wildsight/profiler/profiler.rb', line 5

def initialize(context)
  @context = context
  @context.data[:profiler] ||= {}
end

Instance Method Details

#duration(name, time = nil, options = {}, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wildsight/profiler/profiler.rb', line 10

def duration(name, time = nil, options = {}, &block)
  if block_given?
    start = DateTime.now.to_f
    result = block.call
    stop = DateTime.now.to_f
    item = { before: start, after: stop }
  else
    result = nil
    item = { before: time[0], after: time[1] }
  end
  item[:duration] = (item[:after] - item[:before]) * 1000
  durations[name] ||= { intervals: [], duration: 0.0 }
  durations[name][:intervals] << item
  durations[name][:duration] += item[:duration]
  return result
end

#durationsObject



27
28
29
# File 'lib/wildsight/profiler/profiler.rb', line 27

def durations
  @context.data[:profiler][:durations] ||= {}
end