Class: BloatCheck::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/bloat_check/stats.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.getObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bloat_check/stats.rb', line 23

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

Instance Method Details

#-(other) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/bloat_check/stats.rb', line 15

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

#log(prefix = "") ⇒ Object



11
12
13
# File 'lib/bloat_check/stats.rb', line 11

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

#to_sObject



7
8
9
# File 'lib/bloat_check/stats.rb', line 7

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