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

ruby-changes:20221

From: naruse <ko1@a...>
Date: Tue, 28 Jun 2011 15:09:54 +0900 (JST)
Subject: [ruby-changes:20221] naruse:r32269 (trunk): * lib/benchmark.rb: merge eregon/benchmark.

naruse	2011-06-28 15:09:46 +0900 (Tue, 28 Jun 2011)

  New Revision: 32269

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=32269

  Log:
    * lib/benchmark.rb: merge eregon/benchmark.
      https://github.com/eregon/ruby/tree/benchmark
      patched by Benoit Daloze. [ruby-core:37593] [Bug #4940]
    
    * lib/benchmark (Benchmark#bmbm): bmbm should be consistent with bm
      for the return value.
    
    * test/benchmark: remove preemptive test instead of skipping
      I removed the preemptive test I wrote for Feature #4197.
      I'll add it back when the implementation will be able to satisfy it.
    
    * lib/benchmark (Benchmark#bmbm): remove useless explicit call,
      #format is an alias of #to_s test/benchmark: add a test for
      format of long time.
    
    * lib/benchmark: fix label width: always add 1 to ensure there is a
      space delimiter even with times over 100s
      When I asked for Feature #4197, I wanted to make delimiting spaces
      consistent for #bm and #bmbm.
      But with times over 100s, the output contains no space between the
      label and the first time (user).
      Now both ensure there is always a space, even if that means 3 spaces
      with times under 10s (because it is formatted as %10.6f)
    
    * test/benchmark: let labels be a constant
      lib/benchmark (Benchmark#realtime): avoid creating an unused Proc
      lib/benchmark (Benchmark#benchmark): use ensure clause to restore
      STDOUT.sync, as in #bmbm

  Modified files:
    trunk/ChangeLog
    trunk/lib/benchmark.rb
    trunk/test/benchmark/test_benchmark.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 32268)
+++ ChangeLog	(revision 32269)
@@ -1,3 +1,34 @@
+Tue Jun 28 15:09:21 2011  NARUSE, Yui  <naruse@r...>
+
+	* lib/benchmark.rb: merge eregon/benchmark.
+	  https://github.com/eregon/ruby/tree/benchmark
+	  patched by Benoit Daloze. [ruby-core:37593] [Bug #4940]
+
+	* lib/benchmark (Benchmark#bmbm): bmbm should be consistent with bm
+	  for the return value.
+
+	* test/benchmark: remove preemptive test instead of skipping
+	  I removed the preemptive test I wrote for Feature #4197.
+	  I'll add it back when the implementation will be able to satisfy it.
+
+	* lib/benchmark (Benchmark#bmbm): remove useless explicit call,
+	  #format is an alias of #to_s test/benchmark: add a test for
+	  format of long time.
+
+	* lib/benchmark: fix label width: always add 1 to ensure there is a
+	  space delimiter even with times over 100s
+	  When I asked for Feature #4197, I wanted to make delimiting spaces
+	  consistent for #bm and #bmbm.
+	  But with times over 100s, the output contains no space between the
+	  label and the first time (user).
+	  Now both ensure there is always a space, even if that means 3 spaces
+	  with times under 10s (because it is formatted as %10.6f)
+
+	* test/benchmark: let labels be a constant
+	  lib/benchmark (Benchmark#realtime): avoid creating an unused Proc
+	  lib/benchmark (Benchmark#benchmark): use ensure clause to restore
+	  STDOUT.sync, as in #bmbm
+
 Tue Jun 28 13:41:51 2011  NAKAMURA Usaku  <usa@r...>
 
 	* thread_win32.c (native_stop_timer_thread): fixed commit miss of
Index: lib/benchmark.rb
===================================================================
--- lib/benchmark.rb	(revision 32268)
+++ lib/benchmark.rb	(revision 32269)
@@ -167,6 +167,7 @@
     sync = STDOUT.sync
     STDOUT.sync = true
     label_width ||= 0
+    label_width += 1
     format ||= FORMAT
     print ' '*label_width + caption
     report = Report.new(label_width, format)
@@ -174,8 +175,9 @@
     Array === results and results.grep(Tms).each {|t|
       print((labels.shift || t.label || "").ljust(label_width), t.format(format))
     }
-    STDOUT.sync = sync
     report.list
+  ensure
+    STDOUT.sync = sync unless sync.nil?
   end
 
 
@@ -244,7 +246,7 @@
   def bmbm(width = 0, &blk) # :yield: job
     job = Job.new(width)
     yield(job)
-    width = job.width
+    width = job.width + 1
     sync = STDOUT.sync
     STDOUT.sync = true
 
@@ -263,7 +265,7 @@
     job.list.map { |label,item|
       GC.start
       print label.ljust(width)
-      Benchmark.measure(&item).tap { |res| print res.format }
+      Benchmark.measure(label, &item).tap { |res| print res }
     }
   ensure
     STDOUT.sync = sync unless sync.nil?
@@ -288,7 +290,7 @@
   #
   # Returns the elapsed real time used to execute the given block.
   #
-  def realtime(&blk) # :yield:
+  def realtime # :yield:
     r0 = Time.now
     yield
     Time.now - r0
Index: test/benchmark/test_benchmark.rb
===================================================================
--- test/benchmark/test_benchmark.rb	(revision 32268)
+++ test/benchmark/test_benchmark.rb	(revision 32269)
@@ -19,16 +19,14 @@
     x.report { 1.upto(n) do ; '1'; end }
   end
 
-  def labels
-    %w[first second third]
-  end
+  LABELS = %w[first second third]
 
   def bench(type = :bm, *args, &block)
     if block
       Benchmark.send(type, *args, &block)
     else
       Benchmark.send(type, *args) do |x|
-        labels.each { |label|
+        LABELS.each { |label|
           x.report(label) {}
         }
       end
@@ -50,6 +48,8 @@
       Benchmark::Tms.new(1,2,3,4,5,'label').format('%u %y %U %Y %t %r %n').must_equal \
         "1.000000 2.000000 3.000000 4.000000 10.000000 (5.000000) label"
       Benchmark::Tms.new(1).format('%u %.3f', 2).must_equal "1.000000 2.000"
+      Benchmark::Tms.new(100, 150, 0, 0, 200).to_s.must_equal \
+        "100.000000 150.000000 250.000000 (200.000000)\n"
     end
 
     it 'wont modify the format String given' do
@@ -70,22 +70,19 @@
 
   describe 'bm' do
     it "returns an Array of the times with the labels" do
-      capture_io do
-        results = bench
-        results.must_be_instance_of Array
-        results.size.must_equal labels.size
-        results.zip(labels).each { |tms, label|
-          tms.must_be_instance_of Benchmark::Tms
-          tms.label.must_equal label
-        }
+      [:bm, :bmbm].each do |meth|
+        capture_io do
+          results = bench(meth)
+          results.must_be_instance_of Array
+          results.size.must_equal LABELS.size
+          results.zip(LABELS).each { |tms, label|
+            tms.must_be_instance_of Benchmark::Tms
+            tms.label.must_equal label
+          }
+        end
       end
     end
 
-    it 'correctly guess the label width even when not given' do
-      skip :not_implemented
-      capture_bench_output(:bm).must_equal BM_OUTPUT
-    end
-
     it 'correctly output when the label width is given' do
       capture_bench_output(:bm, 6).must_equal BM_OUTPUT
     end
@@ -130,37 +127,37 @@
 end
 
 BM_OUTPUT = <<BENCH
-            user     system      total        real
-first   --time--   --time--   --time-- (  --time--)
-second  --time--   --time--   --time-- (  --time--)
-third   --time--   --time--   --time-- (  --time--)
+             user     system      total        real
+first    --time--   --time--   --time-- (  --time--)
+second   --time--   --time--   --time-- (  --time--)
+third    --time--   --time--   --time-- (  --time--)
 BENCH
 
 BM_OUTPUT_NO_LABEL = <<BENCH
-      user     system      total        real
-  --time--   --time--   --time-- (  --time--)
-  --time--   --time--   --time-- (  --time--)
-  --time--   --time--   --time-- (  --time--)
+       user     system      total        real
+   --time--   --time--   --time-- (  --time--)
+   --time--   --time--   --time-- (  --time--)
+   --time--   --time--   --time-- (  --time--)
 BENCH
 
 BMBM_OUTPUT = <<BENCH
-Rehearsal -----------------------------------------
-first   --time--   --time--   --time-- (  --time--)
-second  --time--   --time--   --time-- (  --time--)
-third   --time--   --time--   --time-- (  --time--)
--------------------------------- total: --time--sec
+Rehearsal ------------------------------------------
+first    --time--   --time--   --time-- (  --time--)
+second   --time--   --time--   --time-- (  --time--)
+third    --time--   --time--   --time-- (  --time--)
+--------------------------------- total: --time--sec
 
-            user     system      total        real
-first   --time--   --time--   --time-- (  --time--)
-second  --time--   --time--   --time-- (  --time--)
-third   --time--   --time--   --time-- (  --time--)
+             user     system      total        real
+first    --time--   --time--   --time-- (  --time--)
+second   --time--   --time--   --time-- (  --time--)
+third    --time--   --time--   --time-- (  --time--)
 BENCH
 
 BENCHMARK_OUTPUT_WITH_TOTAL_AVG = <<BENCH
-             user     system      total        real
-for:     --time--   --time--   --time-- (  --time--)
-times:   --time--   --time--   --time-- (  --time--)
-upto:    --time--   --time--   --time-- (  --time--)
->total:  --time--   --time--   --time-- (  --time--)
->avg:    --time--   --time--   --time-- (  --time--)
+              user     system      total        real
+for:      --time--   --time--   --time-- (  --time--)
+times:    --time--   --time--   --time-- (  --time--)
+upto:     --time--   --time--   --time-- (  --time--)
+>total:   --time--   --time--   --time-- (  --time--)
+>avg:     --time--   --time--   --time-- (  --time--)
 BENCH

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

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