[前][次][番号順一覧][スレッド一覧]

ruby-changes:41984

From: ko1 <ko1@a...>
Date: Wed, 9 Mar 2016 16:22:31 +0900 (JST)
Subject: [ruby-changes:41984] ko1:r54060 (trunk): * benchmark/driver.rb: support memory usage benchmark.

ko1	2016-03-09 16:22:27 +0900 (Wed, 09 Mar 2016)

  New Revision: 54060

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54060

  Log:
    * benchmark/driver.rb: support memory usage benchmark.
      use `--measure-target=[target]'.
      Now, we can use the following targets:
        * real (default): real time which returns process time in sec.
        * peak: peak memory usage (physical memory) in bytes.
        * size: last memory usage (physical memory) in bytes.
    
    * benchmark/memory_wrapper.rb: ditto.

  Added files:
    trunk/benchmark/memory_wrapper.rb
  Modified files:
    trunk/ChangeLog
    trunk/benchmark/driver.rb
Index: benchmark/memory_wrapper.rb
===================================================================
--- benchmark/memory_wrapper.rb	(revision 0)
+++ benchmark/memory_wrapper.rb	(revision 54060)
@@ -0,0 +1,16 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/memory_wrapper.rb#L1
+
+write_file, target, script_file = ARGV
+
+load(script_file)
+require_relative '../test/lib/memory_status'
+open(write_file, 'wb'){|f|
+  ms = Memory::Status.new
+  case target.to_sym
+  when :peak
+    key = ms.member?(:hwm) ? :hwm : :peak
+  when :size
+    key = ms.member?(:rss) ? :rss : :size
+  end
+
+  f.puts ms[key]
+}
Index: benchmark/driver.rb
===================================================================
--- benchmark/driver.rb	(revision 54059)
+++ benchmark/driver.rb	(revision 54060)
@@ -18,6 +18,7 @@ end https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L18
 
 require 'benchmark'
 require 'pp'
+require 'tempfile'
 
 class BenchmarkDriver
   def self.benchmark(opt)
@@ -98,6 +99,7 @@ class BenchmarkDriver https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L99
     @output = opt[:output] ? open(opt[:output], 'w') : nil
     @loop_wl1 = @loop_wl2 = nil
     @ruby_arg = opt[:ruby_arg] || nil
+    @measure_target = opt[:measure_target]
     @opt = opt
 
     # [[name, [[r-1-1, r-1-2, ...], [r-2-1, r-2-2, ...]]], ...]
@@ -109,6 +111,7 @@ class BenchmarkDriver https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L111
       @execs.each_with_index{|(path, label, version), i|
         message "target #{i}: " + (label == version ? "#{label}" : "#{label} (#{version})") + " at \"#{path}\""
       }
+      message "measure target: #{@measure_target}"
     end
   end
 
@@ -196,7 +199,11 @@ class BenchmarkDriver https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L199
       output "minimum results in each #{@repeat} measurements."
     end
 
-    output "Execution time (sec)"
+    output({
+      real: "Execution time (sec)",
+      peak: "Memory usage (peak) (B)",
+      size: "Memory usage (last size) (B)",
+    }[@measure_target])
     output if markdown
     output ["name".ljust(name_width), @execs.map.with_index{|(_, v), i| sprintf(strformat, v, width[i])}].join("").rstrip
     output ["-"*name_width, width.map{|n|":".rjust(n, "-")}].join("|") if markdown
@@ -211,7 +218,11 @@ class BenchmarkDriver https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L218
 
     if @execs.size > 1
       output
-      output "Speedup ratio: compare with the result of `#{@execs[0][1]}' (greater is better)"
+      output({
+        rss:  "Memory consuming ratio (RSS) with the result of `#{@execs[0][1]}' (greater is worse)",
+        peak:  "Memory consuming ratio (peak) with the result of `#{@execs[0][1]}' (greater is worse)",
+        size:  "Memory consuming ratio (size) with the result of `#{@execs[0][1]}' (greater is worse)",
+      }[@measure_target])
       output if markdown
       output ["name".ljust(name_width), @execs[1..-1].map.with_index{|(_, v), i| sprintf(strformat, v, width[i])}].join("").rstrip
       output ["-"*name_width, width[1..-1].map{|n|":".rjust(n, "-")}].join("|") if markdown
@@ -223,7 +234,7 @@ class BenchmarkDriver https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L234
             if r == 0
               rets << "Error"
             else
-              rets << sprintf(numformat, first_value/r, width[rets.size+1])
+              rets << sprintf(numformat, first_value/Float(r), width[rets.size+1])
             end
           else
             first_value = r
@@ -312,18 +323,30 @@ class BenchmarkDriver https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L323
   end
 
   def measure executable, file
-    cmd = "#{executable} #{@ruby_arg} #{file}"
-
-    m = Benchmark.measure{
+    case @measure_target
+    when :real
+      cmd = "#{executable} #{@ruby_arg} #{file}"
+      m = Benchmark.measure{
+        system(cmd, out: File::NULL)
+      }
+      result = m.real
+    when :peak, :size
+      tmp = Tempfile.new("benchmark-memory-wrapper-data")
+      wrapper = "#{File.join(__dir__, 'memory_wrapper.rb')} #{tmp.path} #{@measure_target}"
+      cmd = "#{executable} #{@ruby_arg} #{wrapper} #{file}"
       system(cmd, out: File::NULL)
-    }
+      result = tmp.read.to_i
+      tmp.close
+    else
+      raise "unknown measure target"
+    end
 
     if $? != 0
       raise $?.inspect if $? && $?.signaled?
       output "\`#{cmd}\' exited with abnormal status (#{$?})"
       0
     else
-      m.real
+      result
     end
   end
 end
@@ -333,6 +356,7 @@ if __FILE__ == $0 https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L356
     :execs => [],
     :dir => File.dirname(__FILE__),
     :repeat => 1,
+    :measure_target => :real,
     :output => nil,
     :raw_output => nil,
     :format => :tsv,
@@ -368,6 +392,9 @@ if __FILE__ == $0 https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L392
     o.on('--ruby-arg [ARG]', "Optional argument for ruby"){|a|
       opt[:ruby_arg] = a
     }
+    o.on('--measure-target [TARGET]', 'real (execution time), peak, size (memory)'){|mt|
+      opt[:measure_target] = mt.to_sym
+    }
     o.on('--rawdata-output [FILE]', 'output rawdata'){|r|
       opt[:rawdata_output] = r
     }
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54059)
+++ ChangeLog	(revision 54060)
@@ -1,3 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Mar  9 16:20:25 2016  Koichi Sasada  <ko1@a...>
+
+	* benchmark/driver.rb: support memory usage benchmark.
+	  use `--measure-target=[target]'.
+	  Now, we can use the following targets:
+	    * real (default): real time which returns process time in sec.
+	    * peak: peak memory usage (physical memory) in bytes.
+	    * size: last memory usage (physical memory) in bytes.
+
+	* benchmark/memory_wrapper.rb: ditto.
+
 Wed Mar  9 15:04:22 2016  Koichi Sasada  <ko1@a...>
 
 	* benchmark/bm_vm3_gc_old_full.rb: add GC.start benchmark.

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]