Module: MemoryProfiler::TopN

Included in:
StatHash
Defined in:
lib/memory_profiler/top_n.rb

Instance Method Summary collapse

Instance Method Details

#top_n(max, metric_method) ⇒ Object

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.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/memory_profiler/top_n.rb', line 5

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