【问题标题】:How to analyze the performance of requests in ASP.NET MVC application?如何分析 ASP.NET MVC 应用程序中请求的性能?
【发布时间】:2012-10-10 11:37:08
【问题描述】:

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

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

【问题讨论】:

    标签: c# asp.net-mvc performance iis-7 httpmodule


    【解决方案1】:

    检查 miniprofiler,由 * 团队开发

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

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

    Scott 写了一个 post 来介绍如何使用它。

    您也可以查看Glimpse

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

    【讨论】:

    • Scott 在 Glimpse 上也有一个 post,好吧,你对 min profiler vs glimpse 有什么看法,在分析在线购物网站时使用哪个?
    【解决方案2】:

    您可以创建自己的小型性能测试监视器。这是 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"/>
    

    【讨论】:

      【解决方案3】:

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

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

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

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

      【讨论】:

      • 这很好,适合本地使用,但如果您必须在客户网站上检查性能,事情就会变得更加复杂,而 Shyju 谈到的 miniprofiler 在这种情况下非常方便。跨度>
      最近更新 更多