Class: Profiler::Collectors::CacheCollector

Inherits:
BaseCollector show all
Defined in:
lib/profiler/collectors/cache_collector.rb

Instance Attribute Summary

Attributes inherited from BaseCollector

#profile

Instance Method Summary collapse

Methods inherited from BaseCollector

descendants, #has_data?, inherited, #name, #panel_content, #render_html, #render_mode

Constructor Details

#initialize(profile) ⇒ CacheCollector

Returns a new instance of CacheCollector.



8
9
10
11
12
13
14
# File 'lib/profiler/collectors/cache_collector.rb', line 8

def initialize(profile)
  super
  @cache_reads = []
  @cache_writes = []
  @cache_deletes = []
  @subscriptions = []
end

Instance Method Details

#collectObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/profiler/collectors/cache_collector.rb', line 64

def collect
  @subscriptions.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) }

  hits = @cache_reads.count { |r| r[:hit] }
  misses = @cache_reads.count { |r| !r[:hit] }

  data = {
    reads: @cache_reads,
    writes: @cache_writes,
    deletes: @cache_deletes,
    total_reads: @cache_reads.size,
    total_writes: @cache_writes.size,
    total_deletes: @cache_deletes.size,
    hits: hits,
    misses: misses,
    hit_rate: @cache_reads.empty? ? 0 : (hits.to_f / @cache_reads.size * 100).round(2)
  }

  store_data(data)
end

#iconObject



16
17
18
# File 'lib/profiler/collectors/cache_collector.rb', line 16

def icon
  "💾"
end

#priorityObject



20
21
22
# File 'lib/profiler/collectors/cache_collector.rb', line 20

def priority
  50
end

#subscribeObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/profiler/collectors/cache_collector.rb', line 35

def subscribe
  return unless defined?(ActiveSupport::Notifications)

  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("cache_read.active_support") do |name, started, finished, unique_id, payload|
    duration = ((finished - started) * 1000).round(2)
    @cache_reads << {
      key: payload[:key],
      hit: payload[:hit],
      duration: duration
    }
  end

  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("cache_write.active_support") do |name, started, finished, unique_id, payload|
    duration = ((finished - started) * 1000).round(2)
    @cache_writes << {
      key: payload[:key],
      duration: duration
    }
  end

  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("cache_delete.active_support") do |name, started, finished, unique_id, payload|
    duration = ((finished - started) * 1000).round(2)
    @cache_deletes << {
      key: payload[:key],
      duration: duration
    }
  end
end

#tab_configObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/profiler/collectors/cache_collector.rb', line 24

def tab_config
  {
    key: "cache",
    label: "Cache",
    icon: icon,
    priority: priority,
    enabled: true,
    default_active: false
  }
end

#toolbar_summaryObject



85
86
87
88
89
90
91
92
93
# File 'lib/profiler/collectors/cache_collector.rb', line 85

def toolbar_summary
  hits = @cache_reads.count { |r| r[:hit] }
  total = @cache_reads.size

  {
    text: "#{hits}/#{total} hits",
    color: "cyan"
  }
end