Reputation: 3406
I have this ajax call executing from client side, with jQuery, but at times, (I mean generally at the very first call) it is executing very slow, perhaps this figure will explain..
As you can see, the last call is waiting 609ms before anything, though it took 210ms to receive data, that's bearable, but why the wait of 609ms?
Fiddler's statistics are
Request Count: 1
Bytes Sent: 552 (headers:543; body:9)
Bytes Received: 364 (headers:234; body:130)
ACTUAL PERFORMANCE
--------------
ClientConnected: 15:16:42.799
ClientBeginRequest: 15:16:42.799
GotRequestHeaders: 15:16:42.799
ClientDoneRequest: 15:16:42.799
Determine Gateway: 0ms
DNS Lookup: 0ms
TCP/IP Connect: 0ms
HTTPS Handshake: 0ms
ServerConnected: 15:16:42.799
FiddlerBeginRequest:15:16:42.799
ServerGotRequest: 15:16:43.408
ServerBeginResponse:15:16:43.408
GotResponseHeaders: 15:16:43.408
ServerDoneResponse: 15:16:43.408
ClientBeginResponse:15:16:43.408
ClientDoneResponse: 15:16:43.705
The jQuery is nothing special, just a simple ajax call...
$.ajax({
url: '/AutoComplete.asmx/GetPriorityAndRemarks',
type: 'POST',
timeout: 20000,
datatype: 'xml',
cache: false,
data: 'arg=' + custCode,
success: function (response) {
var result = $(response).find("string").text();
// the values is in form of name, address, mobile, priority and remark, and discount
var resultAry = result.split(':');
//alert(resultAry);
$('#txtCustomerName').val(resultAry[0].trim());
$('#lblAddress').text(resultAry[1]);
$('#lblMobileNo').text(resultAry[2]);
$('#lblPriority').text(resultAry[3]);
$('#lblRemarks').text(resultAry[4]);
$('#txtDiscount').val(resultAry[5]);
$('#txtQty').focus();
$('#txtQty').select();
$('#hdnCustCode').val(custCode);
return false;
},
error: function (response) {
alert('some error occured');
}
});
The code is implemented as
public string GetPriorityAndRemarks(string arg)
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "sp_NewBooking";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@BranchId", Globals.BranchID);
cmd.Parameters.AddWithValue("@CustCode", arg);
cmd.Parameters.AddWithValue("@Flag", 31);
return PrjClass.ExecuteScalar(cmd);
}
catch (Exception)
{
return "";
}
}
So, what's causing it to taking so much time? Also, as a added measure, thinking that calling this code the first time might be slow, I called this code with dummy argument at page load just so that when code gets called with real data, it's not the first time and any thing regarding server being set up, caching or whatever is already done. But still no luck, the call regardless of weather I call this code with dummy data at page load or not, is always slow when making first call. Can anyone explain to me why is this happening this way?
Upvotes: 1
Views: 2850
Reputation: 21086
Two (overlapping) possibilities are:
1) Client -> Server is slow
2) Server -> Database is slow
For (1) if your web server doesn't have enough bandwidth or is being overloaded with requests, it's not going to function efficiently. On the same note, if your client (test) computer is on a very slow connection your performance will be hindered. There's no way to fix this without getting a better (faster) server with more bandwidth. But more testing would be needed to verify this case.
For (2) your stored procedure 'sp_NewBooking' might be running poorly. I would consider putting a non-clustered index on (BranchId, CustCode) for the table it's accessing. This could also be related to the problem explained in (1), if your database server is being bogged down with requests or the connection between your web server and database is poor you could be running into performance issues.
To test these possibilities:
Have your client computer PING the server, or access a static HTML or image file. Check the response time, if it's slow then you have a either a connection issue or your web server is being bogged down with more requests than it can handle efficiently.
Connect to your database server from within the network/domain it's on (e.x. remote desktop into the server, connect with sql management studio from the same LAN). Run your stored procedure manually and check the response time. If the response is slow then you your database server is either bogged down with more requests than it can handle efficiently or your underlying database is not well optimized (in which case try adding the index).
Upvotes: 2
Reputation: 382150
This isn't Firefox waiting before to send the request but Firefox waiting before the server starts pushing data on the socket back.
See here : Firefox is "Waiting For Response - waiting for a response from the server".
The problem is thus only server related (and yes, 609ms to handle a request is terrible). Profile your "asmx" page to see what happens. Maybe you're doing a bad SQL query for your autocomplete or the database is poorly indexed.
Upvotes: 1