Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

how to measure upload/download speed and latency in android wifi connection

I need some api or manipulative code by which i can measure the upload/download speed and the latency in wifi connection from an android application.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Are you using 2.2 (Froyo) or higher?

If so, in your application, import Traffic Stats and when your application is using the internet add in the following.

Download/Upload:

long BeforeTime = System.currentTimeMillis();
long TotalTxBeforeTest = TrafficStats.getTotalTxBytes();
long TotalRxBeforeTest = TrafficStats.getTotalRxBytes();

/* DO WHATEVER NETWORK STUFF YOU NEED TO DO */

long TotalTxAfterTest = TrafficStats.getTotalTxBytes();
long TotalRxAfterTest = TrafficStats.getTotalRxBytes();
long AfterTime = System.currentTimeMillis();

double TimeDifference = AfterTime - BeforeTime;

double rxDiff = TotalRxAfterTest - TotalRxBeforeTest;
double txDiff = TotalTxAfterTest - TotalTxBeforeTest;

if((rxDiff != 0) && (txDiff != 0)) {
    double rxBPS = (rxDiff / (TimeDifference/1000)); // total rx bytes per second.
    double txBPS = (txDiff / (TimeDifference/1000)); // total tx bytes per second.
    testing[0] = String.valueOf(rxBPS) + "B/s. Total rx = " + rxDiff;
    testing[1] = String.valueOf(txBPS) + "B/s. Total tx = " + txDiff;
}
else {
    testing[0] = "No uploaded or downloaded bytes.";
}

Now you have testing[0] is your download speed (roughly) and testing[1] is your upload speed (roughly)

Just make sure you are only calling this code when you are actually doing network communication or the time will skew your results.

As per latency, there is nothing that great out there. I've written this which is untested, but should work ok, but there are likely better solutions available.

Latency:

String host = YOUR_HOST
HttpGet request = new HttpGet(host);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);

for(int i=0; i<5; i++) {
    long BeforeTime = System.currentTimeMillis();
    HttpResponse response = httpClient.execute(request);
    long AfterTime = System.currentTimeMillis();
    Long TimeDifference = AfterTime - BeforeTime;
    time[i] = TimeDifference 
}

Note: Keep in mind that this will not say the latency at your time of use, but give you an idea of the latency experienced on the network you are using at a particular period of time. Also, this is the request and response time, instead of the request reaching the network time as say "ping" would normally do.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...