如何分析ASP.NET MVC应用程序中的请求性能?

我想捕获ASP.NET MVC应用程序中的请求的命中时间,处理时间,内存消耗和响应时间。

有没有办法或工具来执行此操作?

检查由stackoverflow团队开发的miniprofiler

http://code.google.com/p/mvc-mini-profiler/

这有助于您进行一些分析。 有一个nuget pacakge可用于将其添加到您的项目中。

斯科特写了一篇关于如何使用它的post 。

你也可以看看Glimpse 。

有一些商业产品可以进行内存和性能分析,例如telerik just trace 。 您可以下载他们的试用版并使用它

您可以创建自己的小型性能测试监视器。 这是Steven Sanderson的书,Pro Asp.Net MVC 2框架的第670页:

public class PerformanceMonitorModule : IHttpModule { public void Dispose() { /* Nothing to do */ } public void Init(HttpApplication context) { context.PreRequestHandlerExecute += delegate(object sender, EventArgs e) { HttpContext requestContext = ((HttpApplication)sender).Context; Stopwatch timer = new Stopwatch(); requestContext.Items["Timer"] = timer; timer.Start(); }; context.PostRequestHandlerExecute += delegate(object sender, EventArgs e) { HttpContext requestContext = ((HttpApplication)sender).Context; Stopwatch timer = (Stopwatch)requestContext.Items["Timer"]; timer.Stop(); if (requestContext.Response.ContentType == "text/html") { double seconds = (double)timer.ElapsedTicks / Stopwatch.Frequency; string result = string.Format("{0:F4} sec ({1:F0} req/sec)", seconds, 1 / seconds); requestContext.Response.Write("
Time taken: " + result); } }; } }

然后添加到您的web.config:

  

不是免费但非常好:

http://www.jetbrains.com/profiler/

dotTrace是.NET应用程序的性能和内存分析器系列。

我们的最新版本dotTrace 5.2性能可帮助.NET开发人员快速找到性能瓶颈并优化其应用程序。