Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Page Load Time in Rails 3

How can I display the page load time in the view, similar to how the log file shows "Completed 200 OK in 19ms (Views: 16.8ms | Models: 0.497ms)"

like image 471
Mark Richman Avatar asked Nov 15 '10 19:11

Mark Richman


2 Answers

You might want to use Seconds better:

class ApplicationController < ActionController::Base

  before_filter :set_start_time

  def set_start_time
    @start_time = Time.now.to_f
  end

end

View Code:

Page Rendered in <%= sprintf('%.3f', (Time.now.to_f - @start_time) ) %> seconds
like image 97
Fred Avatar answered Oct 16 '22 15:10

Fred


You can do this.. add a before_filter to your application_controller:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :init

  def init
    @start_time = Time.now
  end
end

in the view (I am using HAML):

load_time=#{Time.now-@start_time} seconds

This is not going to be exactly the same as the time you see in the logs, since it's only from the before_filter to the place where it was called in the view, but it should be close.

like image 44
johnmcaliley Avatar answered Oct 16 '22 16:10

johnmcaliley