Reputation:
How to measure latency of any async ajax call!!
Is there any specific library or api which can be used to extract all these information from the browser
I have been looking for a solution which can run on IE6
Upvotes: 1
Views: 7001
Reputation: 16706
The following code contains an ajax function
https://stackoverflow.com/a/18309057/2450730
And a function to measure the time it takes to get the response.
This could be done with the Date
object, but if you want a precise output i show you a better way. This works only on modern browser.
https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
//Ajax XHR2
function ajax(a,b,c){ // Url, Callback, just a placeholder
c=new XMLHttpRequest;
c.open('GET',a);
c.onload=b;
c.send()
}
// Measuring the time in microseconds it takes to make the request.
var start;
function measure(){
start=window.performance.now();
ajax('URL',function(){
var Delay=window.performance.now()-start;
alert(Delay);
});
}
measure()
The returned value is in milliseconds. alternatively replace both window.performance.now()
with Date.now()
.
By changing the ajax function it's also possible to time the individual processes the ajax call does.This using onreadystatechange
.
If you have some questions, just ask.
Upvotes: 1
Reputation: 13866
You can view this in the browser's console
or alternatively you can check the time if you surround your request function like this for example:
function sendRequest(url) {
var startTime = new Date(); //start time
$.ajax({
//your request here
,done: {
var endTime = new Date(); //end time
console.log("Request took " + (endTime-startTime) + " ms.");
}
}
}
Regarding asynchronous calls, it depends on what you need. If you need the full execution cycle (request building on client's side, sending request, server-side operations, receiving data, handling response), then you can do it the same way but you have to ensure you are measuring the same operation, in other words match the start to the end and make sure they correspond. Although based on the fact that we are talking about asynchronous requests you should measure only the part until the request leaves the client's side. This depends on how your application is implemented.
Upvotes: 0