Class: BenchmarkHarness

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/benchmark_harness.rb,
lib/benchmark_harness/stats.rb,
lib/benchmark_harness/collection.rb

Defined Under Namespace

Modules: Stats Classes: Collection

Instance Method Summary collapse

Instance Method Details

#collectionsObject



17
18
19
# File 'lib/benchmark_harness.rb', line 17

def collections
  @collections ||= Hash.new{|h,k| h[k] = Collection.new}
end

#flush!Object



21
22
23
# File 'lib/benchmark_harness.rb', line 21

def flush!
  @collections = nil
end

#get_constant(name) ⇒ Object

Parameters:

  • name (String)


67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



25
26
27
28
29
30
31
32
# 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) ⇒ Object

Examples:

“BenchmarkHarness#measure” instance method

“BenchmarkHarness.measure” class method

Parameters:

  • signature (String)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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