Sha256: 966c00d6f11b01110417f1bf0ea9793c2f279b5df31d84f6b07719676696181f

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents



class Tryouts; class Drill; module Sergeant
  
  # = Benchmark
  # 
  # The sergeant responsible for running benchmarks
  #
  class Benchmark
    require 'benchmark'
    
    attr_reader :output
    
    # * +reps+ Number of times to execute drill (>= 0, <= 1000000). Default: 3
    #
    def initialize(reps=nil)
      @reps = (1..1000000).include?(reps) ? reps : 5
      @stats = Tryouts::Stats.new
    end
  
    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 &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
    
  end
end; end; end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
delano-tryouts-0.7.3 lib/tryouts/drill/sergeant/benchmark.rb
tryouts-0.7.3 lib/tryouts/drill/sergeant/benchmark.rb