1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
module Test
module Unit
module Util
class MemoryUsage
attr_reader :virtual
attr_reader :physical
def initialize
@virtual = nil
@physical = nil
collect_data
end
def collected?
return false if @virtual.nil?
return false if @physical.nil?
true
end
private
def collect_data
collect_data_proc
end
def collect_data_proc
status_file = "/proc/self/status"
return false unless File.exist?(status_file)
data = File.binread(status_file)
data.each_line do |line|
case line
when /\AVm(Size|RSS):\s*(\d+)\s*kB/
name = $1
value = Integer($2, 10) * 1024
case name
when "Size"
@virtual = value
when "RSS"
@physical = value
end
end
end
collected?
end
end
end
end
end
|