Class: MemoryProfiler::Results

Inherits:
Object
  • Object
show all
Defined in:
lib/memory_profiler/results.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#total_allocatedObject

Returns the value of attribute total_allocated.



35
36
37
# File 'lib/memory_profiler/results.rb', line 35

def total_allocated
  @total_allocated
end

#total_retainedObject

Returns the value of attribute total_retained.



36
37
38
# File 'lib/memory_profiler/results.rb', line 36

def total_retained
  @total_retained
end

Class Method Details

.from_raw(allocated, retained, top) ⇒ Object



38
39
40
# File 'lib/memory_profiler/results.rb', line 38

def self.from_raw(allocated, retained, top)
  self.new.register_results(allocated,retained,top)
end

.register_type(name, lookup) ⇒ Object



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

def self.register_type(name, lookup)
  ["allocated","retained"].product(["objects","memory"]).each do |type, metric|
    full_name = "#{type}_#{metric}_by_#{name}"
    attr_accessor full_name

    @@lookups ||= []
    mapped = lookup

    if metric == "memory"
      mapped = lambda{|stat|
        [lookup.call(stat), stat.memsize]
      }
    end

    @@lookups << [full_name, mapped]

  end
end

Instance Method Details

#dump(description, data) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/memory_profiler/results.rb', line 75

def dump(description, data)
  puts description
  puts "-----------------------------------"
  if data
    data.each do |item|
      puts "#{item[:data]} x #{item[:count]}"
    end
  else
    puts "NO DATA"
  end
  puts
end

#pretty_printObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/memory_profiler/results.rb', line 63

def pretty_print
  puts "Total allocated #{total_allocated}"
  puts "Total retained #{total_retained}"
  puts
  ["allocated","retained"]
    .product(["memory", "objects"])
    .product(["gem", "file", "location"])
    .each do |(type, metric), name|
    dump "#{type} #{metric} by #{name}", self.send("#{type}_#{metric}_by_#{name}")
  end
end

#register_results(allocated, retained, top) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/memory_profiler/results.rb', line 42

def register_results(allocated, retained, top)
  @@lookups.each do |name, lookup|
    mapped = lambda{|tuple|
        lookup.call(tuple[1])
    }

    result =
      if name =~ /^allocated/
        allocated.top_n(top, &mapped)
      else
        retained.top_n(top, &mapped)
      end

    self.send "#{name}=", result
    self.total_allocated = allocated.count
    self.total_retained = retained.count
  end

  self
end