ASP. NET Website performance optimization [1]

Source: Internet
Author: User
Tags add numbers website performance
1. database access performance optimization
1) connection and closure of the Database Connection Pool
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. The stored procedure can avoid multiple compilation of commands. After a command is executed, the execution plan will reside in the cache. You only need to directly call the binary in the cache when necessary. Code You can.
In addition, the stored procedure runs on the server and is independent of ASP. NET. Program To facilitate modification, the most important thing is that it can reduce the transmission of database operation statements in 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.
2. Optimized string operation performance
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:
The following is a reference clip:
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
3. Optimize configuration files for Web server computers and specific applications to meet your specific needs
By default, ASP. NET configuration is set to enable the widest range of features and adapt to the most common solutions. Therefore, application developers can optimize and modify some of the configurations based on the features used by the application to improve application performance. The following lists some options you should consider.
Only enable authentication for the desired application. By default, the authentication mode is windows, or NTLM is integrated. In most cases, it is best to disable authentication in the machine. config file and enable authentication in the web. config file for applications that require authentication.
Configure the application based on the appropriate request and response encoding settings. ASP. NET default encoding format is UTF-8. If your application is strictly ASCII, configure the application to use ASCII for a slight performance improvement.
Disable autoeventwireup for applications. In the machine. config file, set the autoeventwireup attribute to false, which means that the page does not match the method name with the event or hook the two (for example, page_load ). If page developers want to use these events, they need to override these methods in the base class (for example, they need to rewrite page. onload for page loading events, rather than using the page_load method ). If autoeventwireup is disabled, the page will be slightly improved by leaving the event connection to the page author rather than automatically executing it.
Remove unused modules from the request processing pipeline. By default, all functions of nodes in the machine. config file of the server computer are retained to active. Based on the features used by the application, you can remove unused modules from the request pipeline for a slight performance improvement. Check each module and its functions and customize it as needed.
For example, if you do not use session Status and output cache in an application, you can remove them from the list so that requests do not perform other meaningful processing, you do not have to execute the code for entering and leaving each module.
4. Disable the debugging mode.
Remember to Disable debug mode before deploying production applications or performing any performance measurements. If the debug mode is enabled, the performance of the application may be greatly affected.
5. For applications that rely heavily on external resources, consider enabling network gardening on a multi-processor computer.
ASP. net Process Model helps enable scalability on a multi-processor computer, distribute work to multiple processes (one CPU per worker), and each process sets the processor relationship to its CPU. This technology is called Web gardening. Enable Web gardening for your application if your application uses a slow database server or calls a COM Object with external dependencies (only two possibilities are mentioned here. However, before deciding to enable Web gardening, you should test how applications are executed in the Web garden.
6. cache data and page output whenever possible
ASP. NET provides some simple mechanisms to cache the output or data of these pages when you do not need to request dynamic computing page output or data for each page. In addition, by designing pages and data requests to be cached (especially in areas where the site is expected to have a large amount of communication), you can optimize the performance of these pages. Compared with any web form function of. NET Framework, using the cache appropriately can improve the performance of the website. Sometimes this increase is more than an order of magnitude.
There are two points to note when using the ASP. NET cache mechanism. First, do not cache too many items. Each item in the cache has an overhead, especially for memory usage. Do not cache items that are easy to recalculate and rarely used. Second, the period of validity allocated to cached items should not be too short. Items that expire soon will result in unnecessary turnover in the cache, and often lead to more code cleanup and garbage collection work. If you are concerned about this issue, monitor the cache total turnover rate performance counters associated with ASP. NET applications performance objects. High turnover rate may indicate problems, especially when items are removed before expiration. This is also called memory pressure.
7. Select a data view mechanism suitable for pages or applications
There are often important trade-offs between convenience and performance based on how you choose to display data on a web form page. For example, the DataGrid web server control may be a convenient and quick way to display data, but its overhead is often the largest in terms of performance. In some simple cases, it may be very effective to generate an appropriate HTML to present your own data, but custom and browser targeting will quickly offset the extra benefits you get. The repeater Web Server Control is a compromise between convenience and performance. It is efficient, customizable, and programmable.
[To be continued ...]
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.