Class: Effective::Profiler

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/profiler.rb

Class Method Summary collapse

Class Method Details

.allocations(sourcefiles: ['effective_'], &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/effective/profiler.rb', line 8

def self.allocations(sourcefiles: ['effective_'], &block)
  raise('please install the allocation_stats gem') unless defined?(AllocationStats)

  # Run block
  retval = nil
  stats = AllocationStats.trace { retval = yield(block) }

  # Compute Allocations
  allocations = stats.allocations.to_a

  # Filter
  if sourcefiles.present?
    sourcefiles = Array(sourcefiles)
    allocations = allocations.select! { |allocation| sourcefiles.any? { |str| allocation.sourcefile.include?(str) } }
  end

  # Sort
  allocations = allocations.sort_by { |allocation| allocation.memsize }

  # Print
  puts AllocationStats::AllocationsProxy.new(allocations).to_text

  puts "Total allocations: #{allocations.length}. Total size: #{allocations.sum(&:memsize)}"

  retval
end