通过我自己的代码测试时的互联网速度不同

时间:2013-10-29 14:16:45

标签: android performance

我在检查互联网速度方面遇到了问题。其实我正在开发一个Android应用程序,可以测试你的手机上的网速。我做了一个样本来测试速度和它的显示写入速度,例如我从ISP获得的7.3 Mbps。但是在这个测试中我使用的是下面的代码。

long startCon = System.currentTimeMillis();             Log.i(“mymobilespeedtest”,“start conn =”+ startCon);

        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(imageURL);
        HttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            Log.i("SketchEffect","Executing http connection to download selected image.");
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            Log.i("FBAlbum", e.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("FBAlbum", e.toString());
        }
        long endCon = System.currentTimeMillis();
        Log.i("mymobilespeedtest", "endCon = " + endCon);

现在我想通过使用处理程序和使用不同的方法下载文件来显示互联网速度的进展。'

在这个方法中我使用下面的代码

         String downloadFileUrl =    "http://www.gregbugaj.com/wp-content/uploads/2009/03/dummy.txt";
          URL    url1 = new URL(downloadFileUrl);
            URLConnection con1 = url1.openConnection();
            con1.setUseCaches(false); stream1 = con1.getInputStream();
            Message msgUpdateConnection = Message.obtain(mHandler,
                    MSG_UPDATE_CONNECTION_TIME);
            msgUpdateConnection.arg1 = (int) connectionLatency;
            mHandler.sendMessage(msgUpdateConnection);

            long start = System.currentTimeMillis();
            int currentByte = 0;
            long updateStart = System.currentTimeMillis();
            long updateDelta = 0;
            int bytesInThreshold = 0;
            int bytesIn = 0;
            while (true) {
                if ((currentByte = stream1.read()) == -1) {
                    break;
                } else {
                    bytesIn++;
                    bytesInThreshold++;
                    baf.append((byte) currentByte);
                    if (updateDelta >= UPDATE_THRESHOLD) {
                        int progress = (int) ((bytesIn / (double) EXPECTED_SIZE_IN_BYTES) * 100);
                        Message msg = Message.obtain(mHandler,
                                MSG_UPDATE_STATUS,
                                calculate(updateDelta, bytesInThreshold));
                        msg.arg1 = progress;
                        msg.arg2 = bytesIn;
                        mHandler.sendMessage(msg);
                        // Reset
                        updateStart = System.currentTimeMillis();
                        bytesInThreshold = 0;
                    }
                    updateDelta = System.currentTimeMillis() - updateStart;
                }

            } if (downloadTime == 0) {
                downloadTime = 1;
            }

            Message msg = Message.obtain(mHandler, MSG_COMPLETE_STATUS,
                    calculate(downloadTime, bytesIn));
            msg.arg1 = bytesIn;
            mHandler.sendMessage(msg);

通过上面的代码我得到的速度只有.8或1.o Mbps(每个sega的兆位而不是字节)

1 个答案:

答案 0 :(得分:0)

我试图做同样的事情,使用HTTP GET来衡量互联网速度(下载),但是有一个问题。执行此操作时:响应= httpClient.execute(httpGet); 您不仅仅测量接收答案所需的时间,而是衡量完成整个请求所需的时间,这意味着您正在测量整个协议的开销,例如在服务器和客户端之间建立TCP连接。

请检查:http://tinypic.com/r/15u6og/8

您需要的有用数据是PDU,其余的只是开销。但是你正在衡量整个事情。如您所见,建立TCP连接需要花费大量时间。

这就是你得错值的原因。

我知道这有点晚了,但你有没有解决这个问题呢?