Asp.net| Program | performance | Optimization Summary This paper explores how to improve the performance of asp.net applications from page, data access, string manipulation, and provides several test tools for detecting asp.net Web site performance.
Keyword asp.net application performance optimization
The performance of a Web site is very important for asp.net developers. Although a good site has beautiful page design, perfect service function, but open the page has a long delay, the user will eventually unbearable. Especially for large e-commerce sites, there are tens of thousands of users per second access, no good site performance, simply can not meet the huge demand.
Asp. NET as a new generation of Dynamic Web page generation system, it has an essential improvement in platform performance compared with the original ASP. But on this basis to develop professional standards, meet the standards of production, user-welcome Web applications, but also need developers from a programmatic point of view in the page, data access and string processing and other aspects of the optimization process to improve the overall performance of the site.
In this paper, we will discuss several methods of performance optimization in asp.net and pay attention to the problems concerned.
Page Performance Optimization
1, the appropriate choice of session state
The HTTP protocol is a stateless communication protocol that cannot record and identify requests from different clients, but in practice the system maintains session state information between different requests from the client. Asp. NET solves this problem by storing session state information in a process, state server, or SQL Server database.
Saving session state information in the memory of a Web server has the best performance and speed, but lacks the ability of session state information to span multiple servers. To maintain session information between multiple Web servers, you can use a state server for storage, which increases the scalability and reliability of your system by deploying applications to multiple servers, but at the expense of performance. For extremely important session information, you need to use SQL Server storage to avoid losing important session information, but the resulting workload is much larger than the previous two.
If you do not consider the retention of state information and multiple server sharing, you should choose to save in the process of the server in order to get the best performance.
The way in which session state information is stored is selected through the Web.config file:
<sessionstate
Mode= "Inproc/stateserver/sqlserver"//Storage mode selected by this row
Stateconnectionstring= "tcpip=127.0.0.1:42424"
......
timeout= "20"/>
2, server control optimization selection
2.1 Reducing unnecessary server controls
The convenience and functionality that the server control brings is unmatched by the HTML control. However, each server control needs to create the appropriate object on the server side, at the expense of server-side resources, excessive use of server controls can greatly affect program performance.
In many cases, simply using HTML tags or data binding enables you to achieve the desired functionality. For example, a <asp:Label> control that uses it to display static information can be implemented entirely with simple tags. If the HTML control does not reach the functionality you want to implement, and you cannot implement it in a scripting language such as JavaScript or VBScript, consider selecting a server control.
2.2 Disabling unnecessary view of status
The State view properties of the server control can automatically maintain the state of the server control during a round trip, reducing the developer's workload, but requiring a large amount of server memory resources. Therefore, you should set its EnableViewState property to false, such as a commonly used <asp:Lable> and <asp:Button> control, without requiring a server control state view.
The application of 2.3 page.ispostback
Page.IsPostBack is used to record whether the page is returned from the client, or false for the first run, otherwise the page is returned from the client. The reasonable application of page.ispostback can avoid some unnecessary operation of the page in the round trip process. This property can be used to improve application performance in the Page_Load function and in some event functions that need to be initialized only once.
void Page_Load (Object o, EventArgs e)
{
if (! Page.IsPostBack)
{
Conn=new SqlConnection ("server=localhost;uid=sa;pwd=;d atabase=data");
String sql= "SELECT * from student";
Cmd. Fill (ds, "Stu");
Mydatagrid.databind ();
}
}
The above code will ensure that the database is read and bound only when the page is first accessed.
2.4 Rational use of the DataGrid control
The DataGrid control has the most powerful data display capabilities, as well as built-in data modification, deletion, addition, paging and many other functions. The DataGrid is not the best choice if you need to simply display the data. The paging function of the DataGrid control, how the data is stored (stored in ViewState), and so on, although it is easy and quick to use by program developers, the resulting performance overhead is not negligible.
The DataList control is much less powerful than the DataGrid feature. But the customization is much stronger. It is more convenient to display the specific multiline data. The function that the DataGrid can achieve, it basically can achieve.
Repeater controls have the least functionality, but they are very customizable. The server's performance is minimized because of the reduced functionality.
Therefore, when you simply display the list of data, selecting the Repeater or DataList control also achieves your purpose and reduces performance overhead.
Database Access Performance Optimization
1, the database connection and shutdown
Accessing a database resource requires creating a connection, opening a connection, and closing a connection several operations. These processes need to exchange information with the database more than once to authenticate and consume server resources. Asp. NET provides a connection pool (Connection pool) to improve the performance impact of opening and shutting down the database. The system places the user's database connection in the connection pool, takes out when needed, recovers the connection when it is closed, and waits for the next connection request.
The size of the connection pool is limited, and if the connection pool is maximized to create a connection, it will have a significant impact on performance. Therefore, after the database connection is established, the connection is opened only when the operation is really needed, closed immediately after use, so as to minimize the time to open the database connection, and avoid the occurrence of exceeding the connection limit.
2. Using Stored Procedures
A stored procedure is a set of precompiled SQL statements stored on a server, similar to a batch file in a DOS system. The stored procedure has the function of immediate access to the database, and the information processing is very rapid. By using stored procedures, you can avoid multiple compilations of commands, which reside in the cache after a single execution, and need to call only the binary code in the cache at a later time.
In addition, the stored procedure runs on the server side, independent of the ASP.net program, easy to modify, and most importantly, it can reduce the transmission of database operation statements in the network.
3, optimize the query statement
Asp. NET, the ADO connection consumes a large amount of resources, the longer the SQL statement runs, the longer it takes to occupy the system resources. Therefore, try to use optimized SQL statements to reduce execution time. For example, do not include subqueries in query statements, make full use of indexes, and so on.
String Manipulation Performance optimization
1, use the value type of ToString method
When connecting strings, you often add numbers directly to the string using the "+" number. Although this method is simple, it can get the correct result, but because it involves different data types, the number needs to be converted into a reference type by boxing operation to be added to the string. However, boxing operations have a greater impact on performance because, when such processing is done, a new object is allocated in the managed heap, and the original value is copied to the newly created object.
Use the ToString method of value types to avoid boxing operations, thereby improving application performance.
2, the use of StringBuilder class
The string class object is immutable, and the method ToString does not significantly improve performance for a string object that is essentially recreating a string object and assigning the new value to the object.
When working with strings, it is best to use the StringBuilder class, whose. NET namespace is system.text. Instead of creating a new object, the class operates directly on the string through methods such as Append,remove,insert, and returns the result of the operation through the ToString method.
Its definition and operation statements are as follows:
int num;
System.Text.StringBuilder str=new System.Text.StringBuilder (); Creating a String
Str. Append (Num. ToString ()); Add Numeric num
Response.Write (str. ToString); Show action Results
Asp. NET Application Performance test
Before performing a performance test on an asp.net application, make sure that the application is not faulty and that the functionality is correct. Specific performance tests can be performed using the following tools:
WEB Application Strees Tool (WAS) is a free test tool released by Microsoft that can be downloaded from http://webtool.rte.microsoft.com/. It can simulate hundreds of users at the same time access to Web applications requests, the formation of traffic load on the server, so as to achieve the purpose of testing, can generate average TTFB, average TTLB performance summary reports.
Application Center Test (ACT) is a test tool that is attached to the Enterprise Edition of Visual Studio.NET and is a fully supported Web application testing tool for Microsoft. It can visually generate chart results that are more functional than was, but do not have the ability to simultaneously test multiple clients.
The performance counter in the administrative tools of the server operating system to monitor the server to understand application performance.
Conclusion
For Web site developers, when writing asp.net applications, pay attention to performance issues, develop good habits, improve application performance, at least delay the necessary hardware upgrades, reduce the cost of the site.
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.