8

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

有什么方法或工具可以执行此操作吗?

4

3 回答 3

11

检查由 stackoverflow 团队开发的miniprofiler

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

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

Scott 写了一篇关于如何使用它的文章。

您也可以查看Glimpse

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

于 2012-10-19T19:40:44.337 回答
6

您可以创建自己的小型性能测试监视器。这是 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"/>
于 2012-10-19T22:26:51.077 回答
2

不是免费的,但它真的很好:

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

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

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

于 2012-10-19T19:46:08.797 回答