9

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

4

4 回答 4

16

您可能希望更好地使用 Seconds:

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
于 2012-03-11T07:17:24.850 回答
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=#{Time.now-@start_time} seconds

这不会与您在日志中看到的时间完全相同,因为它只是从 before_filter 到视图中调用它的位置,但它应该是接近的。

于 2010-11-15T20:00:26.640 回答
0

只需观看下面的 railcast 视频,您就会了解所有您关心的细节。

http://railscasts.com/episodes/368-miniprofiler?view=asciicast
于 2015-04-30T18:09:18.997 回答
0

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

于 2017-10-18T02:19:47.300 回答