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 srcEventArgs e)
{
    Context.Items["loadstarttime"] = DateTime.Now;
}

public void Application_EndRequest(object srcEventArgs 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.

0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (49) CD (1) CI (2) CloudComputing (2) Coding (8) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (19) Entity Framework (4) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives