Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
2.3k views
in Java FTP by (140 points)
Hi

Is there a way to calculate or measure the transfer rate of a file?

in the bytesTransferred(long bytes) method, I have
bytes transered for file
bytes transfered since bytesTransferred(long bytes) was last called

start is a global variable, initially set to 0;

The problem I am having is that sometimes this is fired and the time taken is only a few ms, sometimes 5 or even 1 which causes my transfer rate to be upto 8MB when my broadband connection is 200Kb/s.

Also, how efficient is the ftp library? Does it get good transfer rates compared to command line ftp?

Thanks,
Rob

public void bytesTransferred(long bytes) {
      long end = System.currentTimeMillis();
      Log.debug("End : " + end);
      long begin = start;
      Log.debug("Begin : " + begin);
      start = System.currentTimeMillis();
      long bytesChunk = bytes - bytesSinceLastUpdate;
      
      float kps = 1;
      if (begin > 0) {
         float timeTaken = end - begin;
         Log.debug("Time taken : " + timeTaken);
         timeTaken = timeTaken / 1000;
         float k = bytesChunk/1024;
         kps = k/timeTaken;
         Log.debug(kps+"KB/s");
         Log.debug("Chunk bytes : " + bytesChunk);
      }
      
      totalBytesTransfered += bytesChunk;
      
      Log.debug(bytes + "bytes transferred");
      Log.debug(fileSize + "bytes to transfer");
      Log.debug(bytes/fileSize*100 + "%");
      Log.debug("Total transfered : " + totalBytesTransfered);
      Log.debug("Total to transfer : " + totalBytesToTransfer);
      Log.debug("Total : " + (totalBytesTransfered/totalBytesToTransfer)*100 + "%");
      Log.debug("Total time left " + (totalBytesToTransfer/1000) / kps + " seconds");
   bytesSinceLastUpdate = bytes;
   }

1 Answer

0 votes
by (162k points)
The best way to measure transfer rate is to time how long it takes for the entire file to transfer and work it out from the size of the file.

The problem with bytesTransferred() is that (especially for sending a file) when bytes are written, they are probably written to an internal OS buffer that isn't necessarily flushed and written to the network immediately. So for small increments it won't be a very accurate guide. You are best setting the notification interval to say 1 MB for larger files (e.g. 50 MB).

As for performance, edtFTPj has been benchmarked independently and shown to be one of the fastest libraries for transferring large files (79% of command line FTP on Windows) and by far the fastest for small files (significantly faster than command line FTP on Windows):

http://www.javaworld.com/javaworld/jw-0 ... tml?page=3

Categories

...