Class: HeapProfiler::DiffResults

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

Constant Summary collapse

TYPES =
["allocated", "retained"].freeze

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(directory, types = TYPES, metrics = METRICS, groupings = GROUPINGS) ⇒ DiffResults

Returns a new instance of DiffResults.



158
159
160
161
162
163
# File 'lib/heap_profiler/results.rb', line 158

def initialize(directory, types = TYPES, metrics = METRICS, groupings = GROUPINGS)
  @directory = directory
  @types = types
  @metrics = metrics
  @groupings = groupings
end

Instance Method Details

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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/heap_profiler/results.rb', line 206

def dump_data(io, dimensions, type, metric, grouping, options)
  print_title io, "#{type} #{metric} by #{grouping}"
  data = dimensions[type][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, _type, _options) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/heap_profiler/results.rb', line 241

def dump_shape_edges(io, dimensions, _type, _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, type, options) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/heap_profiler/results.rb', line 222

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

  print_title(io, "#{type.capitalize} 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



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/heap_profiler/results.rb', line 165

def pretty_print(io = $stdout, **options)
  diff = Diff.new(@directory)
  heaps = @types.each_with_object({}) { |t, h| h[t] = diff.public_send("#{t}_diff") }
  index = Index.new(diff.allocated)

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

  dimensions = {}
  heaps.each do |type, heap|
    analyzer = Analyzer.new(heap, index)
    dimensions[type] = analyzer.run(@metrics, @groupings)
  end

  dimensions.each do |type, metrics|
    io.puts "Total #{type}: #{scale_bytes(metrics['total'].memory)} " \
            "(#{metrics['total'].objects} objects)"
  end

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

  if @metrics.include?("strings")
    @types.each do |type|
      dump_strings(io, dimensions[type], type, options)
    end
  end

  if @metrics.include?("shape_edges")
    @types.each do |type|
      dump_shape_edges(io, dimensions[type], type, options)
    end
  end
end