Android 使用我自己的代码进行测试时不同的互联网速度

Android 使用我自己的代码进行测试时不同的互联网速度,android,performance,Android,Performance,我在检查互联网速度时遇到了一个问题。实际上,我正在开发一款android应用程序,可以在手机上测试你的上网速度。我制作了一个样本来测试速度和它的显示写入速度,比如我从ISP获得的7.3Mbps。但是在这个测试中,我使用了下面的代码 long startCon=System.currentTimeMillis(); Log.i(“mymobilespeedtest”、“启动连接=“+startCon”) 现在我想通过使用一个处理程序和使用不同的方法下载一个文件来显示互联网速度的进展 在这个方法中,

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

long startCon=System.currentTimeMillis(); Log.i(“mymobilespeedtest”、“启动连接=“+startCon”)

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

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

         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的速度(兆比特/秒而不是字节)

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

选中此项:

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

这就是为什么你得到了一个错误的值


我知道这有点晚了,但是你解决了吗?

如果你想测量下载速度,你应该去掉消息处理代码。从套接字读取,不要将读取的字节存储在任何位置。现在,由于代码中的瓶颈,速度可能会降低。谢谢您的帮助。事实上,我没有得到任何解决方案,而不是套接字编程,这是非常大的时间,这就是为什么我跳过这件事。与此功能相比,此项目的时间非常重要。
         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);