views:

166

answers:

0

I'm attempting to estimate the round-trip time from the client to some web server (not the same domain the initial page was from) using Javascript running in the browser on the client side. Essentially, I'm looking to collect ping estimates to a variety of servers not under my control from the perspective of a number of clients, without the client having to do/install anything special. At the moment I'm using a bit of a hack to do this, which is to request an image from the server that I know does not exist and see how long it takes for the response to come back. The javascript code I'm using to test this is as follows:

<script type="text/javascript">
var ImageObject = new Image();
ImageObject.onerror = function(evt){
var endTime = new Date();
alert(endTime-startTime);
}
var startTime = new Date();
ImageObject.src = "http://www.someserver.com/nonexistentpic.jpg";
</script>

I've done very little Javascript work before and so have a couple of questions regarding this code:

  1. Is there any way the code could be made more efficient to reduce any additional time that gets tacked on to the measurement due to javascript parsing/executing?
  2. Is there a way to tell whether a DNS lookup had to take place first/remove the effects of DNS lookups on the measurement (I'm more interested in the round-trip time to the server itself, not the actual lookup time)? I was thinking just issue the request a few times so that the DNS entry should be cached after the first request...
  3. Would the measurement be drastically affected by imprecision in the timer resolution? E.g. the post at http://www.belshe.com/2010/06/04/chrome-cranking-up-the-clock/ says that even Chrome only gets a 4ms resolution on timers - would this apply to what I'm trying to do here, or is that something different all together?

As a bit of a reference point, on one domain I'm testing against, I'm getting back a result of ~1300ms through my Javascript estimate here, as opposed to using ping from the command line on the same domain with a result of ~270ms. So there's definitely some overhead going on in my Javascript estimate.