qragamu has asked for the wisdom of the Perl Monks concerning the following question:

I am quite happy with using Net:FTP for uplink and downlink file transfers, what I am having problems with is obtaining the same statistics that are provided by ftp when you perform a manual transfer, i.e file size, transfer duration and transfer speed in KBs. Is there any way of doing this?
  • Comment on Obtaining statistics for Net::FTP transfers

Replies are listed 'Best First'.
Re: Obtaining statistics for Net::FTP transfers
by grinder (Bishop) on Sep 06, 2007 at 08:07 UTC
    file size, transfer duration and transfer speed in KBs. Is there any way of doing this?

    You sure can. There is the underlying dataconn class (see the documentation), which will let you read block by block (of 4Kb for instance). You just need to keep running totals (and you will have to take care of writing the blocks yourself to the local file).

    You need to save a copy of epoch time (my $begin = time) at the beginning of the transfer) and then you can divide the amount of bytes of transferred by the seconds elapsed ($delta = time - $begin) to get the throughput in seconds.

    The size() method will allow you to obtain the size of the file on the remote server, which will enable you to estimate how far through the transfer you are. Be careful: if you are transferring text files between Unix and Windows systems, the sizes will differ due to newline conversions. Just clamp the progress rate to 100% if you wind up with more bytes on the receiving end than you started out with.

    If you don't need to know the dynamic transfer rate (during the transfer, that is), the just take the timestamp at the beginning and end to obtain the duration, and divide the size of the file by time taken, to obtain the overall throughput.

    • another intruder with the mooring in the heart of the Perl

      Thanks for that, I am still waiting for access to the system to try it. I shall let you know.
Re: Obtaining statistics for Net::FTP transfers
by atemon (Chaplain) on Sep 06, 2007 at 07:59 UTC

    the $ftp->message; will contain some message. For example

    #!/usr/bin/perl use Net::FTP; $ftp = Net::FTP->new("mysite.com", Debug => 0); $ftp->login("myusr",'mypwd'); print $ftp->message,"\n"; $ftp->cwd("/pub"); print $ftp->message,"\n"; $ftp->get("myfile.pdf"); print $ftp->message,"\n"; $ftp->quit;
    gives an output like
    User myusr logged in.
    
    CWD command successful
    
    Opening ASCII mode data connection for myfile.pdf (10816217 bytes)
    Transfer complete
    
    Here we gets the size in bytes.

    Cheers !

    --VC



    There are three sides to any argument.....
    your side, my side and the right side.

      Bizarre. I tried adding print $ftp->message,"\n"; after the ftp as you did above. It had no effect at all.
Re: Obtaining statistics for Net::FTP transfers
by monarch (Priest) on Sep 06, 2007 at 08:20 UTC
    May I suggest some ways to approach your problems. Firstly, have a good read of the Net::FTP documentation.

    Size: how big is a file? Well, if you are downloading the file, you can use the Net::FTP->size() function. If you are uploading a file, you can use the stat function (type perldoc -f stat for more information).

    Transfer duration: this shouldn't be too hard! Get the time (look at the time function) before you start the transfer, and subtract that from the time at the end of the transfer! Easy!

    Transfer speed: quite simply is file size divided by transfer time. Again, so basic, I'm wondering why you asked this.. maybe you want this value in real time? If so.. refer to the module documentation again! There is an option to the ->New() function called Hash that will notify you every time 1024 bytes are transferred. Read about it!

    Here's an example on how to get the size of files using Net::FTP (tested):

      Thanks, I already had Net::FTP->size() and perldoc -f stat, I was just hoping that there was an option in Net::FTP that I hadn't seen that produced the same printout as a manual session. I am using the 'time' as well, but I require millisecond accuracy. I know, then I should load the Time::HiRes Module. Tried that, but it needs a C complier and that's a no-no on the test system. So then I found a pre-compiled version ( as suggested by the Module's author ), but perl complains 'Can't locate loadable object for module Time::HiRes in @INC...etc' which is not very helpful.