如何显示在页脚中生成页面所需的持续时间?

时间:2021-01-12 22:15:01

During debug builds I would like to show the duration it took, server side, to generate a page in the pages footer.

在调试版本中,我想显示服务器端在页脚中生成页面所花费的时间。

So for example if a page takes 250ms server side I would like that displayed in the footer, in debug builds. How can I achieve this in an ASP.NET MVC project?

因此,例如,如果页面需要250毫秒服务器端,我希望在页脚中显示,在调试版本中。如何在ASP.NET MVC项目中实现此目的?

2 个解决方案

#1


5  

Add this to the footer in your master page:

将其添加到母版页中的页脚:

Page rendering took <%= DateTime.Now.Subtract( this.ViewContext.HttpContext.Timestamp ).TotalMilliseconds.ToString() %>

You can also wrap this in an extension method:

您还可以将其包装在扩展方法中:

public static class Extensions
{
  public static string RequestDurationinMs( this HtmlHelper helper )
  {
    #if DEBUG
    return DateTime.Now.Subtract( helper.ViewContext.HttpContext.Timestamp ).TotalMilliseconds.ToString();
    #endif
  }
}

Use that like this:

像这样使用:

<%= Html.RequestDurationinMs() %>

You may need to import the namespace of the extensions class: <%@ Import Namespace="Your.Namespace" %>

您可能需要导入扩展类的名称空间:<%@ Import Namespace =“Your.Namespace”%>

#2


1  

The ViewContext.HttpContext.Timestamp thing as suggested by Marnix is clever, I hadn't realised that was there. However, you could also do it as an HttpModule which would work in non MVC application too:

Marnix建议的ViewContext.HttpContext.Timestamp事情很聪明,我没有意识到那里存在。但是,您也可以将它作为HttpModule来实现,它也适用于非MVC应用程序:

using System;
using System.Web;

namespace MyLibrary
{
    public class PerformanceMonitorModule : IHttpModule
    {
        public void Dispose() { }

        public void Init(HttpApplication context)
        {
            context.PreSendRequestContent += delegate(object sender, EventArgs e)
            {
                HttpContext httpContext = ((HttpApplication)sender).Context;
                if (httpContext.Response.ContentType == "text/html")
                {
                    DateTime timestamp = httpContext.Timestamp;

                    double seconds = (double)DateTime.Now.Subtract(timestamp).Ticks / (double)TimeSpan.TicksPerSecond;
                    string result = String.Format("{0:F4} seconds ({1:F0} req/sec)", seconds, 1 / seconds);

                    httpContext.Response.Write("<div style=\"position: fixed; right: 5px; bottom: 5px; font-size: 15px; font-weight: bold;\">Page Execution Time: " + result + "</div>");
                }
            };
        }
    }
}

Then put this into your web.config:

然后把它放到你的web.config中:

<httpModules>
    <!-- Other httpModules (snip) -->

    <add name="PerformanceMonitor" type="MyLibrary.PerformanceMonitorModule, MyLibrary"/>
</httpModules>

This will record the time at the very last moment it gets before sending the HTML content to the browser so that it is measuring as much of the HTTP pipeline as possible. Not sure if sticking ViewContext.HttpContext.Timestamp in the page markup can achieve this?

这将记录在将HTML内容发送到浏览器之前的最后一刻的时间,以便尽可能多地测量HTTP管道。不确定在页面标记中粘贴ViewContext.HttpContext.Timestamp是否可以实现此目的?

Note: this won't produce valid HTML markup because it spits a <div> to the bottom of the page, so only use for development/performance analysis.

注意:这不会产生有效的HTML标记,因为它会将

吐出到页面底部,因此仅用于开发/性能分析。

EDIT: I modified the HttpModule to use HttpContext.Timestamp instead of storing a Stopwatch object in the Request context, as it seems to give more accurate results.

编辑:我修改了HttpModule以使用HttpContext.Timestamp而不是在Request上下文中存储Stopwatch对象,因为它似乎给出了更准确的结果。

#1


5  

Add this to the footer in your master page:

将其添加到母版页中的页脚:

Page rendering took <%= DateTime.Now.Subtract( this.ViewContext.HttpContext.Timestamp ).TotalMilliseconds.ToString() %>

You can also wrap this in an extension method:

您还可以将其包装在扩展方法中:

public static class Extensions
{
  public static string RequestDurationinMs( this HtmlHelper helper )
  {
    #if DEBUG
    return DateTime.Now.Subtract( helper.ViewContext.HttpContext.Timestamp ).TotalMilliseconds.ToString();
    #endif
  }
}

Use that like this:

像这样使用:

<%= Html.RequestDurationinMs() %>

You may need to import the namespace of the extensions class: <%@ Import Namespace="Your.Namespace" %>

您可能需要导入扩展类的名称空间:<%@ Import Namespace =“Your.Namespace”%>

#2


1  

The ViewContext.HttpContext.Timestamp thing as suggested by Marnix is clever, I hadn't realised that was there. However, you could also do it as an HttpModule which would work in non MVC application too:

Marnix建议的ViewContext.HttpContext.Timestamp事情很聪明,我没有意识到那里存在。但是,您也可以将它作为HttpModule来实现,它也适用于非MVC应用程序:

using System;
using System.Web;

namespace MyLibrary
{
    public class PerformanceMonitorModule : IHttpModule
    {
        public void Dispose() { }

        public void Init(HttpApplication context)
        {
            context.PreSendRequestContent += delegate(object sender, EventArgs e)
            {
                HttpContext httpContext = ((HttpApplication)sender).Context;
                if (httpContext.Response.ContentType == "text/html")
                {
                    DateTime timestamp = httpContext.Timestamp;

                    double seconds = (double)DateTime.Now.Subtract(timestamp).Ticks / (double)TimeSpan.TicksPerSecond;
                    string result = String.Format("{0:F4} seconds ({1:F0} req/sec)", seconds, 1 / seconds);

                    httpContext.Response.Write("<div style=\"position: fixed; right: 5px; bottom: 5px; font-size: 15px; font-weight: bold;\">Page Execution Time: " + result + "</div>");
                }
            };
        }
    }
}

Then put this into your web.config:

然后把它放到你的web.config中:

<httpModules>
    <!-- Other httpModules (snip) -->

    <add name="PerformanceMonitor" type="MyLibrary.PerformanceMonitorModule, MyLibrary"/>
</httpModules>

This will record the time at the very last moment it gets before sending the HTML content to the browser so that it is measuring as much of the HTTP pipeline as possible. Not sure if sticking ViewContext.HttpContext.Timestamp in the page markup can achieve this?

这将记录在将HTML内容发送到浏览器之前的最后一刻的时间,以便尽可能多地测量HTTP管道。不确定在页面标记中粘贴ViewContext.HttpContext.Timestamp是否可以实现此目的?

Note: this won't produce valid HTML markup because it spits a <div> to the bottom of the page, so only use for development/performance analysis.

注意:这不会产生有效的HTML标记,因为它会将

吐出到页面底部,因此仅用于开发/性能分析。

EDIT: I modified the HttpModule to use HttpContext.Timestamp instead of storing a Stopwatch object in the Request context, as it seems to give more accurate results.

编辑:我修改了HttpModule以使用HttpContext.Timestamp而不是在Request上下文中存储Stopwatch对象,因为它似乎给出了更准确的结果。