class Bnchmrkr::Mark

Bnchmrkr::Mark represents individual lambda runs within a Bnchmrkr run

Attributes

computed[R]
fastest[R]
lambda[R]
mean[R]
median[R]
mode[R]
mode_precision[R]
name[R]
slowest[R]
total[R]

Public Class Methods

new(name, lambda, mode_precision = 0) click to toggle source
# File lib/bnchmrkr/mark.rb, line 11
def initialize(name, lambda, mode_precision = 0)
  @name      = name
  @lambda    = lambda # TODO this name is going to cause problems
  @measures  = Array.new

  @mode_precision = mode_precision

  reset_computations # initialize to known values
end

Public Instance Methods

add_measure(measure) click to toggle source

measure Benchmark.measure{} result

# File lib/bnchmrkr/mark.rb, line 22
def add_measure(measure)
  @measures << measure
  reset_computations if @computed # as soon as a measure is added, previous computations are invalid
end
compute() click to toggle source
# File lib/bnchmrkr/mark.rb, line 32
def compute
  frequency_hash = Hash.new(0)
  total          = 0

  sorted = @measures.sort { |a,b| a.real <=> b.real }

  @measures.each do |measure|
    operand = @mode_precision.equal?(0) ? measure.real : measure.real.round(@mode_precision)
    frequency_hash[operand] += 1
  end

  max_frequency  = frequency_hash.values.max
  mode_candidate = frequency_hash.select{ |_operand, frequency| frequency.equal?(max_frequency) }.keys

  if max_frequency.equal?(1)
    mode = nil
  else
    mode = mode_candidate.size > 1 ? mode_candidate : mode_candidate.first
  end

  sorted.collect { |r| total += r.real }
  @fastest = sorted.first
  @slowest = sorted.last
  @mean    = sprintf('%5f', total / sorted.size).to_f
  @median  = sorted[(sorted.size / 2)]
  @mode    = mode
  @total   = sprintf('%5f', total).to_f

  @computed = true
end
each(&block) click to toggle source

allows Array-like behavior on the Array of measurements contained in a Bnchmrkr::Mark

# File lib/bnchmrkr/mark.rb, line 28
def each(&block)
  @measures.each(&block)
end
inspect() click to toggle source

a semi-fancy and somewhat fragile inspect method

try to compute if we haven't been, ensuring we always return a hash
# File lib/bnchmrkr/mark.rb, line 65
def inspect
  begin
    self.compute unless @computed
    {
      :name    => @name,
      :fastest => @fastest.real,
      :slowest => @slowest.real,
      :mean    => @mean,
      :median  => @median,
      :mode    => @mode,
      :total   => @total,
    }
  rescue => e
    { :name => @name, :computed => @computed }
  end
end

Private Instance Methods

reset_computations() click to toggle source

reset internal computed values, used when a new measure is added

# File lib/bnchmrkr/mark.rb, line 85
def reset_computations
  @computed = false
  @fastest  = :uncomputed
  @slowest  = :uncomputed
  @mean     = :uncomputed
  @median   = :uncomputed
  @mode     = :uncomputed
  @total    = :uncomputed
end