【发布时间】:2012-10-10 11:37:08
【问题描述】:
我想捕获 ASP.NET MVC 应用程序中请求的命中时间、处理时间、内存消耗和响应时间。
有什么方法或工具可以执行此操作吗?
【问题讨论】:
标签: c# asp.net-mvc performance iis-7 httpmodule
我想捕获 ASP.NET MVC 应用程序中请求的命中时间、处理时间、内存消耗和响应时间。
有什么方法或工具可以执行此操作吗?
【问题讨论】:
标签: c# asp.net-mvc performance iis-7 httpmodule
检查 miniprofiler,由 * 团队开发
http://code.google.com/p/mvc-mini-profiler/
这可以帮助您进行一些分析。有一个可用的 nuget 包,您可以使用它来将其添加到您的项目中。
Scott 写了一个 post 来介绍如何使用它。
您也可以查看Glimpse。
有一些商业产品可以进行内存和性能分析,例如telerik just trace。您可以下载他们的试用版并使用它
【讨论】:
您可以创建自己的小型性能测试监视器。这是 Steven Sanderson 的书 Pro Asp.Net MVC 2 Framework 的第 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("<hr/>Time taken: " + result);
}
};
}
}
然后添加到您的 web.config:
<add name="PerfModule" type="Namespace.PerformanceMonitorModule, AssemblyName"/>
【讨论】:
不是免费的,但真的很好:
http://www.jetbrains.com/profiler/
dotTrace 是用于 .NET 应用程序的一系列性能和内存分析器。
我们的最新版本 dotTrace 5.2 Performance 可帮助 .NET 开发人员快速找到性能瓶颈并优化他们的应用程序。
【讨论】: