A couple of weeks ago, I wrote a simple Ruby script to test a couple of FTP commands in a Windows XP environment. Everything worked as expected, and I wasn't even aware of the time taken for the code to run (I'd guess 3-4 seconds at the very most.)
A few days ago, a much more involved Ruby application I'm developing started running very slowly. As I investigated the issue, I isolated the problem to the FTP commands. I've now rerun the original test script, and it takes over two minutes to run. Command-line FTP is essentially instantaneous.
No files in the ruby directory structure have been changed. I do not believe any new applications have been installed - certainly no other applications appear to be running.
Can anyone suggest why the following code should run so slowly? Manually timing the intervals between print
statements suggest the nlst
and ls
take about 65 seconds each! The profiler gives a much more plausible total ms/call of 16 for nlst
and 31 for ls
.
require 'net/ftp'
Net::FTP.open("ip_redacted", "user_redacted", "password_redacted") do |ftp|
ftp.chdir("dir_redacted")
files = ftp.nlst
print "files = #{files.sort!}\n"
list = ftp.ls
print "list = #{list}\n"
file = "filename_redacted"
size = ftp.size(file)
print "size = #{size}\n"
end
From a google search:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/112910
Hope this helps.
Try removing the
#sort!
(justputs "files = #{files}"
) since that can be fairly expensive if there's a bunch of files in the directory. This would account for the large lag around the#nlst
and the#ls
I had a similar issue and found Passive mode increased the speed, eg.
ftp.passive=true
.