Sha256: a81b1388cc07ca12d5eac59d6b8335d41aa45647fa0aa3059cf628ad046262bc

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 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) rescue Object) # don'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

2 entries across 2 versions & 1 rubygems

Version Path
bloat_check-0.0.4 lib/bloat_check/stats.rb
bloat_check-0.0.3 lib/bloat_check/stats.rb