tags:

views:

558

answers:

3

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
A: 

Try removing the #sort! (just puts "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

rampion
Thanks, but it makes very little difference in this case - there are only about a dozen files in the directory. By the profiler, the #sort takes 0.00 ms. I've stepped (or rather nexted) through the code in the debugger, and it's the #nlst and the #ls that are taking the time.
Dave McLaughlin
ah, well, was worth a guess.
rampion
+4  A: 

From a google search:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/112910

Hope this helps.

Lee Irving
Outstanding! I've been struggling with this for days. I wish I could upvote this answer more than once.
Dave McLaughlin
+1  A: 

I had a similar issue and found Passive mode increased the speed, eg. ftp.passive=true.

Ben
Thanks. Disabling reverse DNS lookup worked for me, but others might try passive mode.
Dave McLaughlin