Sha256: 2ec40fb478c8e3c561d40e95aa96a751c3ce2db607f270652a8dcf3f3db6450a

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'key_struct'
require 'its-it'

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

1 entries across 1 versions & 1 rubygems

Version Path
bloat_check-0.0.5 lib/bloat_check/stats.rb