Reputation: 1107
What is the best way to measure the code run time in ASP.NET page?
Here is my code, trying to time the page load and writing to log.
private Stopwatch PageTimer = null;
protected void Page_Init(Object Src, EventArgs E)
{
if (!IsPostBack)
{
PageTimer = new Stopwatch();
PageTimer.Start();
}
}
protected override void OnPreRender(EventArgs e)
{
if (!IsPostBack)
{
PageTimer.Stop();
Logger.SectionEnd("PageTimer", PageTimer, "", true);
}
base.OnPreRender(e);
}
Upvotes: 2
Views: 4760
Reputation: 1452
If you exclude logging, you can use firebug to measure client-side performance.
Upvotes: 0
Reputation: 8081
I'd recommend you use a HttpModule for that. There's one you can use at
HttpModule For Timing Requests (pretty old, I know, but still valid)
A good thing about HttpModules is that you have them self-register themselves if the "main application" has code for it. Using a module in another application is then just a copy paste in to the bin folder and it will start to operate.
If the "main application" doesn't support modules to self-register it can be added to the bin and web.config to start operating.
Upvotes: 3
Reputation: 3315
If you'd like something simple and fast i suggest using the global.asax file.
You can create this file by selecting new file in a website.
The global.asax file has a few methods, the ones you should be looking for are
Application_BeginRequest
Application_EndRequest
You could start a timer, write some logging , do whatever to get the time between these 2 requests.
By doing it like this you have the exact time the request spent on your server, if you do it in the page_load and page_prerender you miss out on stuff like redirection, authentication, membership etc
Upvotes: 2