class BenchmarkHarness

Public Instance Methods

collections() click to toggle source
# File lib/benchmark_harness.rb, line 17
def collections
  @collections ||= Hash.new{|h,k| h[k] = Collection.new}
end
flush!() click to toggle source
# File lib/benchmark_harness.rb, line 21
def flush!
  @collections = nil
end
get_constant(name) click to toggle source

@param [String] name

# File lib/benchmark_harness.rb, line 67
def get_constant(name)
  pieces = name.split("::")
  parent_constant = Object
  pieces.each do |piece|
    if parent_constant.const_defined?(piece)
      parent_constant = parent_constant.const_get(piece)
    else
      raise NameError, "#{name} contains an undefined constant; it may be autoloaded"
    end
  end
  parent_constant
end
measure(collection_name) { || ... } click to toggle source
# File lib/benchmark_harness.rb, line 25
def measure(collection_name)
  result = nil
  m = Benchmark.measure{
    result = yield
  }
  collections[collection_name] << m.to_a[1..-1]
  result
end
wrap_method(signature, collection_name) click to toggle source

@param [String] signature @example “BenchmarkHarness#measure” instance method @example “BenchmarkHarness.measure” class method

# File lib/benchmark_harness.rb, line 37
  def wrap_method(signature, collection_name)
    if signature =~ /#/
      split_on = "#"
      class_method = false
    elsif signature =~ /\./
      split_on = "."
      class_method = true
    else
      raise ArgumentError, "your signature must contain a method"
    end
        
    constant_name, method_name = signature.split(split_on)
    method_name_safe = method_name.gsub(/[^\w\d]/, '') # strip out ie !
    constant = get_constant(constant_name)
    
    method_definition = <<-END
      alias :#{method_name_safe}_pre_benchmark_harness :#{method_name}
      def #{method_name}(*args, &block)
        BenchmarkHarness.measure(:#{collection_name}){result = #{method_name_safe}_pre_benchmark_harness(*args, &block)}
      end
    END
    
    if class_method
      constant = class << constant; self; end
    end
    
    constant.class_eval method_definition
  end