Sha256: efde17836eac9468093e1d01c50abb6d08ee566e0907775dfa9b9c571bb2b11d

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

require 'key_struct'

module BloatCheck
  class Stats < KeyStruct.reader(:memory, :counts, :time)

    def to_s
      "TIME=#{time.is_a?(Numeric) ? "#{time.round(1)} sec" : time} MEM=#{memory} OBJ: #{counts.sort_by(&:last).reverse[0...5].map(&it.join('>')).join(' ')}"
    end

    def log(prefix="")
      BloatCheck.logger.warn("BLOAT[#{$$}] (#{Time.now}) #{prefix} #{self.to_s}") unless BloatCheck.disabled?
    end

    def -(other)
      delta = {}
      counts.each do |klass, count|
        delta[klass] = count - other.counts[klass]
      end
      Stats.new(:memory => self.memory - other.memory, :counts => delta, :time => self.time - other.time)
    end

    def self.get 
      return new(:memory => 0, :counts => {}, :time => 0) if BloatCheck.disabled?

      memory = `ps -o rss= -p #{$$}`.to_i
      counts = Hash.new(0)
      ObjectSpace.each_object do |obj|
        klass = (class << obj ; superclass ; end) # can't use obj.class method since it may be overwritten
        counts[klass] += 1
      end
      self.new(:memory => memory, :counts => counts, :time => Time.now)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bloat_check-0.0.2 lib/bloat_check/stats.rb
bloat_check-0.0.1 lib/bloat_check/stats.rb
bloat_check-0.0.0 lib/bloat_check/stats.rb