Class: Trashed::Meter

Inherits:
Object
  • Object
show all
Defined in:
lib/trashed/meter.rb

Defined Under Namespace

Classes: ChangeInstrument, GaugeInstrument

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMeter

Returns a new instance of Meter.



5
6
7
8
# File 'lib/trashed/meter.rb', line 5

def initialize
  @timers = []
  @gauges = []
end

Instance Attribute Details

#instrumentsObject (readonly)

Returns the value of attribute instruments.



3
4
5
# File 'lib/trashed/meter.rb', line 3

def instruments
  @instruments
end

Instance Method Details

#counts(name, &block) ⇒ Object

Counters increase, so we measure before/after differences. Time elapsed, memory growth, objects allocated, etc.



12
13
14
# File 'lib/trashed/meter.rb', line 12

def counts(name, &block)
  instrument ChangeInstrument.new(name, block)
end

#gauges(name, &block) ⇒ Object

Gauges measure point-in-time values. Heap size, live objects, GC count, etc.



18
19
20
# File 'lib/trashed/meter.rb', line 18

def gauges(name, &block)
  instrument GaugeInstrument.new(name, block)
end

#instrument(instrument) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/trashed/meter.rb', line 22

def instrument(instrument)
  if instrument.respond_to?(:start)
    @timers << instrument
  else
    @gauges << instrument
  end
end

#instrument!(state, timings, gauges) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/trashed/meter.rb', line 30

def instrument!(state, timings, gauges)
  @timers.each { |i| i.start state, timings, gauges }
  yield.tap do
    @timers.reverse_each { |i| i.measure state, timings, gauges }
    @gauges.each { |i| i.measure state, timings, gauges }
  end
end