在Rails 3中显示页面加载时间

9

我该如何在视图中显示页面加载时间,类似于日志文件中显示的“Completed 200 OK in 19ms (Views: 16.8ms | Models: 0.497ms)”


这里已经有一切了,你还想要什么? - shingara
5
他的意思是在浏览器中查看时显示时间,而不是从日志中获取时间。 - johnmcaliley
4个回答

16

你可能会更好地使用秒:

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”每秒都会归零。 - Artur Sapek

10
你可以这样做...在你的application_controller中添加一个before_filter:
class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :init

  def init
    @start_time = Time.now
  end
end
在视图中(我正在使用HAML):
load_time=#{Time.now-@start_time} seconds

这个时间并不完全与日志中看到的时间相同, 因为它仅限于从before_filter到在视图中调用它之间的时间,但应该很接近。


当然,这在视图中有点丑陋,只是为了简单起见才显示出来。您应该将其放入助手中。 - johnmcaliley

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

0

只需观看下面的Railscast视频,您就可以了解所有相关细节。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接