views:

89

answers:

3

I want to calculate the execution time of my ASP.NET pages and display it on the page. Currently I'm calculating the execution time using a System.Diagnostics.Stopwatch and then store the value in a log database.

The stopwatch is started in OnInit and stopped in OnPreRenderComplete. This seems to be working quite fine, and it's giving a similar execution time as the one shown in the page trace.

The problem now is that I'm not able to display the execution time on the page because the stopwatch is stopped too late in the life cycle.

What is the best way to do this?

A: 

Persist the execution time information to the server (database, filesystem, cache) and then use ajax or jquery to pull it in off the server into an area of the page asynchronously.

amelvin
A: 

Here a working solution that redefine Page_Init() and Page_preRender() methods to get the differences in time.

private int startTime; 

protected void Page_Init(object sender, EventArgs e) 
{ 
    startTime = Environment.TickCount; 
}
protected void Page_PreRender(object sender, EventArgs e) 
{ 
    int endTime = Environment.TickCount; 
    double executionTime = (double)(endTime - startTime) / 1000.0;

    lblLabel.Text = "Page Execution time is " + executionTime + " seconds."; 
}
systempuntoout
A: 

I would get the elapsed time from the stopwatch during render time. Most (likely almost all) of the page execution time has elapsed at this point. Go ahead and stop the stop watch where you are now and use that number to log to your DB. I'd bet it won't be much different from what gets displayed on the page.

Matt Wrock
Should be good enough :)
henningst