Benchmarkable is a module that allows a class to mention which instance methods to benchmark, and then allows retrieving the benchmark report in csv format from the class's instances. The csv includes the method invocation timestamp, some context description (if provided), the method name along with arguments passed for the invocation, and the number of seconds the method took to execute.
Benchmarkable is available as a gem. Simply type the following:gem install benchmarkable
Example:
class Gateway # ... def make_request(url) url = URI.parse(url) Net::HTTP.start(url.host, url.port) { |http| http.get('/index.html') } rescue => error puts "Unexpected error: #{error.message}" end # ... include Benchmarkable benchmark :make_request end gateway = Gateway.new gateway.make_request('http://www.google.com/') gateway.make_request('http://www.yahoo.com/') gateway.make_request('http://www.wikyblog.com/AmanKing/') File.open('./gateway_performance_report.csv', 'a+') do |file| file.puts gateway.benchmark_report.to_csv(:starting_context => 'gateway') end
Output (in gateway_performance_report.csv):
"2009-09-10T20:13:59+05:30","gateway","make_request http://www.google.com/","2.306514978408813" "2009-09-10T20:13:59+05:30","gateway","make_request http://www.yahoo.com/","2.08063411712646" "2009-09-10T20:14:00+05:30","gateway","make_request http://www.wikyblog.com/AmanKing/","5.1521680355072"
This can have interesting usages, one of them being monitoring AJAX performance of a web application by combining Benchmarkable with Selenium functional specs. I'll write in more details later but a sample is provided in the bitbucket repository: http://bitbucket.org/amanking/benchmarkable