Solved

jquery how to measure response time for a server

Posted on 2013-01-18
3
1,486 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Several part series to implement Internet Explorer 11 Enterprise Mode
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.

830 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