title: Measure ASP.NET page load time
body: <p>I'm currently developing an ASP.NET application which uses a <code>MasterPage</code> and I want to measure my application webform's loading time and display that information to the client.</p>
  
  <p>My current strategy involves using the <code>Application_BeginRequest</code> event associated callback (in the <code>Global.asax</code> file of my website solution), to start the measurement of the time spent on the server-side process (as follows)</p>
  
  <pre><code>protected void Application_BeginRequest(Object sender, EventArgs e) {
     Context.Items.Add("Request_Start_Time", DateTime.Now);
  }
  </code></pre>
  
  <p>and calculate the elapsed time on the webform's <code>OnPreRender</code> event associated callback, printing it on a placeholder  element (as follows)</p>
  
  <pre><code>protected override void OnPreRender(EventArgs e) {
     base.OnPreRender(e);
  
     TimeSpan tsDuration = DateTime.Now.Subtract((DateTime)Context.Items["Request_Start_Time"]);
     ExecutionTime.InnerHtml = "&lt;em&gt;Server-side processing duration: " + tsDuration.TotalMilliseconds + " miliseconds.&lt;/em&gt;";
  }
  </code></pre>
  
  <p>is this the best way to measure loading time? Is there a more "elegant" way to accomplish this?</p>
  
  <p>Thanks in advance for your time and cooperation.</p>