A common feature that gets requested on a lot of big web applications is to show the time it took to load the page. Sometimes it’s just to show to the user how long a certain action took, other times it’s to have a ballpark number when performance issues arise.
First things first, how can the load time of a page be measured? There are different ways of measuring. You can use DateTimes, you can use StopWatch and they all have advantages and disadvantages. For this post DateTimes will suffice, we’re looking for a ballpark number.
I was surprised how difficult it was to implement this in ASP.Net Web Forms. For a simple one page project the solution is quite straight forward. You take the page where you want to show the load time and when the Page_Load has finished you subtract the DateTime that was initialized at the beginning of the method from the DateTime that was initialized at the end. The code hopefully speaks for itself:
protected void Page_Load(object sender, EventArgs e)
var start = DateTime.UtcNow;
var loadTime = DateTime.UtcNow - start;
loadTimeLiteral.Text = (loadTime.TotalMilliseconds/1000).ToString(CultureInfo.InvariantCulture);