Skip to content

Instantly share code, notes, and snippets.

@amiel
Created June 26, 2012 19:02
Show Gist options
  • Save amiel/2998059 to your computer and use it in GitHub Desktop.
Save amiel/2998059 to your computer and use it in GitHub Desktop.
Delegate all methods to the decorated class and benchmark each call.
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
@amiel
Copy link
Author

amiel commented Jun 26, 2012

Example usage:

startup = Startup.new
startup.bootstrap
startup.start_burning_out
startup.look_for_funding

Hmm, which of those methods is taking the longest...

startup = Benchmarker.new(Startup.new)
startup.bootstrap
startup.start_burning_out
startup.look_for_funding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment