i am downloading any type of file and want to calculate Latency time that downloaded file.
Plzz help how it will be implemented in android.
Thanks
For me, something like this does the trick:
Process process = Runtime.getRuntime().exec("ping -c 1 google.com");
process.waitFor();
You can regex through the answer by reading InputStream provided by process.
I've been looking into a similar issue.
Here's some related resources that I've found. How to test internet speed (JavaSE)?
This blog post recommends using InetAddress.getByName(host).isReachable(timeOut) and then measuring response time.
http://tech.gaeatimes.com/index.php/archive/how-to-do-icmp-ping-in-java-jdk-15-and-above/
This is likely not the best solution, but it is easy. So something like this.
String host = "172.16.0.2";
int timeOut = 3000;
long[] time = new long[5];
Boolean reachable;
for(int i=0; i<5; i++)
{
long BeforeTime = System.currentTimeMillis();
reachable = InetAddress.getByName(host).isReachable(timeOut);
long AfterTime = System.currentTimeMillis();
Long TimeDifference = AfterTime - BeforeTime;
time[i] = TimeDifference;
}
Now you have an array of 5 values that showed roughly how long it took to see if the machine is reachable by ping; false otherwise. We know if the time it difference is 3 seconds then it timed out, you could also add in an array of booleans to show success vs fail rate.
This is not perfect, but gives a rough idea of the latency at a given time.
This matches the definition of latency found at the link below, except it is measuring both the send and return time rather than the time from sender to receiver: http://searchcio-midmarket.techtarget.com/definition/latency
Definition: In a network, latency, a synonym for delay, is an expression of how much time it takes for a packet of data to get from one designated point to another.
Doing more research shows that the isReachable might not work that great. Android Debugging InetAddress.isReachable
This might work better.
HttpGet request = new HttpGet(Url.toString());
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 as you are downloading the file, but give you an idea of the latency experienced on that network at a particular period of time.
If this helps, please accept this answer.