c# - 测量 ASP.NET MVC 3 的性能

标签 c# asp.net-mvc-3 diagnostics

我在 ASP.NET MVC 3 中构建了一个 JSON 服务,我希望能够测量应用程序中操作的执行时间(我希望它自动记录缓慢的操作)。

因此这看起来很棒; http://coderjournal.com/2010/10/timing-the-execution-time-of-your-mvc-actions/ (在堆栈溢出的地方也提到过)

问题是我从这种方法中得到的测量值肯定是错误的; 我添加了另一个秒表,它会在 Action 的第一件事开始并在返回之前停止。

例子:

  • 方法里面的秒表=> 10ms(这里省略了序列化成json的过程,所以可以理解比实际要短)
  • 秒表属性(上面的代码)=> 676ms
  • Firefox 说请求耗时 => 70ms

我相信 firefox 在这里有正确的时间(但它包括下载所以它有点大),但我想了解为什么属性代码不起作用,有什么想法吗?

最佳答案

这可能不是它显示执行时间很长的原因,但当您一次有多个请求时,该属性将无法与 mvc 3 一起正常工作。

In previous versions of ASP.NET MVC, action filters are create per request except in a few cases. This behavior was never a guaranteed behavior but merely an implementation detail and the contract for filters was to consider them stateless. In ASP.NET MVC 3, filters are cached more aggressively. Therefore, any custom action filters which improperly store instance state might be broken.

我建议在 OnActionExecutingsave it to HttpContext.Current.Items 中实例化新秒表- 然后您可以在 OnActionExecuted 中检索它并打印出结果。

关于c# - 测量 ASP.NET MVC 3 的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5821411/

相关文章:

c# - ASP.net MVC dateFormat 验证问题,客户端验证

c# - 用于 Mediawiki 标记的开源解析器代码

c# - 如何在 linq DateTime 中正确设置变量?

asp.net-mvc - 有什么方法可以避免 iisexpress 每次构建时加载 DLL 时的启动延迟?

kotlin - 如何让编译器说出它认为表达式是什么类型?

Azure:如何创建 WADLogsTable 来捕获诊断代码?

c# - 如何发送正文中包含字符串数组的 post 请求

asp.net-mvc-3 - 在 Entity Framework 中使用数据库优先方法

asp.net-mvc-3 - Azure云服务部署错误: "The instance of SQL Server you attempted to connect to does not support encryption. "

c# - 如何使用 Microsoft.CodeAnalysis.Diagnostic 获取错误行