Class: Tryouts::Drill::Sergeant::Benchmark

Inherits:
Object
  • Object
show all
Defined in:
lib/tryouts/drill/sergeant/benchmark.rb

Overview

Benchmark

The sergeant responsible for running benchmarks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reps = nil) ⇒ Benchmark

  • reps Number of times to execute drill (>= 0, <= 1000000). Default: 3



18
19
20
21
# File 'lib/tryouts/drill/sergeant/benchmark.rb', line 18

def initialize(reps=nil)
  @reps = (1..1000000).include?(reps) ? reps : 5
  @stats = Tryouts::Stats.new
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



14
15
16
# File 'lib/tryouts/drill/sergeant/benchmark.rb', line 14

def output
  @output
end

Instance Method Details

#run(block, context, &inline) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tryouts/drill/sergeant/benchmark.rb', line 23

def run(block, context, &inline)
  # A Proc object takes precedence over an inline block. 
  runtime = (block.nil? ? inline : block)
  response = Tryouts::Drill::Reality.new
  
  if runtime.nil?
    raise "We need a block to benchmark"
  else
    begin
      
      @reps.times do
        run = ::Benchmark.realtime {
          context.instance_eval &runtime
        }
        @stats.sample run
      end
      
      # We add the output after we run the block so that
      # that it'll remain nil if an exception was raised
      response.output = @stats
      
    rescue => e
      puts e.message, e.backtrace if Tryouts.verbose > 2
      response.etype = e.class
      response.error = e.message
      response.trace = e.backtrace
    end
  end
  response
end