Extend your class to wrap a benchmark around whatever method you want.
Add this line to your application's Gemfile:
gem 'benchmark_wrapper'
And then execute:
$ bundle
Or install it yourself as:
$ gem install benchmark_wrapper
require 'benchmark_wrapper'
class A
extend BenchmarkWrapper
def method1; 1; end
def method2(arg); arg + yield; end
wrap_with_benchmark :method1, :method2
end
a = A.new
a.method1
# 0.000000 0.000000 0.000000 ( 0.000010)
# => 1
a.method2(1) { 1 }
# 0.000000 0.000000 0.000000 ( 0.000008)
# => 2
It's important to define the methods you want to be benchmarked before you wrap them. The following will not work:
class A
extend BenchmarkWrapper
wrap_with_benchmark :method1
def method1; 1; end
end
# NameError: undefined method `method1' for class `A'
Output defaults to $stdout but can be redirected:
RESULTS = []
class B
extend BenchmarkWrapper
def method1; 1; end
wrap_with_benchmark :method1, out: RESULTS, out_method: :<<
end
B.new.method1
# => 1
RESULTS
# => [#<Benchmark::Tms ...>]
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request