计算网络下载速度 [英] Calculating network download speed

查看:205
本文介绍了计算网络下载速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  / *作者:Jinu Joseph Daniel * / 
import java .IO *。
import java.net。*;
class bwCalc {
static class CalculateBw {
public void calculateUploadBw(){}
public float calculateDownloadRate(int waitTime)throws异常{
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;
浮动利率;
while((stopAt = System.currentTimeMillis()) - startedAt)< waitTime){
if(in .read(data,0,bufferSize)!= -1){
计数++;
} else {
System.out.println(Finished);
break;
}
}
in .close();
rate = 1000 *(((float)count * bufferSize * 8 /(stoppedAt - startedAt)))/(1024 * 1024); //以Mbps
返回率的速率;
}
public float calculateAverageDownloadRate()throws Exception {
int times [] = {100,200,300,400,500};
float bw = 0,curBw;
int i = 0,len = times.length;
while(i curBw = calculateDownloadRate(times [i ++]);
bw + = curBw;
System.out.println(当前速率:+ 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()));
}
}


解决方案

你的代码有很多问题...




  • 你没有检查你正在阅读多少字节

  • 由于内容大小非常小,大部分下载时间与网络延迟有关,所以使用Google的主页进行测试是无用的;您应该尝试下载一个大文件(10+ MB) - UNLESS ,您实际上要测量延迟而不是带宽,在这种情况下,您可以简单地运行ping
  • 如果你想得到任何相关的结果,你还需要提供超过500ms - 我会说至少5秒

  • 大量的代码风格问题,但那些不太重要li>

I have written the follwing code to calculate download speed using java. But it is not giving correct results.What is the problem?.Is there a problem with my logic , or is it a problem with java networking classes usage?I think it is a problem with the usage of java networking classes.Can anybody tell me what exactly the problem is?

/*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()));
}
}

解决方案

There are many problems with your code...

  • you're not checking how many bytes you are reading
  • testing with Google's home page is useless, since the content size is very small and most of the download time is related to network latency; you should try downloading a large file (10+ MB) - UNLESS you actually want to measure latency rather than bandwidth, in which case you can simply run ping
  • you also need to give it more than 500ms if you want to get any relevant result - I'd say at least 5 sec
  • plenty of code style issues, but those are less important

这篇关于计算网络下载速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆