lib/rgot/m.rb in rgot-0.0.4 vs lib/rgot/m.rb in rgot-0.0.5

- old
+ new

@@ -1,15 +1,33 @@ require 'stringio' module Rgot class M # Ruby-2.0.0 wants default value of keyword_argument - def initialize(tests: [], benchmarks: [], examples: [], opts: {}) + def initialize(tests: nil, benchmarks: nil, examples: nil, opts: {}) + raise ArgumentError, "missing keyword: tests" unless tests + raise ArgumentError, "missing keyword: benchmarks" unless benchmarks + raise ArgumentError, "missing keyword: examples" unless examples + @tests = tests @benchmarks = benchmarks @examples = examples @opts = opts + @cpu_list = @opts.fetch(:cpu, "1").split(',').map { |i| + j = i.to_i + if j == 0 + raise OptionError, "expect integer string, got #{i.inspect}" + end + j + } + @thread_list = @opts.fetch(:thread, "1").split(',').map { |i| + j = i.to_i + if j == 0 + raise OptionError, "expect integer string, got #{i.inspect}" + end + j + } end def run test_ok = false example_ok = false @@ -49,15 +67,27 @@ ok = true return ok unless @opts[:bench] @benchmarks.each do |bench| next unless /#{@opts[:bench]}/ =~ bench.name - b = B.new(bench.module, bench.name.to_sym, @opts) - b.run - b.report - if b.failed? - ok = false + @cpu_list.each do |procs| + @thread_list.each do |threads| + opts = @opts.dup + opts[:procs] = procs + opts[:threads] = threads + b = B.new(bench.module, bench.name.to_sym, opts) + + benchname = bench.name.to_s + benchname << "-#{procs}" if 1 < procs + benchname << "(#{threads})" if 1 < threads + print "#{benchname}\t" + result = b.run + puts result + if b.failed? + ok = false + end + end end end ok end @@ -86,9 +116,11 @@ end ok end def capture + raise LocalJumpError, "no block given" unless block_given? + orig_out, orig_err = $stdout, $stderr out, err = StringIO.new, StringIO.new $stdout, $stderr = out, err yield [out.string, err.string]