Class: Timer::TimerGroup
Direct Known Subclasses
Instance Attribute Summary collapse
-
#nodes ⇒ Object
readonly
Array of tuples of label and time or TimerGroup objects.
-
#scale ⇒ Object
Number of digits after the decimal point.
-
#unit ⇒ Object
Currently only :s, or :ms.
Attributes inherited from TimerNode
Instance Method Summary collapse
- #dump(file = $stdout, indent = "") ⇒ Object
- #factor ⇒ Object
- #group(label, **opts) ⇒ Object
-
#initialize(timer, label = nil, unit: :ms, scale: 2) ⇒ TimerGroup
constructor
A new instance of TimerGroup.
- #time(label, &block) ⇒ Object
- #total ⇒ Object
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
#nodes ⇒ Object (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 |
#scale ⇒ Object
Number of digits after the decimal point
42 43 44 |
# File 'lib/pg_graph/timer.rb', line 42 def scale @scale end |
#unit ⇒ Object
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 |
#factor ⇒ Object
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 |
#total ⇒ Object
73 74 75 |
# File 'lib/pg_graph/timer.rb', line 73 def total() @nodes.inject(0) { |a,e| a + e.total } end |