Q:

Track duration of controller request in Rails?

I have a download action in my controller that is logged when the user clicks on the download link. Is there any way to track how long it took for the download to complete or at least that it was successful?
Here is the download action in the controller (Rails 3.2.8):
def download
    send_file @download.attachment.path, :filename => @download.attachment_file_name,
                                         :content_type => @download.attachment_content_type

    DownloadsLog.debug "log details here! -- at #{Time.now}"
end

downloads_log.rb model
class DownloadsLog
  def self.debug(message=nil)
    @@downloads_log ||= Logger.new("#{Rails.root}/log/downloads.log", 10, 1024000)
    @@downloads_log.debug(message) unless message.nil?
  end
end

Maybe it's not possible but I thought I would ask if anyone had any ideas...
Thanks!

A:

In Rails 3, wrap a section of code in a block, and supply it to the method benchmark.
benchmark "<My identifying label>" do
  # do this and that...
end

In your log, you'll see a line that indicates how long your block took to execute:
<My identifying label> (138.8ms)

You can even use this in a view:
<% benchmark "Process data files" do %>
  <%= expensive_files_operation %>
<% end %>

