测量网络吞吐量Java / Android

时间:2014-04-14 09:48:37

标签: java android http networking

我正在为Android做一个应用程序,并希望使用HTTP来衡量网络吞吐量。 到目前为止,已经完成了对www.google.pt页面进行HTTP GET的代码。 我衡量请求之前和之后的时间。

问题:我正在测量服务器和我的应用程序之间的整个数据交换的时间,以及TCP协议的所有开销。我只需要花时间让我的应用程序只接收网页(有用的数据)。

我使用wireshark查看请求期间发生了什么。 有用的数据是您看到的PDU。这就是我想要的。

http://imgur.com/aodpZRQ

http://tinypic.com/r/15u6og/8

public static String GET(String url){
        InputStream inputStream = null;
        String result = "";
        try {

            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            // Time Functions Before
            Calendar calendarBefore = Calendar.getInstance();// instance class Calendar
            int millisecondsBefore = calendarBefore.get(Calendar.MILLISECOND);

            // make GET request to the given URL
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

            // Time Functions After
            Calendar calendarAfter = Calendar.getInstance();// instance class Calendar
            int millisecondsAfter = calendarAfter.get(Calendar.MILLISECOND);

            // Time Difference Calculation
            int timeDiff = millisecondsAfter - millisecondsBefore;

            String timeDifference = new String(); // time difference (After-Before)

            //Verify the signal of the timeDifference value
            if(timeDiff < 0){
                timeDiff = timeDiff * (-1); // reverse the signal in the operation
                timeDifference=Integer.toString(timeDiff);
            }
            else{
                timeDifference=Integer.toString(timeDiff);                  
            }


            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // convert input stream to string

            if(inputStream != null)
                result = convertInputStreamToString(inputStream, timeDiff);
            else
                result = "Did not work!";

        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }

        return result;
    }

所以,我正在测量很多我不需要的东西。我需要测量接收这些PDU的时间。

如果你们知道任何可以帮助我的事情,对我帮助很大。如果您有任何其他建议,我很乐意听到。

谢谢!

1 个答案:

答案 0 :(得分:0)

可能会有所帮助:

public static String GET(String url){
        InputStream inputStream = null;
        String result = "";
        try {


            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();

           //Get Bytes of data Consumed
           long oldCount = android.net.TrafficStats.getMobileRxBytes();

            // make GET request to the given URL
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

             //Get Bytes of data Consumed
             long newCount = android.net.TrafficStats.getMobileRxBytes();

            //GetTotalData Consumed
            Log.i("totalDataConsumption",(newCount-oldCount)+" Bytes");

            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // convert input stream to string
            if(inputStream != null)
                result = convertInputStreamToString(inputStream, timeDiff);
            else
                result = "Did not work!";

        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }

        return result;
    }
相关问题