Introduction to Asp. Net Program Performance

Source: Internet
Author: User

Directory

1. Page and Server Control Processing

2. Status Management

3. Data Access

4. Web Applications

5. Optimization skills and key points

I. Page and Server Control Processing

Avoid excessive round-trip with the server

Use Page. IsPostback to avoid additional work on the round-trip

Exercise caution when using server controls

Avoid excessive Server Control view statuses

Ex:

<Asp: datagrid EnableViewState = "false" datasource = "" runat = "server"/>

<% @ Page EnableViewState = "false" %>

 

Ii. Status Management

Disable session status when not in use

Ü not all applications or pages require the session Status of a specific user. You should disable the session status when you do not need it.

Ex:<% @ Page EnableSessionState = "false" %>

Ü if the Page needs to access session variables without creating or modifying them, set the EnableSessionState attribute in the @ Page command to ReadOnly.

Ü disable the session Status of the XML Web services Method

Ex:[WebMethod (false)]

[WebMethod (EnableSession = false)]

Ü to disable the session Status of an application, set the Mode attribute to Off in the SessionState section of the Web. config file of the application.

Ex:<SessionState mode = "off"/>

 

Iii. Status Management

Select the session Status provider with caution

Ü in-process session Status

The fastest solution to date

If only a small amount of data is easy to lose in the session state, use the in-process provider

Ex:<SessionState mode = "inProc"/>

Ü non-process session status as a Windows Service

Ex:

<SessionState mode = "StateServer" stateConnectionString = "tcpip = localhost: 42424" />

Ü non-process session Status in the SQL database

Ø used to scale applications across multiple processors or computers

Ø used for Web garden and Web farm solutions, or for cases where data cannot be lost during server/process restart

Ex:<SessionState mode = "SQLServer" allowCustomSqlDatebase = "false"

SqlConnectionString = "data source = ben \ sql2005; uid = sa"/>

Aspnet_regsql.exe-S ben \ sql2005-U sa-ssadd-sstype p

 

Iv. Data Access

Use SQL stored procedures for data access

Ü among all the data access methods provided by the. NET Framework, SQL-based data access is the best choice for generating scalable Web applications with the best performance.

Ü when using a hosted SQL provider, you can use compiled stored procedures instead of special queries for additional performance improvements

Use SqlDataReader to obtain the forward read-only data cursor

Ü SqlDataReader object provides forward read-only cursor for data retrieved from the SQL database

Ü when read-only streams are used in ASP. NET applications, the SqlDataReader class provides higher performance than the Dataset class.

Ü SqlDataReader class reads data directly from the database connection using the local network data transmission format of SQL Server

Ü SqlDataReader class implements the IEnumerable interface, which also allows you to bind data to server controls

Cache data and page output as much as possible

Ü cache the output or data of these pages when dynamic computing page output or data is not required for each page request

Ü the performance of these pages can be optimized by designing pages and data requests to be cached (especially in areas where the site is expected to have a large amount of communication)

Note:

Do not cache too many items

The period of validity allocated to cache items should not be too short.

 

V. Web Applications

Large Web application, please pre-compile it

Ü reduce the analysis and compilation overhead for the first page access

Adjust the number of threads for each auxiliary process of the application if necessary

Ü this structure determines the number of requests that can be executed simultaneously based on the CPU power available for the request. This technology is called thread-control.

Ü use the "Pipeline Instance Count" (Pipeline Instance Count) performance counter associated with the "ASP. NET Applications" performance object to Monitor thread-control in Windows Performance Monitor

Ü manually set the number of threads in the process by changing the MaxWorkerThreads and MaxIOThreads attributes in the processModel element (ASP. NET setting architecture) section of the Machine. config configuration file

U. The auxiliary thread is used to process ASP. NET requests, while the IO thread is used to provide services for data from files, databases, or XML Web services.

For applications that rely heavily on external resources, consider enabling network gardening on a multi-processor computer.

Disable debug mode

Ex:<Compilation debug = "true">

Optimize configuration files for Web servers and specific applications

Ü only enable authentication for the desired application

By default, the authentication mode of ASP. NET applications is Windows or integrated NTLM.

In most cases, it is recommended that only applications requiring authentication be in the Machine. config file

Disable authentication and enable authentication in the Web. config file

Ex:<Authentication mode = "Forms">

<Forms name = "SDP" loginUrl = "login. aspx" protection = "All" path = "/">

</Authentication>

Ü configure the application according to the appropriate request and response encoding settings

The default encoding format of ASP. NET is UTF-8.

Ø if your application only uses ASCII characters, configure your ASCII application for a slight Performance Improvement

Ex:<Globalization requestEncoding = "utf -8" ResponseEncoding = "utf -8" />

Optimize configuration files for Web servers and specific applications

Ü disable AutoEventWireup for Applications

Rewrite the OnLoad method of the Page object instead of the Page_Load method.

Ex:<Pages autoEventWireupautoEventWireup = "false"/>

Ü Remove unused modules from the request processing pipeline


6. Optimization skills and key points

Do not rely on exceptions in the code

Ü exceptions are a waste of resources and should be avoided in the Code as much as possible. Never use exceptions as a way to control regular program streams

Ü check the null value, assign it to the string that is analyzed as the numeric value, and check the specific value before applying the mathematical operation.

// This is not recommended.

Try {

Result = 100/num;

}

Catch (Exception e ){

Result = 0 ;;

}

// This is preferred.

If (num! = 0 ){

Result = 100/num;

}

Else {

Result = 0;

}

 

Use Response. Write for string connection

Ü use the HttpResponse. Write Method for string connection on the page or user control

Response. Write ("")

Response. Write (myString)

Response. Write ("B ")

Response. Write (myObj. ToString ())

Response. Write ("c ")

Response. Write (myString2)

Response. Write ("d ")

 

 

Ü do not use + when connecting a large number of strings. StringBuilder should be used to construct

String str

Write strWrite;

StrWrite = "ABC ";

StrWrite + = strTest;

StrWrite + = ......;

StringBuilder sb = new StringBuilder ();

Sb. Append ("ABC ");

Sb. Append (strTest)

 

 

1. Use the Garbage Collector and Automatic Memory Management of the Common Language Runtime Library as appropriate

2. Do not use too much memory for each request (such as storing large objects or datasets in the memory), because the zookeeper recycler will have to do more work more frequently.

3. When you no longer need objects, do not keep unnecessary object references in the code, because the Garbage Collector will not be able to release resources when they are still referenced.

4. Avoid using objects containing the Finalize method as much as possible, because they will cause more garbage collectors to work later.

5. Port a large number of called COM components into managed code

6. One of the major performance barriers is to mail data from an unmanaged environment to a hosted environment.

7. In interactive operations, execute as many tasks as possible at any end, and then perform a single call instead of a series of small calls.

8. For example, all strings in the Common Language Runtime Library are Unicode, so all strings in the component should be converted to Unicode format before the hosted code is called.

 

These are just for reference. For more information about Performance, see Improving ASP. NET Performance.

 

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.