Class: HeapProfiler::Analyzer::GroupedDimension

Inherits:
Dimension
  • Object
show all
Defined in:
lib/heap_profiler/analyzer.rb

Instance Attribute Summary

Attributes inherited from Dimension

#memory, #objects

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dimension

#stats

Constructor Details

#initializeGroupedDimension

Returns a new instance of GroupedDimension.



48
49
50
51
# File 'lib/heap_profiler/analyzer.rb', line 48

def initialize
  @objects = Hash.new { |h, k| h[k] = 0 }
  @memory = Hash.new { |h, k| h[k] = 0 }
end

Class Method Details

.build(grouping) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/heap_profiler/analyzer.rb', line 31

def build(grouping)
  klass = case grouping
  when "file"
    FileGroupDimension
  when "location"
    LocationGroupDimension
  when "gem"
    GemGroupDimension
  when "class"
    ClassGroupDimension
  else
    raise "Unknown grouping key: #{grouping.inspect}"
  end
  klass.new
end

Instance Method Details

#process(index, object) ⇒ Object



53
54
55
56
57
58
# File 'lib/heap_profiler/analyzer.rb', line 53

def process(index, object)
  if (group = @grouping.call(index, object))
    @objects[group] += 1
    @memory[group] += object[:memsize]
  end
end

#top_n(metric, max) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/heap_profiler/analyzer.rb', line 60

def top_n(metric, max)
  values = stats(metric).sort do |a, b|
    b[1] <=> a[1]
  end
  top = values.take(max)
  top.sort! do |a, b|
    cmp = b[1] <=> a[1]
    cmp == 0 ? b[0] <=> a[0] : cmp
  end
  top
end