我不久前掌握了这项技术,发现它非常方便。
当它就位后,您可以添加?profile=true
到任何点击控制器的 URL。您的操作将照常运行,但它不会将渲染的页面传递给浏览器,而是发送一个详细的、格式良好的 ruby-prof 页面,显示您的操作花费时间的地方。
首先,将 ruby-prof 添加到您的 Gemfile 中,可能在开发组中:
group :development do
gem "ruby-prof"
end
然后添加一个周围过滤器 http://guides.rubyonrails.org/action_controller_overview.html#after-filters-and-around-filters到您的应用程序控制器:
around_action :performance_profile if Rails.env == 'development'
def performance_profile
if params[:profile] && result = RubyProf.profile { yield }
out = StringIO.new
RubyProf::GraphHtmlPrinter.new(result).print out, :min_percent => 0
self.response_body = out.string
else
yield
end
end
阅读 ruby-prof 的输出是一门艺术,但我将把它作为练习。
ScottJShea 的补充说明:如果您想更改测量类型,请放置以下内容:
RubyProf.measure_mode = RubyProf::GC_TIME #example
之前if
在应用程序控制器的配置文件方法中。您可以在以下位置找到可用测量的列表:ruby-prof 页面 https://github.com/ruby-prof/ruby-prof#measurements。截至撰写本文时memory
and allocations
数据流似乎已损坏(见缺陷 https://github.com/ruby-prof/ruby-prof/issues/86).