Android计算下载速度测试

时间:2016-06-22 08:33:07

标签: android performance testing download

我正在尝试计算下载速度测试计算。 在Stackoverflow中找到了很多信息,但没有任何帮助。 最终的计算不是逻辑。 试图了解while线程中的下载速度。 附上我找到的代码。

 public void run() {
            OutputStream out = null;
            URLConnection conn = null;
            InputStream in = null;
            try
            {
                URL url1 = new URL("test");
                out = new BufferedOutputStream(new FileOutputStream(getVideoFile().getPath()));
                conn = url1.openConnection();
                in = conn.getInputStream();
                long start = System.currentTimeMillis();
                byte[] buffer = new byte[1024];
                int numRead;
                long numWritten = 0;
                while ((numRead = in.read(buffer)) != -1)
                {
                    out.write(buffer, 0, numRead);
                    numWritten += numRead;

                    long end = System.currentTimeMillis();

                    if ((end - start)>0) {
                        double rate = 1000f *  numWritten / (end - start) ;
                        Log.d("downloadmanager","speed "+rate);
                    }
                }


            }
            catch (Exception ex)
            {
                Log.d("downloadmanager","Unknown Error: " + ex);
            }
            finally
            {
                try
                {
                    if (in != null)
                    {
                        in.close();
                    }
                    if (out != null)
                    {
                        out.close();
                    }
                }
                catch (IOException ex)
                {
                    Log.d("downloadmanager", "Unknown Error: " + ex);
                }
            }
        }
    }).start();

感谢您帮助解决这个问题。

1 个答案:

答案 0 :(得分:1)

我试过这样的方式,希望它会有所帮助! 改变是,我使用了一组网址。

    public class TestSpeed extends AsyncTask<Void,Void,Void> {


    String TAG = "TestSpeed";
    long startTime,endTime,contentLength;
    String vURL[] = {
            "http://test/url/file1",
            "http://test/url/file2",
            "http://test/url/file3",
            "http://test/url/file4",
            "http://test/url/file5",
            "http://test/url/file6",
            "http://test/url/file7"
            "http://test/url/file8"
    };

    @Override
    protected Void doInBackground(Void... params) {
        setCall(vURL[GlobalData.URLIndex]);
        GlobalData.URLIndex += 1;
        if(GlobalData.URLIndex >= vURL.length){
            GlobalData.URLIndex = 0;
        }
        return null;
    }

    void setCall(String URL){
        try {
            Log.d(TAG,"Start " + URL);
            startTime = System.currentTimeMillis(); //Hold StartTime
            HttpGet httpRequest = new HttpGet(new URL(URL).toURI());
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
            endTime = System.currentTimeMillis();  //Hold EndTime

            HttpEntity entity = response.getEntity();
            BufferedHttpEntity bufHttpEntity;
            bufHttpEntity = new BufferedHttpEntity(entity);

            //You can check the size of your file
            contentLength = bufHttpEntity.getContentLength();

            // Log
            Log.d("TAG", "Dowload time :" + (endTime - startTime) + " ms");
            // Speed : size(KB)/time(s)
            Long mSpeed = contentLength / ((endTime - startTime) * 1000);
            Log.d(TAG, "mSpeed :" + mSpeed);

            Double duration = Double.valueOf((endTime - startTime));
            Double speedKbps = Double.valueOf(roundTwoDecimals(Double.valueOf(contentLength / 1024)));
            Double speedMbps = roundTwoDecimals(speedKbps / 1024);
            Log.d(TAG,"" + speedKbps);
            Log.d(TAG,"" + speedMbps);

            //confirm units for display
            String mTheSpeed = "" + speedMbps;
            String speed;
            if(mTheSpeed.charAt(0) == '0'){
                speed = roundTwoDecimals(speedKbps) + " kb/s";
            }else{
                speed = roundTwoDecimals(speedMbps) + " mb/s";
            }

            Log.d("Speed Result: ","" + speed);

        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    Double roundTwoDecimals(Double vLongValue)
    {
        DecimalFormat twoDForm = new DecimalFormat("#.###");
        return Double.valueOf(twoDForm.format(vLongValue));
    }
}

对于替代方案,您可以使用WifiInfo,代码如下所示。

private static Integer wifiSpeed(Context mContext){
      WifiManager wifiManager = (WifiManager);
      mContext.getSystemService(Context.WIFI_SERVICE);
      WifiInfo wifiInfo = wifiManager.getConnectionInfo();
      Integer wifiSpeed = wifiInfo.getLinkSpeed();
      return wifiSpeed;
}