计算网络下载速度

时间:2013-02-23 10:49:32

标签: java networking download

我编写了以下代码来使用java计算下载速度。 但它没有给出正确的结果。问题是什么?。我的逻辑有问题,还是java网络类使用的问题?我认为使用java网络类是一个问题。任何人都可以告诉我究竟是什么问题?

/*Author:Jinu Joseph Daniel*/
import java.io.*;
import java.net.*;
class bwCalc {
    static class CalculateBw {
            public void calculateUploadBw() {}
            public float calculateDownloadRate(int waitTime) throws Exception {
               int bufferSize = 1;
               byte[] data = new byte[bufferSize]; // buffer
               BufferedInputStream in = new BufferedInputStream(new URL("https://www.google.co.in/").openStream());
               int count = 0;
               long startedAt = System.currentTimeMillis();
               long stoppedAt;
               float rate;
                while (((stoppedAt = System.currentTimeMillis()) - startedAt) < waitTime) {
                    if ( in .read(data, 0, bufferSize) != -1) {
                    count++;
                } else {
                    System.out.println("Finished");
                    break;
                }
            }
            in .close();
            rate = 1000 * (((float) count*bufferSize*8 / (stoppedAt - startedAt)) )/(1024*1024);//rate in Mbps
            return rate;
        }
    public float calculateAverageDownloadRate() throws Exception{
        int times[] = {100,200,300,400,500};
        float bw = 0,curBw;
        int i = 0, len = times.length;
        while (i < len) {
            curBw = calculateDownloadRate(times[i++]);
            bw += curBw;
            System.out.println("Current rate : "+Float.toString(curBw));
        }
        bw /= len;
        return bw;
    }
}
public static void main(String argc[]) throws Exception {
    CalculateBw c = new CalculateBw();
    System.out.println(Float.toString(c.calculateAverageDownloadRate()));
}
}

2 个答案:

答案 0 :(得分:2)

您的代码存在许多问题......

  • 您没有检查您正在阅读的字节数
  • 使用Google的主页进行测试是没用的,因为内容大小非常小,大部分下载时间与网络延迟有关;你应该尝试下载一个大文件(10+ MB) - 除非你真的想要测量延迟而不是带宽,在这种情况下你可以简单地运行ping
  • 如果你想获得任何相关结果,你还需要给它超过500毫秒 - 我要说至少5秒
  • 很多代码风格问题,但那些不太重要

答案 1 :(得分:1)

下面是代码,它将以KB为单位计算平均下载速率,以每秒MB为单位,您可以将它们缩放8以获得每秒位数的速率。

public static void main(String argc[]) throws Exception {
        long totalDownload = 0; // total bytes downloaded
        final int BUFFER_SIZE = 1024; // size of the buffer
        byte[] data = new byte[BUFFER_SIZE]; // buffer
        BufferedInputStream in = new BufferedInputStream(
                new URL(
                        "http://kernel.ubuntu.com/~kernel-ppa/mainline/v2.6.15/linux-headers-2.6.15-020615_2.6.15-020615_all.deb")
                        .openStream());
        int dataRead = 0; // data read in each try
        long startTime = System.nanoTime(); // starting time of download
        while ((dataRead = in.read(data, 0, 1024)) > 0) {
            totalDownload += dataRead; // adding data downloaded to total data
        }
        /* download rate in bytes per second */
        float bytesPerSec = totalDownload
                / ((System.nanoTime() - startTime) / 1000000000);
        System.out.println(bytesPerSec + " Bps");
        /* download rate in kilobytes per second */
        float kbPerSec = bytesPerSec / (1024);
        System.out.println(kbPerSec + " KBps ");
        /* download rate in megabytes per second */
        float mbPerSec = kbPerSec / (1024);
        System.out.println(mbPerSec + " MBps ");
    }