Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page Render Time in MVC

Q: How do I calculate the total time it takes to render an MVC page and display the time on the master page.

In Asp.net Web Form I created a Base page class like so:

public class PageBase : System.Web.UI.Page
{      
    private DateTime startTime = DateTime.Now;
    private TimeSpan renderTime;   
    public DateTime StartTime
    {
        set { startTime = value; }
        get { return startTime; }
    }  
    public virtual string PageRenderTime
    {
        get
        {
            renderTime = DateTime.Now - startTime;
            return renderTime.Seconds + "." + renderTime.Milliseconds + " seconds";
        }
    }       
}

I would then call the method on my Master Page like so:

<div id="performance">
     <% =PageRenderTime %>
</div>

Q: How do I accomplish the same thing with the MVC Framework?

Q: With the MVC framework where do I set the start time when a page is first created?

like image 799
TonyAbell Avatar asked Apr 03 '09 01:04

TonyAbell


People also ask

Does MVC use razor pages?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

What is paging in MVC?

PagedList. mvc is a package for paging and sorting for ASP.NET MVC. PagedList package installs a PagedList collection type and extension methods for IQueryable and IEnumerable collections. Table Data. Open a New Project.


2 Answers

The best way to do this is calculate the request timing .. Begin Request and End Request .... Check out this great post:

http://haacked.com/archive/2008/07/02/httpmodule-for-timing-requests.aspx

like image 54
Jalal El-Shaer Avatar answered Oct 19 '22 20:10

Jalal El-Shaer


Go into your web.config and make sure you have...

<system.web>
    <trace enabled="true" localOnly="false" />
</system.web>

Then you can goto http://.../trace.axd and see every request made.

Then you want to look under the From First(s) column and the very last one is the time it took to render the page (server side).

Example...

aspx.page   End Render  0.06121112  0.005297

61.2 ms to render the page.

If you're looking to time code itself or you want to manually do some diagnostics you want to use the System.Diagnostics.Stopwatch class not DateTime.

Stopwatch sw = Stopwatch.StartNew();
...
sw.Stop();
Trace.Write(sw.ElapsedMilliseconds);
like image 22
Chad Moran Avatar answered Oct 19 '22 21:10

Chad Moran