aboutsummaryrefslogtreecommitdiffstats
path: root/test/benchmark
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-01-31 16:11:06 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-01-31 16:11:06 +0000
commit979ec8df5daf6db314b2f17e53b53d269881d6ca (patch)
treeccfe52c1389f3fea5b872b20d7c997b93ea7bc76 /test/benchmark
parent6b06ba0c05cf966fa495fcaea6a934434e0460da (diff)
downloadruby-979ec8df5daf6db314b2f17e53b53d269881d6ca.tar.gz
* lib/benchmark.rb: fix benchmarck to work with current ruby.
patched by Benoit Daloze [ruby-core:33846] [ruby-dev:43143] merged from https://github.com/eregon/ruby/commits/benchmark * lib/benchmark (Report#width): update documentation * lib/benchmark: document the return value of #benchmark and the :list attribute in Report * lib/benchmark (Tms#format): rename variables, use String#% instead of Kernel.format * lib/benchmark: remove undocumented Benchmark::times (an alias of Process::times used twice) * lib/benchmark (#benchmark): use label_width for the caption * lib/benchmark (Tms#initialize): rename variables * lib/benchmark: allow title to not be a String and call #to_s * lib/benchmark (Benchmark#bm): return an Array of the times with the labels * lib/benchmark: correct output for Benchmark#bmbm (remove the extra space) * lib/benchmark: add a few tests for Benchmark::Tms output * lib/benchmark: improve style (enumerators, ljust, unused vars) * lib/benchmark: add spec about output and return value * lib/benchmark: improve basic style and consistency no parenthesis for print and use interpolation instead of printf * lib/benchmark: remove unnecessary conversions and variables * lib/benchmark: correct indentation * lib/benchmark: rename the FMTSTR constant and variable to FORMAT * lib/benchmark: remove useless exception * test/benchmark: remove unused variable warnings git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/benchmark')
-rw-r--r--test/benchmark/test_benchmark.rb168
1 files changed, 162 insertions, 6 deletions
diff --git a/test/benchmark/test_benchmark.rb b/test/benchmark/test_benchmark.rb
index 6d390558ac..e3305aa0ba 100644
--- a/test/benchmark/test_benchmark.rb
+++ b/test/benchmark/test_benchmark.rb
@@ -1,10 +1,166 @@
-require "test/unit"
-require "benchmark"
+require 'minitest/spec'
+require 'benchmark'
-class TestBenchmark < Test::Unit::TestCase
- def test_add!
- assert_nothing_raised("[ruby-dev:40906]") do
- Benchmark::Tms.new.add! {}
+MiniTest::Unit.autorun
+
+describe Benchmark do
+ BENCH_FOR_TIMES_UPTO = lambda do |x|
+ n = 1000
+ tf = x.report("for:") { for _ in 1..n; '1'; end }
+ tt = x.report("times:") { n.times do ; '1'; end }
+ tu = x.report("upto:") { 1.upto(n) do ; '1'; end }
+ [tf+tt+tu, (tf+tt+tu)/3]
+ end
+
+ BENCH_FOR_TIMES_UPTO_NO_LABEL = lambda do |x|
+ n = 1000
+ x.report { for _ in 1..n; '1'; end }
+ x.report { n.times do ; '1'; end }
+ x.report { 1.upto(n) do ; '1'; end }
+ end
+
+ def labels
+ %w[first second third]
+ end
+
+ def bench(type = :bm, *args, &block)
+ if block
+ Benchmark.send(type, *args, &block)
+ else
+ Benchmark.send(type, *args) do |x|
+ labels.each { |label|
+ x.report(label) {}
+ }
+ end
+ end
+ end
+
+ def capture_output
+ capture_io { yield }.first.gsub(/\d\.\d{6}/, '--time--')
+ end
+
+ def capture_bench_output(type, *args, &block)
+ capture_output { bench(type, *args, &block) }
+ end
+
+ describe 'Tms' do
+ it 'outputs nicely' do
+ Benchmark::Tms.new.to_s.must_equal " 0.000000 0.000000 0.000000 ( 0.000000)\n"
+ Benchmark::Tms.new(1,2,3,4,5).to_s.must_equal " 1.000000 2.000000 10.000000 ( 5.000000)\n"
+ 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"
+ end
+
+ it 'wont modify the format String given' do
+ format = "format %u"
+ Benchmark::Tms.new.format(format)
+ format.must_equal "format %u"
+ end
+ end
+
+ describe 'benchmark' do
+ it 'makes extra calcultations with an Array at the end of the benchmark and show the result' do
+ capture_bench_output(:benchmark,
+ Benchmark::CAPTION, 7,
+ Benchmark::FORMAT, ">total:", ">avg:",
+ &BENCH_FOR_TIMES_UPTO).must_equal BENCHMARK_OUTPUT_WITH_TOTAL_AVG
+ end
+ end
+
+ 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
+ }
+ 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
+
+ it 'correctly output when no label is given' do
+ capture_bench_output(:bm, &BENCH_FOR_TIMES_UPTO_NO_LABEL).must_equal BM_OUTPUT_NO_LABEL
+ end
+
+ it 'can make extra calcultations with an array at the end of the benchmark' do
+ capture_bench_output(:bm, 7, ">total:", ">avg:",
+ &BENCH_FOR_TIMES_UPTO).must_equal BENCHMARK_OUTPUT_WITH_TOTAL_AVG
+ end
+ end
+
+ describe 'bmbm' do
+ it 'correctly guess the label width even when not given' do
+ capture_bench_output(:bmbm).must_equal BMBM_OUTPUT
+ end
+
+ it 'correctly output when the label width is given (bmbm ignore it, but it is a frequent mistake)' do
+ capture_bench_output(:bmbm, 6).must_equal BMBM_OUTPUT
+ end
+ end
+
+ describe 'Report' do
+ describe '#item' do
+ it 'shows the title, even if not a string' do
+ capture_bench_output(:bm) { |x| x.report(:title) {} }.must_include 'title'
+ capture_bench_output(:bmbm) { |x| x.report(:title) {} }.must_include 'title'
+ end
+ end
+ end
+
+ describe 'Bugs' do
+ it '[ruby-dev:40906] can add in-place the time of execution of the block given' do
+ t = Benchmark::Tms.new
+ t.real.must_equal 0
+ t.add! {}
+ t.real.wont_equal 0
end
end
end
+
+BM_OUTPUT = <<BENCH
+ 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--)
+BENCH
+
+BMBM_OUTPUT = <<BENCH
+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--)
+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--)
+BENCH