Class: Klenod::Build::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/klenod/build/profiler.rb

Defined Under Namespace

Classes: Event

Instance Method Summary collapse

Constructor Details

#initialize(enabled: false, store_events: true, categories: [], progress: nil) ⇒ Profiler

Returns a new instance of Profiler.



8
9
10
11
12
13
14
15
16
# File 'lib/klenod/build/profiler.rb', line 8

def initialize(enabled: false, store_events: true, categories: [], progress: nil)
  @enabled = enabled
  @events = store_events ? [] : nil
  @totals = Hash.new(0.0)
  @totals_by_plugin = Hash.new(0.0)
  @counts = Hash.new(0)
  @categories = categories.map(&:to_sym).to_h { |category| [category, true] }
  @progress = progress
end

Instance Method Details

#category?(name) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/klenod/build/profiler.rb', line 40

def category?(name)
  @categories.key?(name.to_sym)
end

#count(name, value = 1) ⇒ Object



44
45
46
47
48
# File 'lib/klenod/build/profiler.rb', line 44

def count(name, value = 1)
  return unless enabled?

  @counts[name] += value
end

#countsObject



64
65
66
# File 'lib/klenod/build/profiler.rb', line 64

def counts
  @counts.dup
end

#enabled?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/klenod/build/profiler.rb', line 18

def enabled?
  @enabled
end

#eventsObject



22
23
24
# File 'lib/klenod/build/profiler.rb', line 22

def events
  @events || []
end

#measure(name, metadata = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/klenod/build/profiler.rb', line 26

def measure(name,  = nil)
  return yield unless enabled?

  started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  result = yield
  duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at
  record(name, duration,  || {})
  result
rescue
  duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at if started_at
  record(name, duration, ( || {}).merge(error: true)) if duration
  raise
end

#progress(name, metadata = {}) ⇒ Object



50
51
52
53
54
# File 'lib/klenod/build/profiler.rb', line 50

def progress(name,  = {})
  return unless enabled? && @progress

  @progress.call(name, )
end

#totalsObject



56
57
58
# File 'lib/klenod/build/profiler.rb', line 56

def totals
  @totals.dup
end

#totals_by_pluginObject



60
61
62
# File 'lib/klenod/build/profiler.rb', line 60

def totals_by_plugin
  @totals_by_plugin.dup
end