Sha256: e2a7db95084378529ecdb17c5164b4e691e1a97c28cd1381214fca033443a96e

Contents?: true

Size: 973 Bytes

Versions: 2

Compression:

Stored size: 973 Bytes

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)

      stat_totals =
        self.values
          .group_by(&metric_method)
          .map do |metric, stats|
            [metric, stats.reduce(0) { |sum, stat| sum + stat.memsize }, stats.size]
          end

      stats_by_memsize =
        stat_totals
          .sort_by! { |metric, memsize, _count| [-memsize, metric] }
          .take(max)
          .map! { |metric, memsize, _count| { data: metric, count: memsize } }

      stats_by_count =
        stat_totals
          .sort_by! { |metric, _memsize, count| [-count, metric] }
          .take(max)
          .map! { |metric, _memsize, count| { data: metric, count: count } }

      [stats_by_memsize, stats_by_count]
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/memory_profiler-0.9.14/lib/memory_profiler/top_n.rb
memory_profiler-0.9.14 lib/memory_profiler/top_n.rb