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?