Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ASP.NET MVC: Show Page Render Time

Tags:

c#

asp.net-mvc

I'd like to add an output to all my ASP.NET MVC pages via the master page which shows the render time of the current page. How could I do this?

like image 641
Alex Avatar asked Sep 15 '09 18:09

Alex


1 Answers

This is tricky, because before you can render the page you have to have all your ViewData added to it.

You can get the total time it took to execute the controller action by writing a custom ActionFilterAttribute and overriding OnActionExecuting to start a timer, and OnActionExecuted to get the elapsed time and store it to ViewData. When the ActionResult is executed, it can take that time and render it into your View.

That may be close enough for your needs? Unfortunately, to get the grand total time of both the Action and the Result being executed, you would need to override OnActionExecuting and OnResultExecuted in your Controller, but at that point it's too late to add information to the View, as it's already been rendered. You could log it to a file however, see this link for an example.


Okay, here's an example timer action filter. Add this as a class into your project, and then you can tag any controller method with [ActionTimer], and it will add the elapsed time into a ViewData bucket called "_ElapsedTime", which you can print out on your views.

using System.Web.Mvc; 
using System.Diagnostics;

namespace MvcApplication1  {
    public class ActionTimerAttribute : ActionFilterAttribute
        {
            public ActionTimerAttribute()
            {
                // Make sure this attribute executes after every other one!
                this.Order = int.MaxValue;
            }

            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                var controller = filterContext.Controller;
                if (controller != null)
                {
                    var timer = new Stopwatch();
                    controller.ViewData["_ActionTimer"] = timer;
                    timer.Start();
                }
                base.OnActionExecuting(filterContext);
            }

            public override void OnActionExecuted(ActionExecutedContext filterContext)
            {
                var controller = filterContext.Controller;
                if (controller != null)
                {
                    var timer = (Stopwatch)controller.ViewData["_ActionTimer"];
                    if (timer != null)
                    {
                        timer.Stop();
                        controller.ViewData["_ElapsedTime"] = timer.ElapsedMilliseconds;
                    }
                }
            }
        } }

As I mentioned earlier, this won't include the time it took to actually render the ActionResult. If you take a look at the example I linked though, it shows you how you could do this to a log file, and also gives you an idea of how the four action filter events can be used.

like image 164
womp Avatar answered Sep 21 '22 15:09

womp