asp.net MVC和$就额外的性能开销

问题描述:

我曾经穿过的表现不佳很好奇的问题时绊倒asp.net的MVC控制的jQuery $就函数调用。控制执行数据库操作的需要403ms,但总$就调用根据萤火虫,这是相当多的额外开销是3400ms。我需要优化性能,但哪里开销来自我也不大清楚。

I have stumbled across a very curious issue of poor performance when asp.net MVC control is called by jquery $.ajax function. The control performs a database action that takes 403ms, but the total $.ajax call is 3400ms according to Firebug, which is quite a bit of added overhead. I need to optimize the performance but I am not clear where does this overhead comes from.

下面是code。
在我的控制器,我有

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;

}

在我的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 removed and I populate the object manually, the performance is super fast.

似乎从403ms去3400ms是完全错误的,并显然有不合理开销。能否请你指出正在做什么错在这里?这是pretty裸露的骨头,我真的不能避免前往该数据库。

It seems that going from 403ms to 3400ms is plain wrong and clearly has unjustified overhead. Can you please point out what is being done wrong here? It's pretty bare bones and I can't really avoid going to the database.

我试过具有控制返回空集(ActionResult的),而不是JsonResult但它有同样的问题。

I tried having the Control return the empty set (ActionResult) rather than JsonResult but it had the same issue.

这是asp.net MVC的问题?
在此先感谢

Is this asp.net MVC issue? thanks in advance

编辑补充

我也有一个返回里面一个Excel文件并具有完全相同的数据库操作的动作。该文件回来在410ms,而不是使用$阿贾克斯的功能。看来,$就以某种方式导致的延迟。所有我需要的是从数据库中的数据,通常它的速度非常快。

I also have an action that returns an excel file and exactly the same database operation inside it. The file comes back in 410ms and not using $.ajax function. It appears that $.ajax is causing the delay somehow. All I need is to get the data from the database, and normally it's very fast.

我添加了控制器code里面,因为有人问它,但我会重复里面(是控制器的呼叫内的总)需要403毫秒。显然,问题的关键不是在服务器或数据库调用。在我看来,这是客户端和服务器之间。任何帮助吗?

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

以防万一有人决定帮我
1)在萤火虫​​花的总时间到后使用Action GetResults为3.54s。
2)当我浏览到网络>所有在Firebug,其中请求的崩溃列出我看到最大的时间都花在等待(3.5秒)。

Just in case anyone decides to help me 1) In Firebug the total time it took to POST using Action GetResults is 3.54s. 2) When I navigate to Net->All in Firebug, where the breakdown of the request is listed I see that the largest time is spent waiting (3.5s).

看来,3.5秒 - 服务器和客户端,但地点和原因之间通信时,403ms的时间都花在

It appears that 3.5s - 403ms time is spent when communicating between server and client, but where and why?

我发现这个问题,这个问题与数据库的呼叫。不过,我起初误导的原因是计算时间差的那块code的。

I found the issue and the issue IS with the database call. However, the reason I was at first misled is the piece of 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;

这code没有给我正确的毫秒值。

This code did not give me the correct milliseconds value.