如何确定在我的 Rails 应用程序中执行操作之前出现延迟的原因?
How to determine the cause of a delay before an action is hit in my Rails app?
我的故障排除已进入死胡同,我真的希望有人能提供帮助。
我正在使用 rack-mini-profiler 来帮助定位我的 Rails 网站中潜在的内存泄漏。我将其缩小到正在调用的方法。
高层简介:rack-mini-profiler high-level
SQL个人资料:rack-mini-profiler results
在 SQL 个人资料图片中,请注意灰色条上方和下方的查询开始时间相差 1037 毫秒。这就是我注意到的延迟,并且在我重新启动应用程序之前它会一直增长。
在本地运行这个时候,我可以监听终端。当请求该方法时,只有 1 秒的延迟,然后它就会执行。在此延迟期间,终端中不会显示任何查询或命令。
有没有人知道我如何找出导致延迟的原因?
我正在使用 Ruby 2.2.0 和 Rails 4.1.6.
谢谢!!
编辑:
这是 rack-mini-profiler 指向的方法:
def submit_answer
quiz_attempt = CourseCompletion.find_by_id(params[:course_completion_id]).quiz_started
choice = Choice.new(:answer_id => params[:answer], :quiz_attempt_id => quiz_attempt.id)
@success = choice.save
@answer = choice.answer
@question = @answer.question
@quiz_attempt = choice.quiz_attempt
render :layout => false
end
看看 ruby-prof github.com/ruby-prof/ruby-prof。尝试将您的动作主体包装到块中:
require 'ruby-prof'
# profile the code
RubyProf.start
# ... code to profile ...
result = RubyProf.stop
# print a flat profile to text
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)
您应该会在控制台中收到如下所示的输出:
您还可以将探查器的输出重定向到日志或服务器上的某个文件。
所以在你的情况下编辑代码:
def submit_answer
require 'ruby-prof'
# profile the code
RubyProf.start
quiz_attempt = CourseCompletion.find_by_id(params[:course_completion_id]).quiz_started
choice = Choice.new(:answer_id => params[:answer], :quiz_attempt_id => quiz_attempt.id)
@success = choice.save
@answer = choice.answer
@question = @answer.question
@quiz_attempt = choice.quiz_attempt
render :layout => false
result = RubyProf.stop
# print a flat profile to text
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)
end
添加到 gemfile
gem 'ruby-prof'
运行 bundle install
并重启服务器。再次尝试 运行 操作并检查 rails 控制台输出。 table 之上的方法最耗时。
我的故障排除已进入死胡同,我真的希望有人能提供帮助。
我正在使用 rack-mini-profiler 来帮助定位我的 Rails 网站中潜在的内存泄漏。我将其缩小到正在调用的方法。
高层简介:rack-mini-profiler high-level
SQL个人资料:rack-mini-profiler results
在 SQL 个人资料图片中,请注意灰色条上方和下方的查询开始时间相差 1037 毫秒。这就是我注意到的延迟,并且在我重新启动应用程序之前它会一直增长。
在本地运行这个时候,我可以监听终端。当请求该方法时,只有 1 秒的延迟,然后它就会执行。在此延迟期间,终端中不会显示任何查询或命令。
有没有人知道我如何找出导致延迟的原因?
我正在使用 Ruby 2.2.0 和 Rails 4.1.6.
谢谢!!
编辑:
这是 rack-mini-profiler 指向的方法:
def submit_answer
quiz_attempt = CourseCompletion.find_by_id(params[:course_completion_id]).quiz_started
choice = Choice.new(:answer_id => params[:answer], :quiz_attempt_id => quiz_attempt.id)
@success = choice.save
@answer = choice.answer
@question = @answer.question
@quiz_attempt = choice.quiz_attempt
render :layout => false
end
看看 ruby-prof github.com/ruby-prof/ruby-prof。尝试将您的动作主体包装到块中:
require 'ruby-prof'
# profile the code
RubyProf.start
# ... code to profile ...
result = RubyProf.stop
# print a flat profile to text
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)
您应该会在控制台中收到如下所示的输出:
您还可以将探查器的输出重定向到日志或服务器上的某个文件。
所以在你的情况下编辑代码:
def submit_answer
require 'ruby-prof'
# profile the code
RubyProf.start
quiz_attempt = CourseCompletion.find_by_id(params[:course_completion_id]).quiz_started
choice = Choice.new(:answer_id => params[:answer], :quiz_attempt_id => quiz_attempt.id)
@success = choice.save
@answer = choice.answer
@question = @answer.question
@quiz_attempt = choice.quiz_attempt
render :layout => false
result = RubyProf.stop
# print a flat profile to text
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)
end
添加到 gemfile
gem 'ruby-prof'
运行 bundle install
并重启服务器。再次尝试 运行 操作并检查 rails 控制台输出。 table 之上的方法最耗时。