asp.net MVC和$ .ajax增加了性能开销

sar*_*ake 6 jquery asp.net-mvc-3

当jquery $ .ajax函数调用asp.net MVC控件时,我偶然发现了一个非常奇怪的性能不佳问题.该控件执行的数据库操作需要403毫秒,但根据Firebug,总的$ .ajax调用是3400毫秒,这是相当多的额外开销.我需要优化性能,但我不清楚这种开销来自何处.

这是代码.在我的控制器中,我有

 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;

}
Run Code Online (Sandbox Code Playgroud)

在我的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;
}
Run Code Online (Sandbox Code Playgroud)

另一个注意事项,当删除数据库调用并手动填充对象时,性能非常快.

似乎从403毫秒到3400毫秒是完全错误的,显然有不合理的开销.你能指出这里做错了什么吗?这是非常简单的骨头,我不能真正避免去数据库.

我尝试让Control返回空集(ActionResult)而不是JsonResult,但它有同样的问题.

这是asp.net MVC问题吗?提前致谢

编辑添加

我还有一个动作,它返回一个excel文件,并在其中返回完全相同的数据库操作.该文件在410ms内返回,而不是使用$ .ajax函数.似乎$ .ajax以某种方式导致延迟.我只需要从数据库中获取数据,通常它的速度非常快.

我添加了控制器代码的内部,因为有人要求它,但我会重复内部(是控制器调用内部的总数)需要403毫秒.显然,问题不在于服务器或数据库调用.在我看来,它是在客户端和服务器之间.有帮助吗?

以防万一有人决定帮助我1)在Firebug中,使用Action GetResults进行POST所花费的总时间是3.54秒.2)当我在Firebug中导航到Net-> All时,列出了请求的细分,我看到最长的时间花在等待(3.5s).

在服务器和客户端之间进行通信时,似乎花费了3.5s-403ms的时间,但是在哪里以及为什么?

sar*_*ake 1

我发现了这个问题,问题出在数据库调用上。然而,我一开始被误导的原因是那段计算时间差的代码。

DateTime start = DateTime.Now;

SearchResult myReportsResult = _repository.GetResults(searchCriteria);  


DateTime got_it = DateTime.Now; 
TimeSpan diff = (got_it - start);
int diff_ms = diff.Milliseconds;
Run Code Online (Sandbox Code Playgroud)

这段代码没有给我正确的毫秒值。

  • 使用 System.Diagnostics.Stopwatch 来计算时间使用量会更容易。 (2认同)