Sha256: 8ad5b5283541a554a6870e9de8a3f1f4dd674f8728e3722d0c259b7efe1edb9e

Contents?: true

Size: 964 Bytes

Versions: 30

Compression:

Stored size: 964 Bytes

Contents

# frozen_string_literal: true

# Effective::Profiler.allocations { my_method() }

module Effective
  class Profiler

    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

  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
effective_developer-0.8.5 app/models/effective/profiler.rb
effective_developer-0.8.4 app/models/effective/profiler.rb
effective_developer-0.8.3 app/models/effective/profiler.rb
effective_developer-0.8.2 app/models/effective/profiler.rb
effective_developer-0.8.1 app/models/effective/profiler.rb
effective_developer-0.8.0 app/models/effective/profiler.rb
effective_developer-0.7.8 app/models/effective/profiler.rb
effective_developer-0.7.7 app/models/effective/profiler.rb
effective_developer-0.7.6 app/models/effective/profiler.rb
effective_developer-0.7.5 app/models/effective/profiler.rb
effective_developer-0.7.4 app/models/effective/profiler.rb
effective_developer-0.7.3 app/models/effective/profiler.rb
effective_developer-0.7.2 app/models/effective/profiler.rb
effective_developer-0.7.1 app/models/effective/profiler.rb
effective_developer-0.7.0 app/models/effective/profiler.rb
effective_developer-0.6.17 app/models/effective/profiler.rb
effective_developer-0.6.16 app/models/effective/profiler.rb
effective_developer-0.6.15 app/models/effective/profiler.rb
effective_developer-0.6.14 app/models/effective/profiler.rb
effective_developer-0.6.13 app/models/effective/profiler.rb