Class: RubyEventStore::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_event_store/profiler.rb,
lib/ruby_event_store/profiler/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(instrumenter) ⇒ Profiler

Returns a new instance of Profiler.



10
11
12
# File 'lib/ruby_event_store/profiler.rb', line 10

def initialize(instrumenter)
  @instrumenter = instrumenter
end

Instance Method Details

#measure(&block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_event_store/profiler.rb', line 14

def measure(&block)
  output =
    Hash.new { 0 }
  subscribers =
    METRICS.map do |name|
      @instrumenter.subscribe(name) do |name, start, finish|
        metric_name = name.split('.').first
        duration    = 1000.0 * (finish - start)
        output[metric_name] += duration
      end
    end

  @instrumenter.instrument('total') do
    block.call
  end

  subscribers.each do |name|
    @instrumenter.unsubscribe(name)
  end

  total = output.delete('total')

  puts "%s %s %s" % ["metric".ljust(18), "ms".rjust(7), "%".rjust(6)]
  puts "\u2500" * 33

  output.each do |metric, duration|
    puts "%s %7.2f %6.2f" % [metric.ljust(18), duration, (duration/total * 100)]
  end

  puts
  puts "%s %7.2f %6.2f" % ["total".ljust(18), total, 100]

  output.merge("total" => total)
end