Sha256: 84c94ff2f41c3391c7d2bd0b626e6afdfacc5798555d0be0780a12f018e7598d

Contents?: true

Size: 1.29 KB

Versions: 12

Compression:

Stored size: 1.29 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 {
              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
    
  end
end; end; end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
delano-tryouts-0.7.4 lib/tryouts/drill/sergeant/benchmark.rb
delano-tryouts-0.8.0 lib/tryouts/drill/sergeant/benchmark.rb
delano-tryouts-0.8.1 lib/tryouts/drill/sergeant/benchmark.rb
delano-tryouts-0.8.2 lib/tryouts/drill/sergeant/benchmark.rb
delano-tryouts-0.8.3 lib/tryouts/drill/sergeant/benchmark.rb
delano-tryouts-0.8.4 lib/tryouts/drill/sergeant/benchmark.rb
tryouts-0.7.4 lib/tryouts/drill/sergeant/benchmark.rb
tryouts-0.8.0 lib/tryouts/drill/sergeant/benchmark.rb
tryouts-0.8.1 lib/tryouts/drill/sergeant/benchmark.rb
tryouts-0.8.2 lib/tryouts/drill/sergeant/benchmark.rb
tryouts-0.8.3 lib/tryouts/drill/sergeant/benchmark.rb
tryouts-0.8.4 lib/tryouts/drill/sergeant/benchmark.rb