Tuesday, 14 May 2013

How to find page load time: ASP.NET

One of the important factors that measures a website's performance is the page loading time. Therefore, it is very essential to keep an eye on how long it takes for web pages to load on the client browser. In ASP.NET, this can be achieved using the application class, that acts as an entry point for all incoming requests. I am considering a situation where you just need the page load time. If you want to know more detailed diagnostic information about web pages, I recommend you to take a look at the Tracing Option ASP.NET provides, that would help you to know more than just the loading time of the page. Using Application class's begin_request and end_request handlers you can log the time taken for each web page in your web application to load.

To make use of the Application Class, add a new Global.asax page to your ASP.NET web application and add the below code to it. 

    public void Application_BeginRequest(object src, EventArgs e)
        Context.Items["loadstarttime"] = DateTime.Now;

    public void Application_EndRequest(object src, EventArgs e)
        DateTime end = (DateTime)Context.Items["loadstarttime"];
        TimeSpan loadtime = DateTime.Now - end;
        Response.Write("<h3>This page took " + loadtime + "ms to load</h3>");

The above code will execute whenever a page in your web application is requested. BeginRequest event will fire whenever a new request is received and EndRequest will fire when the request is processed. We make use of the Items collection of the Context class to store and retrieve the initial time when the request is received. Now the time taken for the page to load will be displayed in each page of your web application whenever each of it is requested just like the one shown below.
Please leave your comments and queries about this post in the comment sections. Thanks for reading!!


Post a Comment


ADFS (1) ADO .Net (2) Ajax (1) Angular (1) Angular Js (17) Angular2 (28) ASP .Net (14) Authentication (1) Azure (1) Breeze.js (1) C# (50) CD (1) CI (2) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DevOps (4) DI (4) Dotnet (22) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (8) Lamda (3) Linq (11) Mongodb (1) MVC (50) NodeJS (7) RDLC (1) Report (1) SDLC (1) Sql Server (30) SSIS (3) SSO (1) SSRS (2) UI (1) WCF (13) Web Api (11) Web Service (1) XMl (1)