ASP.NET: How to find page load time?

One of the important factors that measures a website's performance is the page loading time. Therefore, it is very essential to keep...


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!!



Subscribe to GET LATEST ARTICLES!


Related

ASP.NET 2020314972465072683

Post a Comment

  1. I've encountered a couple problems with this approach.

    1. The output is written as the very last thing on the page. In my case, it appears after the closing HTML tag. The output is still displayed by the browser, so it still functions as intended here.
    2. The output is written to documents that are not .aspx, such as .js and .css files. This can cause errors when browsers parse those files.

    Overall, this seems to be a quick and easy way to calculate page load times on every page in an application, but it probably wouldn't work well in a production environment.

    ReplyDelete
  2. How To Beat the 사설토토사이트 스포츠토토커뮤니티 안전한놀이터주소 - EPIC HOW TO

    ReplyDelete
  3. Very Simple 토토사이트 Win Strategy (Symmetry Strategy)

    ReplyDelete

emo-but-icon

SUBSCRIBE


item