2010-11-15 211 views
8

我怎样才能在视图中显示的页面加载时间,类似于如何在日志文件中显示“在19ms完成200 OK(浏览次数:16.8ms |型号:0.497ms)”显示页面加载时间3

+0

所有在这里,你想要更多? – shingara 2010-11-15 19:54:40

+5

我认为他意味着在浏览器中查看时间,而不是从日志 – johnmcaliley 2010-11-15 20:03:47

回答

14

您可能要更好地利用秒:

class ApplicationController < ActionController::Base 

    before_filter :set_start_time 

    def set_start_time 
    @start_time = Time.now.to_f 
    end 

end 

查看代码:

Page Rendered in <%= sprintf('%.3f', (Time.now.to_f - @start_time)) %> seconds 
+1

这通常会导致负值,因为'usec'每秒都会绕到零。 – 2014-04-17 02:47:51

10

你可以做到这一点..添加的before_filter您application_controller:在视图

class ApplicationController < ActionController::Base 
    protect_from_forgery 
    before_filter :init 

    def init 
    @start_time = Time.now 
    end 
end 

(我使用HAML):

load_time=#{[email protected]_time} seconds 

这不会是完全一样的时间你从日志中看到,因为我t只能从before_filter到它在视图中被调用的地方,但它应该很接近。

+0

中查看,当然这在视图中是有点丑,只是为了简单起见。你会想把它放在帮手里 – johnmcaliley 2010-11-15 20:01:53

+0

足够接近我!谢谢!!! – 2010-11-15 20:32:42

0

刚经历,你会了解你的所有相关细节如下railcast视频。

http://railscasts.com/episodes/368-miniprofiler?view=asciicast 
0

你可以使用rack-mini-profiler它增加了一个小徽章,以显示渲染速度的所有细节页面的顶部。