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.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(instrumenter) ⇒ Profiler

Returns a new instance of Profiler.



8
9
10
# File 'lib/ruby_event_store/profiler.rb', line 8

def initialize(instrumenter)
  @instrumenter = instrumenter
end

Instance Method Details

#measure(&block) ⇒ Object



12
13
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
# File 'lib/ruby_event_store/profiler.rb', line 12

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") { block.call }

  subscribers.each { |s| @instrumenter.unsubscribe(s) }

  total = output.delete("total")

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

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

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

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