Solved

jquery how to measure response time for a server

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

New My Cloud Pro Series - organize everything!

With space to keep virtually everything, the My Cloud Pro Series offers your team the network storage to edit, save and share production files from anywhere with an internet connection. Compatible with both Mac and PC, you're able to protect your content regardless of OS.

Question has a verified solution.

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

Suggested Solutions

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.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

910 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

17 Experts available now in Live!

Get 1:1 Help Now