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
  @instruments = []
  @needs_start = []
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
# File 'lib/trashed/meter.rb', line 22

def instrument(instrument)
  @instruments << instrument
  @needs_start << instrument if instrument.respond_to?(:start)
end

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



27
28
29
30
31
32
# File 'lib/trashed/meter.rb', line 27

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