开发者

How can I most accurately calculate the execution time of an ASP.NET page while also displaying it on the page

开发者 https://www.devze.com 2022-12-23 16:59 出处:网络
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

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 cyc开发者_开发问答le.

What is the best way to do this?


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."; 
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消