How to display the rendering time of a page?

Hi,

If an admin is logged in, I’m trying to display how long the page took
to render, somewhere at the bottom of the page.

I tried with an around_filter :time_an_action in application.rb

def time_an_action
started = Time.now
yield
@time_to_render = Time.now - started
end

In the layout I added this line:
<%= @time_to_render%>

However, as the time_an_action method has already yielded the page
before the instance variable @time_to_render has been given a value,
it does not display it.

Trying to execute an AJAX call inside the filter or redirecting to
another action, for example:
def time_an_action
started = Time.now
yield
@time_to_render = Time.now - started
redirect_to :controller => “admin”, :action => “display_time”
end

def display_time
render :update do |page|
page.replace_html “time_to_render”, @time_to_render
end
end

Gives the error of “You can’t render or redirect twice in the same
action”…

Any suggestions?

Thanks!

Rai

Hi Rai,

This is untested code, hope it works…

Class MyTimer
attr_reader :elapsed_time
def initiailize
@elapsed_time = nil
end
def before_filter
@start_time = Time.now
end
def after_filter
@end_time = Time.now
@elapsed_time = @end_time - @start_time
end
end

in our layout.rhtml at the bottom, i guess u can put this code.

Time take to render <%= @elapsed_time %>

And in application.rb (controller)
require ‘mytimer’
class Application < ActionController::Base
around_filter MyTimer.new
end

P.s:
The above code is untested, you can further optimize it. Sorry, i
don’t have ruby installed in this system.
May find syntax error.

Hope it works,

Regards,
Raghu Kumar K