评估ASP.NET MVC 3的性能 [英] Measuring performance of ASP.NET MVC 3

查看:94
本文介绍了评估ASP.NET MVC 3的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在ASP.NET MVC 3中构建了JSON服务,并且希望能够测量应用程序中动作的执行时间(我希望它能够自动记录慢速动作).

I've built a JSON service in ASP.NET MVC 3 and I want to be able to measure the execution time of the actions in my application (I want to it to automatically log slow actions).

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

Therefor this looked great; http://coderjournal.com/2010/10/timing-the-execution-time-of-your-mvc-actions/ (It's been mentioned on places here on stack overflow as well)

问题是我从这种方法中得到的测量结果一定是错误的; 我添加了另一个秒表,该秒表在动作中首先开始,在返回之前停止.

The problem is that I get measurements that MUST be wrong from this method; I've added another stopwatch that starts the first thing in the action and stops just before the return.

示例:

  • 方法中的秒表=> 10毫秒(此处省略了序列化为json的顺序,因此我可以理解它比实际情况要短)
  • Stopwatch属性(上面的代码)=> 676ms
  • Firefox表示请求花了=> 70毫秒.
  • Stopwatch inside the method => 10ms (the serializing to json is omitted here, so I can understand that it's shorter than reality)
  • Stopwatch attribute (code above) => 676ms
  • Firefox says the request took => 70ms .

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

I believe that firefox has the correct time here (but it includes the download so it's a bit large), but I want to understand why the attribute code doesn't work, any ideas for this?

推荐答案

这可能不是执行时间长的原因,但是当您一次有多个请求时,该属性将无法在mvc 3中正常工作.

This might not be the reason why it shows that long execution time, but that attribute won't work correctly with mvc 3 when you have multiple requests at once.

在以前版本的ASP.NET MVC中, 根据请求创建操作过滤器 除了少数情况.这种行为 从来都不是保证的行为,但是 只是实现细节和 过滤器的合同是 认为他们是无国籍的.在ASP.NET中 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.

我建议在OnActionExecuting将其保存到HttpContext.Current.Items -然后您可以在OnActionExecuted中检索它并打印出结果.

I'd recommend to instantiate new stopwatch in OnActionExecuting and save it to HttpContext.Current.Items - then you can retrieve it in OnActionExecuted and print out result.

这篇关于评估ASP.NET MVC 3的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆