Hi,
To tell you some more details. It's a rails application using sequel to connect to postgresql. And I've managed to pinpoint a call that's taking too long. By profiling passenger's PhusionPassenger::Rack::ThreadHandlerExtension#process_request method, located at /usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:
%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
...
In case someone will run into it and find it useful, here's what exactly I did:
1) added the following code to the beginning of /usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:
def __l *args
File.open('/home/gccs/' + 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) added the following code at the beginning of process_action method:
__start = Time.now
__l '-' * 80, __start, env['REQUEST_URI'], env['HTTP_X_REAL_IP']
__start_profiler
3) put result of big begin...end block at the end of the method into r variable
4) added the following code at the end of the method:
__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) before each test ran:
rm -f ~/*.log && touch tmp/restart.txt && watch 'grep elapsed ~/*.log | sort -gr -k2 | head'
Now then, maybe you have any suggestions now?
Regards,
Yuri