Sha256: 631d48e99fb6b6ea44c7c05532d0edbf18798d517daf7a7a04778a7d6484404a

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module MemoryProfiler
  module TopN
    # Fast approach for determining the top_n entries in a Hash of Stat objects.
    # Returns results for both memory (memsize summed) and objects allocated (count) as a tuple.
    def top_n(max, metric_method)
      [
        top_n_memory(max, metric_method),
        top_n_objects(max, metric_method)
      ]
    end

    def top_n_memory(max, metric_method)
      metric_memsize = Hash.new(0)

      each_value do |value|
        metric = value.send(metric_method)
        metric_memsize[metric] += value.memsize
      end

      metric_memsize
        .to_a
        .sort_by! { |metric, memsize| [-memsize, metric] }
        .take(max)
        .map! { |metric, memsize| { data: metric, count: memsize } }
    end

    def top_n_objects(max, metric_method)
      metric_objects_count = Hash.new(0)

      each_value do |value|
        metric = value.send(metric_method)
        metric_objects_count[metric] += 1
      end

      metric_objects_count
        .to_a
        .sort_by! { |metric, count| [-count, metric] }
        .take(max)
        .map! { |metric, count| { data: metric, count: count } }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
memory_profiler-1.1.0 lib/memory_profiler/top_n.rb
memory_profiler-1.0.2 lib/memory_profiler/top_n.rb
memory_profiler-1.0.1 lib/memory_profiler/top_n.rb