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

- old
+ new

@@ -1,59 +1,63 @@ require 'stringio' +require 'etc' module Rgot class M + class Options < Struct.new( + :bench, + :benchtime, + :timeout, + :cpu, + :thread, + ); end + # Ruby-2.0.0 wants default value of keyword_argument - def initialize(tests: nil, benchmarks: nil, examples: nil, opts: {}) + def initialize(tests: nil, benchmarks: nil, examples: nil, opts: Options.new) 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| + @cpu_list = (opts.cpu || "#{Etc.nprocessors}").split(',').map { |i| j = i.to_i - if j == 0 - raise OptionError, "expect integer string, got #{i.inspect}" - end + raise Rgot::OptionError, "invalid value #{i.inspect} for --cpu" unless 0 < j j } - @thread_list = @opts.fetch(:thread, "1").split(',').map { |i| + @thread_list = (opts.thread || "1").split(',').map { |i| j = i.to_i - if j == 0 - raise OptionError, "expect integer string, got #{i.inspect}" - end + raise Rgot::OptionError, "invalid value #{i.inspect} for --thread" unless 0 < j j } + @tests = tests + @benchmarks = benchmarks + @examples = examples + @opts = opts end def run test_ok = false example_ok = false - Timeout.timeout(@opts[:timeout].to_f) { + Timeout.timeout(@opts.timeout.to_f) { test_ok = run_tests example_ok = run_examples } if !test_ok || !example_ok puts "FAIL" return 1 end - puts "PASS" if @opts[:verbose] + puts "PASS" if Rgot.verbose? run_benchmarks 0 end private def run_tests ok = true @tests.each do |test| - t = T.new(test.module, test.name.to_sym, @opts) - if @opts[:verbose] + t = T.new(test.module, test.name.to_sym) + if Rgot.verbose? puts "=== RUN #{test.name}" end t.run t.report if t.failed? @@ -63,39 +67,44 @@ ok end def run_benchmarks ok = true - return ok unless @opts[:bench] + return ok unless @opts.bench @benchmarks.each do |bench| - next unless /#{@opts[:bench]}/ =~ bench.name + next unless /#{@opts.bench}/ =~ bench.name @cpu_list.each do |procs| @thread_list.each do |threads| - opts = @opts.dup - opts[:procs] = procs - opts[:threads] = threads + opts = B::Options.new + opts.benchtime = @opts.benchtime + 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 + next end + puts result + if 0 < b.output.length + printf("--- BENCH: %s\n%s", benchname, b.output) + end end end end ok end def run_examples ok = true @examples.each do |example| - if @opts[:verbose] + if Rgot.verbose? puts "=== RUN #{example.name}" end example.module.extend(example.module) method = example.module.instance_method(example.name).bind(example.module) out, err = capture do