lib/ddmemoize.rb in ddmemoize-1.0.0a3 vs lib/ddmemoize.rb in ddmemoize-1.0.0rc1

- old
+ new

@@ -1,9 +1,9 @@ # frozen_string_literal: true require 'ref' -require 'ddtelemetry' +require 'ddmetrics' require 'singleton' require_relative 'ddmemoize/version' module DDMemoize @@ -20,28 +20,32 @@ def self.activate(mod) mod.extend(Mixin) end class << self - def enable_telemetry - @telemetry_enabled = true + def enable_metrics + @metrics_enabled = true end - def telemetry_enabled? - @telemetry_enabled + def metrics_enabled? + @metrics_enabled end - def telemetry_counter - @_telemetry_counter ||= DDTelemetry::Counter.new + def metrics_counter + @_metrics_counter ||= DDMetrics::Counter.new end end - def self.print_telemetry + def self.print_metrics headers = %w[memoization hit miss %] - rows_raw = DDMemoize.telemetry_counter.map do |(name, type), count| - { name: name, type: type, count: count } + 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] } @@ -51,34 +55,34 @@ [name, num_hit.to_s, num_miss.to_s, "#{format('%3.1f', pct * 100)}%"] end all_rows = [headers] + rows - puts DDTelemetry::Table.new(all_rows).to_s + puts DDMetrics::Table.new(all_rows).to_s end module Mixin def memoize(method_name) original_method_name = '__nonmemoized_' + method_name.to_s alias_method original_method_name, method_name instance_cache = Hash.new { |hash, key| hash[key] = {} } - counter_label = "#{self}##{method_name}" + full_method_name = "#{self}##{method_name}" define_method(method_name) do |*args| instance_method_cache = instance_cache[self] value = NONE if instance_method_cache.key?(args) object = instance_method_cache[args].object value = object ? object.value : NONE end - if DDMemoize.telemetry_enabled? + if DDMemoize.metrics_enabled? if NONE.equal?(value) - DDMemoize.telemetry_counter.increment([counter_label, :miss]) + DDMemoize.metrics_counter.increment(method: full_method_name, type: :miss) else - DDMemoize.telemetry_counter.increment([counter_label, :hit]) + DDMemoize.metrics_counter.increment(method: full_method_name, type: :hit) end end if value.equal?(NONE) send(original_method_name, *args).tap do |r|