如何使用Java/Android正确地测量下载速度

4

我正在尝试在我的Android应用程序中执行一个AsyncTask类,以分析网络连接速度,包括下载和上传。目前我正在处理下载部分,但是我得到的结果与我期望的结果不符。我在一个Wifi网络上进行测试,该网络始终获得15Mbps的下载/上传速度,但是我的应用程序得到的结果只有大约1Mbps左右。当我在设备上运行速度测试apk时,得到的结果约为3.5Mbps。这个函数可以工作,但似乎只有应该有一半的速度。以下代码是否能产生准确的结果?

try {
           String DownloadUrl = "http://ipv4.download.thinkbroadband.com:8080/5MB.zip";
           String fileName = "testfile.bin";
       
       
           File dir = new File (context.getFilesDir() + "/temp/");
           if(dir.exists()==false) {
                dir.mkdirs();
           }

           URL url = new URL(DownloadUrl); //you can write here any link
           File file = new File(context.getFilesDir() + "/temp/" + fileName);
           

           long startTime = System.currentTimeMillis();
           Log.d("DownloadManager", "download begining: " + startTime);
           Log.d("DownloadManager", "download url:" + url);
           Log.d("DownloadManager", "downloaded file name:" + fileName);
           
           /* Open a connection to that URL. */
           URLConnection ucon = url.openConnection();

           //Define InputStreams to read from the URLConnection.
           InputStream is = ucon.getInputStream();
           BufferedInputStream bis = new BufferedInputStream(is);

           //Read bytes to the Buffer until there is nothing more to read(-1).  
           ByteArrayBuffer baf = new ByteArrayBuffer(1024);
           int current = 0;
           while ((current = bis.read()) != -1) {
              baf.append((byte) current);
           }
           long endTime = System.currentTimeMillis(); //maybe

           /* Convert the Bytes read to a String. */
           FileOutputStream fos = new FileOutputStream(file);
           fos.write(baf.toByteArray());
           fos.flush();
           fos.close();
           
          File done = new File(context.getFilesDir() + "/temp/" + fileName);
          Log.d("DownloadManager", "Location being searched: "+ context.getFilesDir() + "/temp/" + fileName);
          double size = done.length();
          if(done.exists()) {
              done.delete();
          }

          Log.d("DownloadManager", "download ended: " + ((endTime - startTime) / 1000) + " secs");
          double rate = (((size / 1024) / ((endTime - startTime) / 1000)) * 8);
          rate = Math.round( rate * 100.0 ) / 100.0;
          String ratevalue;
          if(rate > 1000)
             ratevalue = String.valueOf(rate / 1024).concat(" Mbps");
          else
             ratevalue = String.valueOf(rate).concat(" Kbps"); 
          Log.d("DownloadManager", "download speed: "+ratevalue);       
   } catch (IOException e) {
       Log.d("DownloadManager", "Error: " + e);
   }

示例输出

10-08 15:09:52.658: D/DownloadManager(13714): download ended: 70 secs
10-08 15:09:52.662: D/DownloadManager(13714): download speed: 585.14 Kbps

感谢您提前的帮助。如果有更好的方法,请告诉我。


你正在一次读取一个字节,这可能会很慢。为什么不使用byte[]一次读取几KB呢? - njzk2
此外,您的速率值可能不是非常精确,因为sizeendTimestartTime都是整数,就像1024、1000和8一样,因此在转换为长整型之前,计算是以整数形式进行的(这可能会导致不精确的测量结果,但并不是您观察到的巨大差异的原因)。 - njzk2
我的时间值设置为long类型,并以毫秒为单位进行测量。@njzk2 我会尝试一次读取更多字节。我在其他网络上测试了几次,似乎得到了更接近的结果,所以可能这是我家庭WiFi的问题。 - Joffroi
在这里的“mean integer”中,转换为double类型是在计算完成后进行的,因此存在精度损失。然而,我相当有信心认为读取方法是速度问题的原因。 - njzk2
@Joffroi 我正在尝试构建一个执行速度测试的应用程序,但我远远无法达到Speedtest.net(OOKLA)的结果。您是否遇到过这种情况,如果可能,请指导我。 - Mukeshkumar S
显示剩余3条评论
1个回答

4

在我之前的评论中,我提到了如何从数据流中读取多个字节的示例。

//Define InputStreams to read from the URLConnection.
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

//I usually use a ByteArrayOutputStream, as it is more common.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int red = 0;
// This size can be changed
byte[] buf = new byte[1024];
while ((red = bis.read(buf)) != -1) {
    baos.write(buf, 0, red);
}

这段代码的作用是读取到一个byte[]缓冲区中,并返回读取的字节数。接着将其写入OutputStream,指定要写入的字节数。 ByteArrayOutputStream也有一个类似的toByteArray方法。
另外,如果你认为文件写入操作比读取函数快得多,也可以直接写入文件。
// Simply start by defining the fileoutputstream
FileOutputStream fos = new FileOutputStream(file);
int red = 0;
// This size can be changed
byte[] buf = new byte[1024];
while ((red = bis.read(buf)) != -1) {
    // And directly write to it.
    fos.write(buf, 0, red);
}
long endTime = System.currentTimeMillis(); //maybe
// Flush after, as this may trigger a commit to disk.
fos.flush();
fos.close();

此外,如果您只关心下载速度,无需将内容写入文件或任何地方,仅执行以下操作即可:
long size = 0;
byte[] buf = new byte[1024];
while ((red = bis.read(buf)) != -1) {
    size += red;
}

非常抱歉没有及时回复。我真的很感激你详细的回复。我尝试了两种方法,最终采纳了建议,甚至不写数据大小,我真正想关注的是下载速度。自从改变为多字节后,我的结果已经非常接近或其他速度测试。再次感谢您的示例,它们完美地发挥作用。 - Joffroi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接