Solved

jquery how to measure response time for a server

Posted on 2013-01-18
3
1,400 Views
Last Modified: 2013-01-21
Could someone help in telling how to calculate the response time of call in jquery. Basically I need to check how long a call took from server side to respond and then capture the timing for each call made.   Some sample code is appreciated.

There is a function which lets say submit a form process it and get the response from server, and I need to capture how long it took to finish the function.  e.g.

signup.AppController.prototype.submitApplication = function(view, submission, fields) {
    view.submitting();
    controller.submission = submission;

    $.ajax({
        type: "POST",
        url: url,
        contentType: 'application/json',
        data: JSON.stringify(submission),

        complete: function(jqXHR, textStatus) {
            var response        = jqXHR.responseText ? JSON.parse(jqXHR.responseText) : {},
                status          = jqXHR.status,
                doneSubmitting  = true;
           
            if (!status) {
                // blah blah
            }

            if (doneSubmitting)
                view.doneSubmitting();
        }
    })
}
0
Comment
Question by:ysgdel
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 38796249
try this


    $.ajax({
var start = new Date().getTime(); //start timer
        type: "POST",
        url: url,
        contentType: 'application/json',
        data: JSON.stringify(submission),



        complete: function(jqXHR, textStatus) {
            var response        = jqXHR.responseText ? JSON.parse(jqXHR.responseText) : {},
                status          = jqXHR.status,
                doneSubmitting  = true;
var elapsed = new Date().getTime() - start; //end timer            
            if (!status) {
                // blah blah
            }

            if (doneSubmitting)
                view.doneSubmitting();
        }
    })
}
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38796280
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 38796334
If you are asking about being able to display on the page, you need to start a timer before by creating a "start" variable with the current time and another "end" by recording the current time when the function is called.   What I gave you should be the timing from submitting the call to receiving.  It would be interesting to do the same serverside where you record the time serverside at the top of your serverside page and then again at the bottom and return the processed time.  Then in your main page you could have something like, "This process took xx milliseconds to process and xx milliseconds to load where the first number is your serverside timing and the 2nd is the js timing.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
The viewer will learn how to dynamically set the form action using jQuery.
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now