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

eka*_*eka 8 c# asp.net-mvc performance iis-7 httpmodule

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

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

Shy*_*yju 11

检查由stackoverflow团队开发的miniprofiler

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

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

斯科特已经写了一个帖子关于如何使用它.

你也可以看看Glimpse.

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


小智 6

您可以创建自己的小型性能测试监视器.这是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("<hr/>Time taken: " + result);
            }
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

然后添加到您的web.config:

<add name="PerfModule" type="Namespace.PerformanceMonitorModule, AssemblyName"/>
Run Code Online (Sandbox Code Playgroud)