Class: Rumx::Beans::Timer

Inherits:
Object
  • Object
show all
Includes:
Rumx::Bean
Defined in:
lib/rumx/beans/timer.rb

Direct Known Subclasses

TimerAndError

Instance Method Summary collapse

Methods included from Rumx::Bean

add_root, #bean_add_child, #bean_children, #bean_each, #bean_each_child, #bean_each_child_recursive, #bean_each_embedded_child, #bean_each_operation, #bean_each_operation_recursive, #bean_embedded, #bean_find, #bean_get_and_set_attributes, #bean_get_attributes, #bean_has_attributes?, #bean_has_operations?, #bean_monitor, #bean_remove_child, #bean_set_and_get_attributes, #bean_set_attributes, #bean_synchronize, find, find_operation, included, remove_root, root

Constructor Details

#initialize(opts = {}) ⇒ Timer

Returns a new instance of Timer.



14
15
16
17
18
19
20
# File 'lib/rumx/beans/timer.rb', line 14

def initialize(opts={})
  # Force initialization of Bean#bean_monitor to avoid race condition (See bean.rb)
  bean_monitor
  @total_count = 0
  @last_time   = 0.0
  self.reset   = true
end

Instance Method Details

#avg_timeObject



53
54
55
56
57
58
# File 'lib/rumx/beans/timer.rb', line 53

def avg_time
  # Do the best we can w/o mutexing
  count, time = @count, @sum_time
  return 0.0 if count == 0
  time / count
end

#measureObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rumx/beans/timer.rb', line 31

def measure
  start_time = Time.now
  begin
    yield
  ensure
    current_time = (Time.now.to_f - start_time.to_f) * 1000.0
    bean_synchronize do
      @last_time    = current_time
      @count       += 1
      @total_count += 1
      @sum_time    += current_time
      @min_time     = current_time if !@min_time || current_time < @min_time
      @max_time     = current_time if current_time > @max_time
    end
  end
  return current_time
end

#min_timeObject



49
50
51
# File 'lib/rumx/beans/timer.rb', line 49

def min_time
  @min_time || 0.0
end

#reset=(val) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/rumx/beans/timer.rb', line 22

def reset=(val)
  if val
    @count    = 0
    @min_time = nil
    @max_time = 0.0
    @sum_time = 0.0
  end
end

#to_sObject



60
61
62
# File 'lib/rumx/beans/timer.rb', line 60

def to_s
  "total_count=#{@total_count} count=#{@count} min=#{('%.1f' % min_time)}ms max=#{('%.1f' % max_time)}ms avg=#{('%.1f' % avg_time)}ms"
end