Class: Rgot::B

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

Constant Summary collapse

Options =
_ = Struct.new(
  :procs,
  :threads,
  :benchtime,
)

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.



14
15
16
17
18
19
20
21
22
23
# File 'lib/rgot/b.rb', line 14

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

Instance Attribute Details

#nObject

Returns the value of attribute n.



12
13
14
# File 'lib/rgot/b.rb', line 12

def n
  @n
end

Instance Method Details

#reset_timerObject



39
40
41
42
43
44
# File 'lib/rgot/b.rb', line 39

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

#run(&block) ⇒ Object



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

def run(&block)
  n = 1
  benchtime = (@opts.benchtime || 1).to_f
  catch(:skip) do
    run_n(n, 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 == 1
          break
        end
        n = [(n * 1.2).to_i, n + 1].max || raise
      end
      run_n(n, block)
    end
  end

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

#run_parallelObject

Raises:

  • (LocalJumpError)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rgot/b.rb', line 73

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
  Process.waitall
end

#start_timerObject



25
26
27
28
29
30
# File 'lib/rgot/b.rb', line 25

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

#stop_timerObject



32
33
34
35
36
37
# File 'lib/rgot/b.rb', line 32

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