我该如何在视图中显示页面加载时间,类似于日志文件中显示的“Completed 200 OK in 19ms (Views: 16.8ms | Models: 0.497ms)”
我该如何在视图中显示页面加载时间,类似于日志文件中显示的“Completed 200 OK in 19ms (Views: 16.8ms | Models: 0.497ms)”
你可能会更好地使用秒:
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
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到在视图中调用它之间的时间,但应该很接近。
只需观看下面的Railscast视频,您就可以了解所有相关细节。
http://railscasts.com/episodes/368-miniprofiler?view=asciicast