Measuring Download Speed Java
Asked Answered
W

3

6

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);
            }
        }
    }
Wore answered 12/6, 2011 at 15:30 Comment(0)
V
9

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.

Vicarious answered 12/6, 2011 at 15:35 Comment(1)
the formula to convert the time that InputStream.read() to KB-MB-GB/s? The answers helps to measure the time of execution of InputStream.read().Cruces
R
1

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.

Rootstock answered 12/6, 2011 at 15:35 Comment(4)
I dont know how to add it, i have a timer for get progress, but on the GUIWore
Put long startTime = System.nanoTime(); outside your while loop. and long estimatedTime = System.nanoTime() - startTime; will give you the current timeRootstock
Yes tried like this long startTime = System.nanoTime();, outside while, and Long end = System.currentTimeMillis()*1000;, inside, after buffer write, but seems to work only once?, also added this speed= (int) ((1024/(double) (start-end)));, because the buffer have 1024 bytesWore
@JesusDavidGulfoAgudelo, Hey I am looking for the same. Have your code worked after doing as you specified above comment.Emilieemiline
D
1

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);
        }
Determined answered 19/11, 2013 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.