Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure ASP.NET page load time

Tags:

asp.net

I'm currently developing an ASP.NET application which uses a MasterPage and I want to measure my application webform's loading time and display that information to the client.

My current strategy involves using the Application_BeginRequest event associated callback (in the Global.asax file of my website solution), to start the measurement of the time spent on the server-side process (as follows)

protected void Application_BeginRequest(Object sender, EventArgs e) {
   Context.Items.Add("Request_Start_Time", DateTime.Now);
}

and calculate the elapsed time on the webform's OnPreRender event associated callback, printing it on a placeholder element (as follows)

protected override void OnPreRender(EventArgs e) {
   base.OnPreRender(e);

   TimeSpan tsDuration = DateTime.Now.Subtract((DateTime)Context.Items["Request_Start_Time"]);
   ExecutionTime.InnerHtml = "<em>Server-side processing duration: " + tsDuration.TotalMilliseconds + " miliseconds.</em>";
}

is this the best way to measure loading time? Is there a more "elegant" way to accomplish this?

Thanks in advance for your time and cooperation.

like image 482
XpiritO Avatar asked Sep 08 '09 18:09

XpiritO


3 Answers

<%@ Page Trace="true" %> (or set this in web.config)

Enable tracing and check out the information in trace.axd (in the root of your website).

Then you could set timing points with:

Trace.Write("Start time intensive task");
Trace.Write("Stop time intensive task");

http://msdn.microsoft.com/en-us/library/bb386420.aspx

This is assuming "your client" want deep debug data.

like image 139
Erwin Avatar answered Oct 23 '22 15:10

Erwin


Your appproach seems to be exact enough, as long as you do not render very large control trees and you don't use server controls that do all their work in an overridden Render method (unexperienced server control authors tend to do exactly that...).

In that case, there is actually a way to render the actual rendering time ;-) Just use a HttpResponse.Filter object to fill a placeholder with the elapsed time. These filters are applied after the web form is rendered and before it goes to the client.

If you need this just for development, search for trace.axd, this will give you a lot of details not only about timing but also the request context, control tree structure and page size.

like image 5
realMarkusSchmidt Avatar answered Oct 23 '22 16:10

realMarkusSchmidt


This really depends on what metric you are looking to actually show.

  1. Is this an overall "page loaded in __ seconds" type thing that will be there all the time?
  2. Is this to determine if your coded solution meets requirements for a client, and not needed for production?

The answer to these two will dictate what makes the most sense when it comes to recording. If your goal is #1, then I would say your method works. If you are going for number 2, I could be looking at what Erwin has.

like image 1
Mitchel Sellers Avatar answered Oct 23 '22 16:10

Mitchel Sellers