Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measuring server response time (client-side) [duplicate]

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

like image 900
Kraken Avatar asked Feb 13 '13 08:02

Kraken


2 Answers

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;

    }
});
like image 81
Robert Fricke Avatar answered Oct 09 '22 13:10

Robert Fricke


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.

like image 40
AmericanUmlaut Avatar answered Oct 09 '22 14:10

AmericanUmlaut