Optimizing ASP. NET application performance [2]

Source: Internet
Author: User
Tags add numbers website performance

For ASP. NETProgramDevelopers are very important. Although a good website has beautiful page design and perfect service functions, there is a long delay in opening the webpage, and users will end up unable to bear it. Especially for large e-commerce websites, tens of thousands of users access the website at the same time every second. Without good website performance, the website cannot meet huge demands.

ASP. NET, as a new generation of dynamic web page generation system, has essentially improved the platform performance compared with the original ASP. However, we need to develop professional, production-compliant, and user-friendly Web applications on this basis, developers also need to optimize pages, data access, string processing, and other aspects from the programming perspective to improve the overall performance of the website.

This article will mainly discuss several performance optimization methods and precautions related to ASP. NET.

Page Performance Optimization

1. select an appropriate session Status

HTTP is a stateless communication protocol that cannot record and identify requests from different clients, however, in actual applications, the system maintains session status information between different requests from the client. ASP. NET solves this problem by storing session status information in processes, status servers, or SQL Server databases.

Saving session status information in the memory of the web server has the best performance and is fast, but it lacks the ability to span multiple servers. To maintain session information between multiple Web servers, you can use the status server for storage. This method improves system scalability and reliability by deploying applications on multiple servers, however, to reduce performance costs. For extremely important session information, you need to use the SQL server storage method to avoid loss of important session information, but the resulting workload is much larger than the previous two.

If you do not consider the retention of status information and the sharing of multiple servers, you should try to save it in the process of the server to get the best performance.

You can use the web. config file to store session status information:

<Sessionstate
Mode = "inproc/StateServer/sqlserver" // select the storage mode
Stateconnectionstring = "TCPIP = 127.0.0.1: 42424"
......
Timeout = "20"/>

2. Server Control Optimization

2.1 reduce unnecessary server controls

The convenience and functions of server controls are incomparable to those of HTML controls. However, each server control needs to create corresponding objects on the server, at the cost of sacrificing server resources. Excessive use of server controls will greatly affect program performance.

In many cases, you can simply use HTML tags or data binding to implement the required functions. For example, the <asp: Label> control can be used to display static information. If the HTML control does not provide the functions to be implemented and the script language, such as JavaScript and VBScript, cannot be implemented, the server control should be selected.

2.2 disable unnecessary status views

The Status view attribute of the server control can automatically maintain the status of the server control during the page round-trip process, reducing developers' workload, but occupying a large amount of server memory resources. Therefore, when the server control status view is not required, set its enableviewstate attribute to false, such as the commonly used <asp: lable> and <asp: button> controls.

2.3 use of page. ispostback

Page. ispostback is used to record whether a page is returned from the client. If it is set to false, it indicates that the page is run for the first time. Otherwise, it indicates that the page is returned from the client again. Reasonable Application of page. ispostback can avoid unnecessary operations during the round-trip process. This attribute can be used in the page_load function and some event functions that only need to be initialized once to improve application performance.

Void page_load (Object o, eventargs E)
{
If (! Page. ispostback)
{
Conn = new sqlconnection ("Server = localhost; uid = sa; Pwd =; database = Data ");
String SQL = "select * from student ";
Cmd. Fill (DS, "Stu ");
Mydatagrid. databind ();
}
}

AboveCodeThe database will be read and bound only when the page is accessed for the first time.

2.4 proper use of the DataGrid Control

The DataGrid Control has the most powerful data display function and has many built-in functions such as data modification, deletion, addition, and paging. If you only need to simply display the data, the DataGrid is not the best choice. The paging function and data storage method of the DataGrid Control (stored in viewstate) make it easy and convenient for program developers to use, but the resulting performance overhead is not negligible.

The datalist control has much fewer functions than the DataGrid Control. However, the customization is much stronger. The unique multi-row data display is more convenient. The functions that the DataGrid can implement.

The Repeater control has the least functions, but is very user-defined. Due to the reduction of many functions, the performance of the server is minimized.

Therefore, when you simply need to display the data list, you can select the repeater or datalist control to achieve the same purpose and reduce the performance overhead.

Database Access Performance Optimization

1. database connection and Shutdown

To access database resources, you need to create a connection, open a connection, and close the connection. These processes need to exchange information with the database multiple times for authentication, which consumes server resources. ASP. NET provides a connection pool to improve the impact of enabling and disabling databases on performance. The system places the user's database connection in the connection pool. If necessary, the connection is taken out. When the connection is closed, the connection is withdrawn and the next connection request is waited.

The size of the connection pool is limited. If you still need to create a connection after the connection pool reaches the maximum limit, the performance will be greatly affected. Therefore, after a database connection is established, the connection is enabled only when operations are required. The connection is closed immediately after use, so as to minimize the time for opening the database connection and avoid exceeding the connection limit.

2. Use stored procedures

Stored procedures are a set of pre-compiled SQL statements stored on the server, similar to the batch processing files in the DOS system. Stored Procedures provide the ability to access databases immediately and process information quickly. Using the stored procedure can avoid multiple compilation of commands. After one execution, the execution plan will reside in the cache. In the future, you only need to directly call the binary code in the cache.

In addition, the stored procedure runs on the server and is independent from the ASP. Net program to facilitate modification. The most important thing is that it can reduce the transmission of database operation statements over the network.

3. Optimize Query statements

In ASP. NET, the ADO connection consumes a considerable amount of resources. The longer the SQL statement runs, the longer the system resources are occupied. Therefore, try to use optimized SQL statements to reduce execution time. For example, a query statement that does not contain a subquery statement makes full use of indexes.

String operation performance optimization

1. Use the tostring method of Value Type

When connecting strings, you often use the "+" sign to directly add numbers to strings. This method is simple and can get the correct results. However, because different data types are involved, the numbers must be converted to the reference type through the boxing operation before they can be added to the string. However, the packing operation has a great impact on the performance, because during such processing, a new object will be allocated in the managed heap, and the original value will be copied to the newly created object.

The Value Type tostring method can be used to avoid packing and improve application performance.

2. Use the stringbuilder class

The string class object cannot be changed. In essence, the re-assignment of the string object re-creates a String object and assigns the new value to the object, the tostring method does not significantly improve the performance.

When processing strings, it is best to use the stringbuilder class. Its. Net namespace is system. Text. This class does not create a new object, but directly performs operations on strings through append, remove, insert, and other methods, and returns the operation results through the tostring method.

Its definition and operation statement are as follows:

Int num;
System. Text. stringbuilder STR = new system. Text. stringbuilder (); // create a string
Str. append (Num. tostring (); // Add the numeric value num
Response. Write (Str. tostring); // display the operation result

ASP. NET application performance test

Before performing a performance test on ASP. NET applications, ensure that the application is correctly functioning and has no errors. The following tools can be used for specific performance tests:
Web application strees tool (was) is a free test tool released by Microsoft. It can be downloaded from http://webtool.rte.microsoft.com. It can simulate hundreds of thousands of users simultaneously to access web applications, form a traffic load on the server, so as to achieve the purpose of testing, can generate average ttfb, average TTLB and other performance summary reports.

Application Center Test (ACT) is a testing tool that is attached to the Enterprise Edition of Visual Studio. NET and is a Web application testing tool officially supported by Microsoft. It can intuitively generate chart results with more functions than was, but does not have the ability to be tested simultaneously by multiple clients.

The "performance" counter in the "Administrative Tools" of the server operating system can monitor the server to understand the application performance.

Conclusion

For website developers, compile ASP. net Applications, develop good habits, improve application performance, at least can delay the necessary hardware upgrade, reduce the cost of the website.

Related Article

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.