Alex Rivera | Logout

Page generation time - ASP.Net MVC

Asked 2009-12-01T14:48:34.200
15

I'm looking for a way to track how long it took for a page to be generated by the server. I know I can use Trace to track this but I need a way to display this per page.

Its ASP.Net MVC 2

Edit
Report

1 Answer

4

It will depend on where you want to include this information. For example you could write an http handler that will display the render time after the </html> tag:

public class RenderTimeModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += (sender, e) =>
        {
            var watch = new Stopwatch();
            var app = (HttpApplication)sender;
            app.Context.Items["Stopwatch"] = watch;
            watch.Start();
        };

        context.EndRequest += (sender, e) =>
        {
            var app = (HttpApplication)sender;
            var watch = (Stopwatch)app.Context.Items["Stopwatch"];
            watch.Stop();
            var ts = watch.Elapsed;
            string elapsedTime = String.Format("{0} ms", ts.TotalMilliseconds);
            app.Context.Response.Write(elapsedTime);
        };
    }

    public void Dispose()
    {
    }
}

If you want to display render time somewhere in the middle of the html page then this render time will not account for the total page render time.

answered 2009-12-01T15:06:43.057

Your Answer