Class: HeapProfiler::HeapResults

Inherits:
AbstractResults show all
Defined in:
lib/heap_profiler/results.rb

Constant Summary

Constants inherited from AbstractResults

AbstractResults::GROUPED_METRICS, AbstractResults::GROUPINGS, AbstractResults::METRICS, AbstractResults::UNIT_PREFIXES

Instance Attribute Summary

Attributes inherited from AbstractResults

#dimensions, #types

Instance Method Summary collapse

Methods inherited from AbstractResults

#normalize_path, #print_output, #print_output2, #print_title, #scale_bytes

Constructor Details

#initialize(heap_path, metrics = METRICS, groupings = GROUPINGS) ⇒ HeapResults

Returns a new instance of HeapResults.



71
72
73
74
75
# File 'lib/heap_profiler/results.rb', line 71

def initialize(heap_path, metrics = METRICS, groupings = GROUPINGS)
  @path = heap_path
  @metrics = metrics
  @groupings = groupings
end

Instance Method Details

#dump_data(io, dimensions, metric, grouping, options) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/heap_profiler/results.rb', line 108

def dump_data(io, dimensions, metric, grouping, options)
  print_title io, "#{metric} by #{grouping}"
  data = dimensions[grouping].top_n(metric, AbstractResults.top_entries_count)

  scale_data = metric == "memory" && options[:scale_bytes]
  normalize_paths = options[:normalize_paths]

  if data && !data.empty?
    data.each { |pair| pair[0] = normalize_path(pair[0]) } if normalize_paths
    data.each { |pair| pair[1] = scale_bytes(pair[1]) } if scale_data
    data.each { |k, v| print_output(io, v, k) }
  else
    io.puts "NO DATA"
  end
end

#dump_shape_edges(io, dimensions, _options) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/heap_profiler/results.rb', line 143

def dump_shape_edges(io, dimensions, _options)
  top = AbstractResults.top_entries_count

  data = dimensions["shape_edges"].top_n(top)
  unless data.empty?
    print_title(io, "Shape Edges Report")

    data.each do |edge_name, count|
      print_output io, count, edge_name
    end
  end
end

#dump_strings(io, dimensions, options) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/heap_profiler/results.rb', line 124

def dump_strings(io, dimensions, options)
  normalize_paths = options[:normalize_paths]
  scale_data = options[:scale_bytes]
  top = AbstractResults.top_entries_count

  print_title(io, "String Report")

  dimensions["strings"].top_n(top).each do |string|
    memsize = scale_data ? scale_bytes(string.memsize) : string.memsize
    print_output2 io, memsize, string.count, @colorize.string(string.value.inspect)
    string.top_n(top).each do |string_location|
      location = string_location.location
      location = normalize_path(location) if normalize_paths
      print_output2 io, '', string_location.count, location
    end
    io.puts
  end
end

#pretty_print(io = $stdout, **options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/heap_profiler/results.rb', line 77

def pretty_print(io = $stdout, **options)
  heap = Dump.new(@path)
  index = Index.new(heap)

  color_output = options.fetch(:color_output) { io.respond_to?(:isatty) && io.isatty }
  @colorize = color_output ? Polychrome : Monochrome

  analyzer = Analyzer.new(heap, index)
  dimensions = analyzer.run(@metrics, @groupings)

  if dimensions['total']
    io.puts "Total: #{scale_bytes(dimensions['total'].memory)} " \
            "(#{dimensions['total'].objects} objects)"
  end

  @metrics.each do |metric|
    next unless GROUPED_METRICS.include?(metric)
    @groupings.each do |grouping|
      dump_data(io, dimensions, metric, grouping, options)
    end
  end

  if @metrics.include?("strings")
    dump_strings(io, dimensions, options)
  end

  if @metrics.include?("shape_edges")
    dump_shape_edges(io, dimensions, options)
  end
end