Measuring server response time (client-side)
Solution 1
You just need to time a request:
var sendDate = (new Date()).getTime();
$.ajax({
//type: "GET", //with response body
type: "HEAD", //only headers
url: "/someurl.htm",
success: function(){
var receiveDate = (new Date()).getTime();
var responseTimeMs = receiveDate - sendDate;
}
});
Solution 2
Create a resource that always returns an empty 200 OK response at a url like mysite.net/speedtest. Then something like:
var startTime = (new Date()).getTime(),
endTime;
$.ajax({
type:'GET',
url: 'http://mysite.net/speedtest',
async: false,
success : function() {
endTime = (new Date()).getTime();
}
});
alert('Took ' + (endTime - startTime) + 'ms');
That will give you the time it takes to send a request to your server, plus the time it takes your server to load the minimum set of resources it requires to respond to a request.
New: Your question is now inconsistent with the additional information you've added. You want to get the client's ping to a URL that you don't control - that's quite a different matter than what you initially asked for, getting the response time from your server. If you're trying to get a client's ping to your server, an ajax call will do the trick. If you're trying to get their ping to some other server, then I'm a bit confused as to what your goal is.
Kraken
Updated on February 14, 2020Comments
-
Kraken over 4 years
I would like to implement a script that would measure the response time of my server (= remote url) from a client using a regular browser without any extra plugins (Java etc.).
I'm only looking at the network speed (response time), not the page loading speed.
What would you recommend me to measure? It has to run on tcp/ip, not anything else like ICMP (ping).
How would you go around the implementation on the client side? I would prefer to use JavaScript (JQuery).
Update: As I need a cross domain check, the ajax calls don't seem to be an option
Furthermore, the ajax method doesn't seem to precise at all. Results seem to have an overhead of about 50ms (it's almost a constant value, no matter what the domain is - I guess it's the processing time in between) on the testing machine in comparison to information from FireBug
-
AmericanUmlaut over 11 yearsresponseTimeMs isn't visible outside the scope of your success function.
-
Kraken over 11 yearsYes but as far as I know this returns the whole response including the html code. In that case the speed of the site would affect the response time. Am I right?
-
Robert Fricke over 11 yearsThis would alert before ajax request was finished.
-
Robert Fricke over 11 yearsIf you don't want the server to return a response body, use
type: "HEAD"
, and only headers are returned. -
Kraken over 11 yearsThat sounds reasonable, will check it out. Thank you so far.
-
Kraken over 11 yearsThat's the thing, I can't make it empty. Anyway, I think if I just request the headers using @RobertFricke solution I might go around this problem
-
Licson over 11 years@kraken yes you can just get the header and the problem is solved.
-
Kraken over 11 yearsSadly, it seems that this method is not precise enough. I also would have a problem with a cross domain check. I don't know the internal implementation of the ajax call but it seems that it fetches the headers even though it throws the error callback. From that I can see that it takes roughly 100ms on google.com (50ms in Firebug), 400 ms on example.com (about 350ms in Firebug) etc. I have tested this on several domains. I don't know why it fetches the headers even though it shouldn't (error callback) but from this I infer that there would be a problem with precision.
-
AmericanUmlaut over 11 years@RobertFricke Oops, you're right. I fixed it, thanks for the note.
-
Robert Fricke over 11 yearscool beans. However by the looks of this,
endTime
is set correctly, but thealert
still runs before thesuccess
-callback since it will execute directly after the ajax-call (before a callback is made). -
AmericanUmlaut over 11 years@RobertFricke I intended to set async to false so that the function would halt until endTime was set, sorry for so many typos today...
-
Robert Fricke over 11 yearsNo need to apologize to me =)