Page 1 of 1

Calculate latency

Posted: 02 Apr 2012, 17:48
by janheuninck
Hi,

I'm trying to display the latency on my client, but I can't get it to work properly. I searched on this forum and I found you guys recommend to use the lagmonitor in combination with the PingPong event. However, I can't find this in the iOS API.

Right now I calculate the latency by adding a unix timestamp to my packages sent from the server with:

Code: Select all

System.currentTimeMillis()


On client side I read this timestamp, and compare it with the current time on the client:

Code: Select all

        // Extract the timestamp from the message
        double timestamp = [[dictionary objectForKey:TIMESTAMP_KEY] doubleValue];

        // Calculate the latency
        NSNumber *systemTimestamp = [NSNumber numberWithDouble:1000.0 * [[NSDate date] timeIntervalSince1970]];
        int latency = (int) ([systemTimestamp doubleValue] - timestamp);


This should work, since I'm using the unix timestamps to calculate the latency. The result of this calculation is always +- 2750ms, which isn't correct (local network + network communication feels very responsive). Am I doing something wrong? Or are there better ways to do this?

Thanks

Re: Calculate latency

Posted: 02 Apr 2012, 18:46
by A51Integrated
Have the timestamp originate on the client - send it as an extension request or an object request. Simply turn it around on the server side and pass it back to the client. Then compare the current time with the received timestamp and you will have your round trip latency.

In short, the server's time may be slightly out of sync with your client. Better to originate timestamps on the same device.

Re: Calculate latency

Posted: 02 Apr 2012, 20:02
by janheuninck
I've implemented it the way you suggested. Thanks!