Created
June 26, 2012 19:02
-
-
Save amiel/2998059 to your computer and use it in GitHub Desktop.
Delegate all methods to the decorated class and benchmark each call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
class Benchmarker < SimpleDelegator | |
def method_missing(method, *args, &block) | |
__log__ "Running #{ method }(#{ args.inspect.gsub(/^\[|\]$/, '') })" | |
result = nil | |
__benchmark__ { result = super } | |
result | |
end | |
def __benchmark__(&blk) | |
__log__ Benchmark::CAPTION | |
__log__ Benchmark.measure(&blk) | |
end | |
def __log__(*messages) | |
puts *messages | |
end | |
# I didn't want to benchmark every call to inspect that irb calls. | |
# But you could undef this if you want to benchmark inspect. | |
def inspect | |
"#<Benchmarker for:#{ __getobj__.inspect }>" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage:
Hmm, which of those methods is taking the longest...