I am working in a desktop application using java. In my application i have to perform a speed test which will show the file uploading and downloading speed.
For uploading test i am uploading a small test file to a FTP server and based on time taken i am calculating the file upload speed. similarly i开发者_高级运维 am downloading a test file form server and calculating download speed.
But result i am getting doesn't match with actual FTP file uploading and downloading speed.it seems that the establishing connection to FTP server is increasing the time, hence the resultant speed i am calculating is less.
here is the file uploading code i am using:
public int getTransferRate(File filename)
{
int trRate = 0;
try {
OutputStream fout = null;
InputStream bin = null;
connect(ftpUser,ftpPass,ftpServer);
ftp.setFileType(FTPSClient.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
fout = ftp.storeFileStream("testuploadfile");
bin = new FileInputStream(filename);
byte[] b = new byte[8192];
int bytesRead = 0;
long startTime = System.currentTimeMillis();
long endTime = 0;
while ((bytesRead = bin.read(b)) != -1) {
fout.write(b, 0, bytesRead);
bytesUploadedSet += bytesRead;
}
endTime = System.currentTimeMillis();
trRate = (int) ((float) bytesUploadedSet / (endTime - startTime));
} catch (IOException ex) {
Logger.getLogger(FTPFileStorageService.class.getName()).log(Level.SEVERE, null, ex);
}
return trRate;
}
Could you suggest any link or some way to get nearest uploading and downloading speed.
i thanks to all your valuable suggestion.
First of all ,filezilla probably uses native code which will be faster than what you are using.
For testing, establish a connection and try uploading many files, about 20 or so..that should give a good idea of the result..usually, also log your output to a comma separated test file or something, which you can later import into excel and analyze. You can use something like JMeter if you want to do some hard core performance testing..
In any kind of performance testing having a large sample size (lot of sample results) gives most accurate results.
http://jmeter.apache.org/
Are you not able to start the timer after the connection is established?
If not then for a quick partial solution, increase the size of the test file, the bigger it is, the less significant the connection overhead will be.
If you need to be more accurate, then i'll defer to somebody with more hava experience...
Can you use a simpler protocol such as HTTP?
Setting up an FTP transfer can be fiddly: separate control and data connections need to be established, and both endpoints need to agree on whether to use passive or active mode to get around firewalls and NAT. This process can involve just trying one of the options (e.g. active) and waiting for success or a timeout, which could be what's distorting your measurements.
精彩评论