Several methods for obtaining the page execution time (asp.net ))

Source: Internet
Author: User
When programming, you often want to test the execution time of your own page, especially during the Asp.net test,

To solve this problem, I will summarize the method for loading the execution time on the page for your learning!

 

1. obtain from the server

1. Calculate the time in the global. asax File

Datetime DT;
Protected void application_beginrequest (Object sender, eventargs E)
{
Dt = datetime. now;
}

Protected void application_endrequest (Object sender, eventargs E)
{

Datetime dt2 = datetime. now;
Timespan Ts = dt2-dT;
Response. Write ("page loading time" + ts. TotalMilliseconds. ToString () + "millisecond ");
}
2. Add the following method to Aspx. CS:

Protected DateTime dt;
Protected void Page_Load (object sender, EventArgs e)
{

}
Protected override void OnPreInit (EventArgs e)
{
Dt = DateTime. Now;
Base. OnPreInit (e );
}

Protected override void onloadcomplete (eventargs E)
{
Base. onloadcomplete (E );
Timespan Ts = datetime. Now-dT;
Response. Write ("when loading the page:" + ts. totalmilliseconds );
}
Ii. Loading time from the client

The procedure is as follows:

1. Add the initial time in the head part of the page
<Script language = JavaScript>
VaR T1 = new date (). gettime ();
</SCRIPT>
2. Add the following content to the body:

<Script language = "JavaScript">
Window. onload = function ()
{
Document. getelementbyid ("timeshow"). innerhtml = "loading this page time" + (new date (). gettime ()-t1) + "millisecond ";
}
</SCRIPT>
3. Insert a tag with the ID of timeshow to be displayed.

<Div id = "timeshow"> </div>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.