Class: Timer::TimerGroup

Inherits:
TimerNode show all
Defined in:
lib/pg_graph/timer.rb

Direct Known Subclasses

Timer

Instance Attribute Summary collapse

Attributes inherited from TimerNode

#label, #timer

Instance Method Summary collapse

Constructor Details

#initialize(timer, label = nil, unit: :ms, scale: 2) ⇒ TimerGroup

Returns a new instance of TimerGroup.



45
46
47
48
49
50
51
# File 'lib/pg_graph/timer.rb', line 45

def initialize(timer, label = nil, unit: :ms, scale: 2)
  super(timer, label)
  @unit = unit.to_sym
  @scale = scale
  @nodes = []
  @nodes_by_label = {}
end

Instance Attribute Details

#nodesObject (readonly)

Array of tuples of label and time or TimerGroup objects



43
44
45
# File 'lib/pg_graph/timer.rb', line 43

def nodes
  @nodes
end

#scaleObject

Number of digits after the decimal point



42
43
44
# File 'lib/pg_graph/timer.rb', line 42

def scale
  @scale
end

#unitObject

Currently only :s, or :ms. Default is :ms



40
41
42
# File 'lib/pg_graph/timer.rb', line 40

def unit
  @unit
end

Instance Method Details

#dump(file = $stdout, indent = "") ⇒ Object



77
78
79
# File 'lib/pg_graph/timer.rb', line 77

def dump(file = $stdout, indent = "")
  dump_element(file, indent, label_width, value_width)
end

#factorObject



41
# File 'lib/pg_graph/timer.rb', line 41

def factor() { s: 1, ms: 1000 }[unit] end

#group(label, **opts) ⇒ Object



67
68
69
70
71
# File 'lib/pg_graph/timer.rb', line 67

def group(label, **opts)
  r = TimerGroup.new(self, label, **{ unit: unit, scale: scale }.merge(opts))
  @nodes << r
  r
end

#time(label, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pg_graph/timer.rb', line 53

def time(label, &block)
  t0 = Time.now
  r = yield
  dt = Time.now - t0
  if @nodes_by_label.key?(label)
    @nodes_by_label[label].time += dt
  else
    element = TimerElement.new(self, label, dt)
    @nodes << element
    @nodes_by_label[label] = element
  end
  r
end

#totalObject



73
74
75
# File 'lib/pg_graph/timer.rb', line 73

def total()
  @nodes.inject(0) { |a,e| a + e.total }
end