Sha256: f2ccd545f45d8239066d876d1b47c62ea0d95c1b524107ef633dc669aac99fdb

Contents?: true

Size: 844 Bytes

Versions: 6

Compression:

Stored size: 844 Bytes

Contents

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

6 entries across 6 versions & 1 rubygems

Version Path
memory_profiler-0.9.12 lib/memory_profiler/top_n.rb
memory_profiler-0.9.11 lib/memory_profiler/top_n.rb
memory_profiler-0.9.10 lib/memory_profiler/top_n.rb
memory_profiler-0.9.9 lib/memory_profiler/top_n.rb
memory_profiler-0.9.8 lib/memory_profiler/top_n.rb
memory_profiler-0.9.7 lib/memory_profiler/top_n.rb