Asp.net MVC and $ .ajax Added Overhead

I ran into a very interesting low performance issue when asp.net MVC control is invoked by the jquery $ .ajax function. The control takes a database action that takes 403 ms, but the total $ .ajax call is 3,400 ms according to Firebug, which is quite a lot of extra overhead. I need to optimize performance, but I don’t understand where this overhead comes from.

Here is the code. In my controller I have

public JsonResult SetSearchResults(Criteria searchCriteria) { SearchResult myReportsResult = _repository.GetResults(searchCriteria); //the statement above takes 403 ms return Json(myReportsResult); } public SearchResult GetResults(SearchCriteria searchCriteria) { SearchResult result = SearchResult(); DataTable dbResults = _da.GetDBResults(searchCriteria); List<IncidentReportHeader> irs = new List<IncidentReportHeader>(); for (int i = 0; i < dbResults.Rows.Count; i++) { IncidentReportHeader ir = new IncidentReportHeader(); //populate all the properties of the ir object here, irs.Add(ir); } result.Reports = irs; return result; } //models public class SearchResult { private List<IncidentReportHeader> _res; private int _numOfPages=0; private int _recordsPerPage=0; public List<IncidentReportHeader> Reports { get { return _res; } set { _res = value; } } public SearchResult() { _res = new List<IncidentReportHeader>(); } } } //db call public DataTable GetDBResults(SearchCriteria searchCriteria) { //add all params to the db object needed for the stored procedure here DataTable dt = _db.ExecuteStoredProc("myDB.PACKAGE_NAME.stored_proc", 2000, ref _spParams, ref _spResultVariables); return dt; } 

in my js

 function SearchIncidentReports() { //pack the searchCriteria object here var searchCriteria = ... var start = new Date().getTime(); $.ajax({ contentType: 'application/json, charset=utf-8', type: "POST", url: myController/SetSearchResults, data: JSON.stringify({ searchCriteria: searchCriteria }), cache: false, dataType: "json", success: function (response) { var got_data = new Date().getTime(); var diff1 = got_data - start; alert("data loaded in: " + diff1 + " ms"); // do whatever you need with the data here. // diff1 = 3400ms which is what Firebug shows too }, error: function (xhr, ajaxOptions, thrownError) { var result = $.parseJSON(xhr.responseText); alert(result.ErrorMessage); } }); return false; } 

Another note: when the database call is deleted and I manually populate the object, the performance is very high.

It seems that switching from 403 ms to 3400 ms is wrong and clearly has unreasonable overhead. Could you indicate what is being done wrong here? These are pretty bare bones, and I cannot avoid going to the database.

I tried to have Control return an empty set (ActionResult), not JsonResult, but had the same problem.

Is this a problem with asp.net MVC? thanks in advance

CHANGE ADD

I also have an action that returns an excel file and exactly the same operation with the database inside it. The file is returned in 410 ms and does not use the $ .ajax function. It seems that $ .ajax is causing a delay somehow. All I need to do is get data from the database and usually very quickly.

I added the inside of the controller code because someone asked for it, but I will repeat that the inside (yes, the general inside of the Controller call) takes 403 ms. Obviously, the problem is not the server or database call. It seems to me that this is between the client and the server. Any help?

Just in case, someone decides to help me 1) In Firebug, the total time spent on POST with Action GetResults is 3.54s. 2) When I go to Net-> All in Firebug, where the breakdown of the request is displayed, I see that most of the time is spent waiting (3.5s).

It seems that the communication time between the server and the client is spent on 3.5 s - 403 ms, but where and why?

+6
source share
1 answer

I found the problem and the problem with calling the database. However, the reason I was initially misled is because it is the part of the code that calculates the time difference.

 DateTime start = DateTime.Now; SearchResult myReportsResult = _repository.GetResults(searchCriteria); DateTime got_it = DateTime.Now; TimeSpan diff = (got_it - start); int diff_ms = diff.Milliseconds; 

This code did not give me the correct value in milliseconds.

+1
source

Source: https://habr.com/ru/post/920979/


All Articles