gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:53:00 24 4
gpt4 key购买 nike

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

这是代码。在我的 Controller 中,我有

 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;
}

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

看起来从 403 毫秒到 3400 毫秒是完全错误的,并且显然具有不合理的开销。您能指出这里做错了什么吗?这是非常简单的,我真的无法避免访问数据库。

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

这是 ASP.NET MVC 问题吗?

更新

我还有一个返回 Excel 文件的操作以及其中完全相同的数据库操作。该文件在 410 毫秒内返回,并且未使用 $.ajax 函数。看来 $.ajax 不知何故导致了延迟。我所需要的只是从数据库中获取数据,通常速度非常快。

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

注释:

  1. 在 Firebug 中,使用 Action GetResults 进行 POST 所需的总时间为 3.54 秒。
  2. 当我导航到 Firebug 中的“网络”->“全部”时,其中列出了请求的详细信息,我发现等待时间最长(3.5 秒)。

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

最佳答案

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

DateTime start = DateTime.Now;

SearchResult myReportsResult = _repository.GetResults(searchCriteria);


DateTime got_it = DateTime.Now;
TimeSpan diff = (got_it - start);
int diff_ms = diff.Milliseconds;

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

关于asp.net-mvc-3 - asp.net MVC 和 $.ajax 增加了性能开销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11583679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com