ruby-changes:33219
From: nobu <ko1@a...>
Date: Sun, 9 Mar 2014 13:17:42 +0900 (JST)
Subject: [ruby-changes:33219] nobu:r45298 (trunk): test/ruby/envutil.rb: compare RSS to check memory leak
nobu 2014-03-09 13:17:38 +0900 (Sun, 09 Mar 2014) New Revision: 45298 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45298 Log: test/ruby/envutil.rb: compare RSS to check memory leak * test/ruby/envutil.rb (Test::Test#assert_no_memory_leak): compare also RSS if available. * test/ruby/memory_status.rb (Memory::Status.parse): read string form of an instance. Modified files: trunk/test/ruby/envutil.rb trunk/test/ruby/memory_status.rb Index: test/ruby/memory_status.rb =================================================================== --- test/ruby/memory_status.rb (revision 45297) +++ test/ruby/memory_status.rb (revision 45298) @@ -1,5 +1,3 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/memory_status.rb#L1 -require_relative "envutil" - module Memory keys = [] vals = [] @@ -110,5 +108,20 @@ module Memory https://github.com/ruby/ruby/blob/trunk/test/ruby/memory_status.rb#L108 def initialize _update end + + def to_s + status = each_pair.map {|n,v| + "#{n}:#{v}" + } + "{#{status.join(",")}}" + end + + def self.parse(str) + status = allocate + str.scan(/(?:\A\{|\G,)(#{members.join('|')}):(\d+)(?=,|\}\z)/) do + status[$1] = $2.to_i + end + status + end end end Index: test/ruby/envutil.rb =================================================================== --- test/ruby/envutil.rb (revision 45297) +++ test/ruby/envutil.rb (revision 45298) @@ -391,6 +391,7 @@ eom https://github.com/ruby/ruby/blob/trunk/test/ruby/envutil.rb#L391 end def assert_no_memory_leak(args, prepare, code, message=nil, limit: 1.5, **opt) + require_relative 'memory_status' token = "\e[7;1m#{$$.to_s}:#{Time.now.strftime('%s.%L')}:#{rand(0x10000).to_s(16)}:\e[m" token_dump = token.dump token_re = Regexp.quote(token) @@ -403,16 +404,22 @@ eom https://github.com/ruby/ruby/blob/trunk/test/ruby/envutil.rb#L404 ] args.unshift(envs) if envs cmd = [ - 'END {STDERR.puts '"#{token_dump}"'"FINAL=#{Memory::Status.new.size}"}', + 'END {STDERR.puts '"#{token_dump}"'"FINAL=#{Memory::Status.new}"}', prepare, - 'STDERR.puts('"#{token_dump}"'"START=#{$initial_size = Memory::Status.new.size}")', + 'STDERR.puts('"#{token_dump}"'"START=#{$initial_size = Memory::Status.new}")', code, + 'GC.start', ].join("\n") _, err, status = EnvUtil.invoke_ruby(args, cmd, true, true, **opt) - before = err.sub!(/^#{token_re}START=(\d+)\n/, '') && $1.to_i - after = err.sub!(/^#{token_re}FINAL=(\d+)\n/, '') && $1.to_i + before = err.sub!(/^#{token_re}START=(\{.*\})\n/, '') && Memory::Status.parse($1) + after = err.sub!(/^#{token_re}FINAL=(\{.*\})\n/, '') && Memory::Status.parse($1) assert_equal([true, ""], [status.success?, err], message) - assert_operator(after.fdiv(before), :<, limit, message) + ([:size, :rss] & after.members).each do |n| + b = before[n] + a = after[n] + next unless a > 0 and b > 0 + assert_operator(a.fdiv(b), :<, limit, message(message) {"#{n}: #{b} => #{a}"}) + end end def assert_is_minus_zero(f) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/