XHR请求随机花费太多时间来完成。我找不到发生这种情况的地方。如果有的话。当我在可疑的ruby代码块周围启用分析器时,阻塞就发生在其他地方。但是我无法用webrick
复制它。有什么想法吗?
答案 0 :(得分:0)
这就是我的所作所为:
1)将以下代码添加到/usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb
的开头:
def __l *args
File.open('/home/USER/' + Process.pid.to_s + '.log', 'a') { |f| f.puts *args }
end
require 'ruby-prof'
def __start_profiler
RubyProf.start
if false
require 'profiler'
$old_compile_option = RubyVM::InstructionSequence.compile_option.select { |k, v|
[:trace_instruction, :specialized_instruction].include? k
}
RubyVM::InstructionSequence.compile_option = {
:trace_instruction => true,
:specialized_instruction => false
}
Profiler__::start_profile
end
end
def __get_profiler_output
sio = StringIO.new
result = RubyProf.stop
# printer = RubyProf::GraphPrinter.new(result)
printer = RubyProf::FlatPrinter.new(result)
printer.print(sio)
return sio.string
if false
Profiler__::print_profile(sio)
RubyVM::InstructionSequence.compile_option = $old_compile_option
sio.string
end
end
2)在process_action
方法的开头添加了以下代码:
__start = Time.now
__l '-' * 80, __start, env['REQUEST_URI'], env['HTTP_X_REAL_IP']
__start_profiler
3)将方法末尾的大begin...end
块的结果放入r
变量
4)在方法的最后添加了以下代码:
__r = __get_profiler_output
if Time.now - __start > 10
__l 'profiler'
__l __r
__l 'profiler'
end
__l 'elapsed: %g: %s' % [Time.now - __start, env['REQUEST_URI']], '-' * 80
r
5)每次测试开始之前:
rm -f ~/*.log && touch tmp/restart.txt && watch 'grep elapsed ~/*.log | sort -gr -k2 | head'
能够找到罪魁祸首:
%self total self wait child calls name
99.92 65.713 65.713 0.000 0.000 5 PG::Connection#async_exec
0.00 0.002 0.002 0.000 0.000 264 Set#delete
...