module DDMemoize
Constants
- NONE
- VERSION
Public Class Methods
activate(mod)
click to toggle source
# File lib/ddmemoize.rb, line 20 def self.activate(mod) mod.extend(Mixin) end
enable_metrics()
click to toggle source
# File lib/ddmemoize.rb, line 25 def enable_metrics @metrics_enabled = true end
metrics_counter()
click to toggle source
# File lib/ddmemoize.rb, line 33 def metrics_counter @_metrics_counter ||= DDMetrics::Counter.new end
metrics_enabled?()
click to toggle source
# File lib/ddmemoize.rb, line 29 def metrics_enabled? @metrics_enabled end
print_metrics()
click to toggle source
# File lib/ddmemoize.rb, line 38 def self.print_metrics headers = %w[memoization hit miss %] rows_raw = DDMemoize.metrics_counter.map do |label, count| { name: label.fetch(:method), type: label.fetch(:type), count: count, } end rows = rows_raw.group_by { |r| r[:name] }.map do |name, rows_for_name| rows_by_type = rows_for_name.group_by { |r| r[:type] } num_hit = rows_by_type.fetch(:hit, []).fetch(0, {}).fetch(:count, 0) num_miss = rows_by_type.fetch(:miss, []).fetch(0, {}).fetch(:count, 0) pct = num_hit.to_f / (num_hit + num_miss).to_f [name, num_hit.to_s, num_miss.to_s, "#{format('%3.1f', pct * 100)}%"] end all_rows = [headers] + rows puts DDMetrics::Table.new(all_rows).to_s end