Class: Rgot::B

Inherits:
Common show all
Defined in:
lib/rgot/b.rb

Defined Under Namespace

Classes: Options

Instance Attribute Summary collapse

Attributes inherited from Common

#output

Instance Method Summary collapse

Methods inherited from Common

#error, #errorf, #fail!, #fail_now, #failed?, #fatal, #fatalf, #finish!, #finished?, #log, #logf, #skip, #skip!, #skip_now, #skipf, #skipped?

Constructor Details

#initialize(benchmark_module, name, opts = Options.new) ⇒ B

Returns a new instance of B.



10
11
12
13
14
15
16
17
18
19
# File 'lib/rgot/b.rb', line 10

def initialize(benchmark_module, name, opts = Options.new)
  super()
  @n = 1
  @module = benchmark_module
  @name = name
  @opts = opts
  @timer_on = false
  @duration = 0
  @module.extend @module if @module
end

Instance Attribute Details

#nObject

Returns the value of attribute n.



9
10
11
# File 'lib/rgot/b.rb', line 9

def n
  @n
end

Instance Method Details

#reset_timerObject



35
36
37
38
39
40
# File 'lib/rgot/b.rb', line 35

def reset_timer
  if @timer_on
    @start = Rgot.now
  end
  @duration = 0
end

#run(&block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rgot/b.rb', line 42

def run(&block)
  n = 1
  benchtime = (@opts.benchtime || 1).to_f
  catch(:skip) do
    run_n(n.to_i, block)
    while !failed? && @duration < benchtime && @n < 1e9
      if @duration < (benchtime / 100.0)
        @n *= 100
      elsif @duration < (benchtime / 10.0)
        @n *= 10
      elsif @duration < (benchtime / 5.0)
        @n *= 5
      elsif @duration < (benchtime / 2.0)
        @n *= 2
      else
        if @n.to_i == 1
          break
        end
        @n *= 1.2
      end
      run_n(@n.to_i, block)
    end
  end

  BenchmarkResult.new(n: @n, t: @duration)
end

#run_parallelObject

Raises:

  • (LocalJumpError)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rgot/b.rb', line 69

def run_parallel
  raise LocalJumpError, "no block given" unless block_given?

  procs = (@opts.procs || 1)
  threads = (@opts.threads || 1)

  procs.times do
    fork do
      Array.new(threads) {
        Thread.new {
          yield PB.new(bn: @n)
        }.tap { |t| t.abort_on_exception = true }
      }.each(&:join)
    end
  end
  @n *= procs * threads
  Process.waitall
end

#start_timerObject



21
22
23
24
25
26
# File 'lib/rgot/b.rb', line 21

def start_timer
  if !@timer_on
    @start = Rgot.now
    @timer_on = true
  end
end

#stop_timerObject



28
29
30
31
32
33
# File 'lib/rgot/b.rb', line 28

def stop_timer
  if @timer_on
    @duration += Rgot.now - @start
    @timer_on = false
  end
end