Measuring Download Speed Java
Solution 1
The same way you would measure anything.
System.nanoTime()
returns a Long
you can use to measure how long something takes:
Long start = System.nanoTime();
// do your read
Long end = System.nanoTime();
Now you have the number of nanoseconds it took to read X bytes. Do the math and you have your download rate.
More than likely you're looking for bytes per second. Keep track of the total number of bytes you've read, checking to see if one second has elapsed. Once one second has gone by figure out the rate based on how many bytes you've read in that amount of time. Reset the total, repeat.
Solution 2
here is my implementation
while (mStatus == DownloadStatus.DOWNLOADING) {
/*
* Size buffer according to how much of the file is left to
* download.
*/
byte buffer[];
// handled resume case.
if ((mSize < mDownloaded ? mSize : mSize - mDownloaded <= 0 ? mSize : mSize - mDownloaded) > MAX_BUFFER_SIZE) {
buffer = new byte[MAX_BUFFER_SIZE];
} else {
buffer = new byte[(int) (mSize - mDownloaded)];
}
// Read from server into buffer.
int read = stream.read(buffer);
if (read == -1)
break;// EOF, break while loop
// Write buffer to file.
file.write(buffer, 0, read);
mDownloaded += read;
double speedInKBps = 0.0D;
try {
long timeInSecs = (System.currentTimeMillis() - startTime) / 1000; //converting millis to seconds as 1000m in 1 second
speedInKBps = (mDownloaded / timeInSecs) / 1024D;
} catch (ArithmeticException ae) {
}
this.mListener.publishProgress(this.getProgress(), this.getTotalSize(), speedInKBps);
}
Solution 3
I can give you a general idea. Start a timer at the beginning of the download. Now, multiply the (percentage downloaded)
by the download size
, and divide it by the time elapsed.
That gives you average download time. Hope I get you on the right track!
You can use System.nanoTime();
as suggested by Brian.
Put long startTime = System.nanoTime();
outside your while loop. and
long estimatedTime = System.nanoTime() - startTime;
will give you the elapsed time within your loop.
Jesus David Gulfo Agudelo
Updated on June 25, 2022Comments
-
Jesus David Gulfo Agudelo almost 2 years
I'm working on downloading a file on a software, this is what i got, it sucesfully download, and also i can get progress, but still 1 thing left that I dont know how to do. Measure download speed. I would appreciate your help. Thanks. This is the current download method code
public void run() { OutputStream out = null; URLConnection conn = null; InputStream in = null; try { URL url1 = new URL(url); out = new BufferedOutputStream( new FileOutputStream(sysDir+"\\"+where)); conn = url1.openConnection(); in = conn.getInputStream(); byte[] buffer = new byte[1024]; int numRead; long numWritten = 0; double progress1; while ((numRead = in.read(buffer)) != -1) { out.write(buffer, 0, numRead); numWritten += numRead; this.speed= (int) (((double) buffer.length)/8); progress1 = (double) numWritten; this.progress=(int) progress1; } } catch (Exception ex) { echo("Unknown Error: " + ex); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException ex) { echo("Unknown Error: " + ex); } } }
-
Jesus David Gulfo Agudelo almost 13 yearsI dont know how to add it, i have a timer for get progress, but on the GUI
-
Anirudh Ramanathan almost 13 yearsPut long startTime = System.nanoTime(); outside your while loop. and long estimatedTime = System.nanoTime() - startTime; will give you the current time
-
Jesus David Gulfo Agudelo almost 13 yearsYes tried like this
long startTime = System.nanoTime();
, outside while, andLong end = System.currentTimeMillis()*1000;
, inside, after buffer write, but seems to work only once?, also added thisspeed= (int) ((1024/(double) (start-end)));
, because the buffer have 1024 bytes -
Amogh over 9 years@JesusDavidGulfoAgudelo, Hey I am looking for the same. Have your code worked after doing as you specified above comment.
-
Admin almost 7 yearsthe formula to convert the time that
InputStream.read()
to KB-MB-GB/s? The answers helps to measure the time of execution ofInputStream.read()
.