Module: Chap::Benchmarking::ClassMethods

Defined in:
lib/chap/benchmarking.rb

Instance Method Summary collapse

Instance Method Details

#benchmark(*methods) ⇒ Object



11
12
13
14
15
# File 'lib/chap/benchmarking.rb', line 11

def benchmark(*methods)
  methods.each do |method|
    benchmark_each method
  end
end

#benchmark_each(method, scope = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chap/benchmarking.rb', line 16

def benchmark_each(method, scope=nil)
  class_eval "    def \#{method}_with_benchmark(*args,&block)\n      scope=\#{scope.inspect}\n      result = nil\n      realtime = Benchmark.realtime do\n        result = \#{method}_without_benchmark(*args,&block)\n      end\n      method_name = if args.empty?\n                      \"\#{method}\"\n                    else\n                      name = \"\#{method}\" + '(\"' + args.join(',') + '\")'\n                      name = shorten_name(name)\n                    end\n      method_name = scope + ': ' + method_name if scope\n      @@benchmarks << [method_name, realtime]\n      result\n    end\n  EOL\n  alias_method \"\#{method}_without_benchmark\", method\n  alias_method method, \"\#{method}_with_benchmark\"\nend\n"