Sha256: b1465f0c64940da078084fad5dc4d804ed4f29f73148d8e26a4441ac15f6c8b7

Contents?: true

Size: 965 Bytes

Versions: 2

Compression:

Stored size: 965 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)

      stats_by_metric = self.values.map! { |stat| [stat.send(metric), stat.memsize] }

      stat_totals = stats_by_metric.group_by { |metric_value, _memsize| metric_value }.
          map { |key, values| [key, values.reduce(0) { |sum, item| _key, memsize = item ; sum + memsize }, values.size] }

      stats_by_memsize = stat_totals.sort_by! { |metric, memsize, _count| [-memsize, metric] }.first(max).
          map! { |metric, memsize, _count| { data: metric, count: memsize } }
      stats_by_count = stat_totals.sort_by! { |metric, _memsize, count| [-count, metric] }.first(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 & 1 rubygems

Version Path
memory_profiler-0.9.6 lib/memory_profiler/top_n.rb
memory_profiler-0.9.5 lib/memory_profiler/top_n.rb