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)"
Asked
Active
Viewed 6,178 times
9
-
All is here, what do you want more ? – shingara Nov 15 '10 at 19:54
-
5I think he means displaying the time in the view when you look at it in a browser.. not from the logs – johnmcaliley Nov 15 '10 at 20:03
4 Answers
16
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

Simeon Leyzerzon
- 18,658
- 9
- 54
- 82

Fred
- 356
- 1
- 4
- 4
-
1This will often result in negative values, since `usec` wraps around to zero every second. – Artur Sapek Apr 17 '14 at 02:47
10
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.

John Topley
- 113,588
- 46
- 195
- 237

johnmcaliley
- 11,015
- 2
- 42
- 47
-
and of course this is kinda ugly in the view, just showing this for simplicity. You would want to put that in a helper – johnmcaliley Nov 15 '10 at 20:01
0
Just go through the below railcast video you'll get to know all your concerned details.
http://railscasts.com/episodes/368-miniprofiler?view=asciicast

samarth
- 89
- 2
- 9
0
You could use rack-mini-profiler which adds a little badge to the top of the page showing all the details of the rendering speed.

Qwertie
- 5,784
- 12
- 45
- 89