Alan Jackson
Alan Jackson

Reputation: 6511

How do I show the page generation time in asp.net?

What is the easiest way to monitor how long it is taking to render my asp.net pages (I'm using webforms still if it matters)?

I know that the page lifecycle is pretty involved, but I'm basically just looking for one number that tells me how long my code took to run and to display that at the bottom (or wherever) of the page.

Edit

Several people have mentioned the asp.net trace config, which is awesome and I'm not sure how I missed it. The one thing I am still looking for is how to output just the final render time: ie

Response.Write(Context.Trace.FinalRenderTimeFromFirst)

But I can't figure out how to inspect the contents of the trace element.

Upvotes: 2

Views: 1377

Answers (4)

Tom Pažourek
Tom Pažourek

Reputation: 10167

I usually add this simple code to the bottom of my Layout view:

<!-- Page generated in @((DateTime.UtcNow - HttpContext.Current.Timestamp.ToUniversalTime()).TotalSeconds.ToString("F4")) s -->

I am using ASP.NET MVC with Razor but should be easy to port to Web Forms too.

Outputs something like:

<!-- Page generated in 0.4399 s -->

This starts measuring when the HttpContext is created (according to the documentation), and stops right before writing to output, so it should be pretty accurate.

It might not work for all use cases (if you want to measure child actions or ignore the time outside the MVC action, etc.), but it's easy to paste into a view.

For more advanced diagnostics, I used to use Glimpse.

Upvotes: 1

Adam McKee
Adam McKee

Reputation: 822

I use Fiddler (http://www.fiddler2.com/Fiddler2/version.asp) for such things. It's not specific for ASP.NET, but it will break down your page as it get rendered to the client, and show the amount of time it took to load each piece of your page as it comes in to the browser.

Hope this helps!

Upvotes: 2

Middletone
Middletone

Reputation: 4270

put trace=true in the first line of you .aspx file and use response.trace("") in your code behind to add additonal information to it.

Upvotes: 1

Andrew Hare
Andrew Hare

Reputation: 351476

You want to enable tracing.

From The Basics of .NET Tracing:

[M]odify the trace element in web.config as follows:

<trace enabled="true" .../>

Upvotes: 1

Related Questions