Asp.net performance optimization Summary

Source: Internet
Author: User
Tags add numbers

The latest optimization of the original project is summarized as follows:

1. Use stored procedures (if exec stored procedure parameters are used in the program, the execution does not seem to be much faster ).

It is pre-compiled in the database and does not need to spend a lot of time on string transmission. Prevents SQL injection attacks.

2. Optimize database statements as much as possible to make the logic as simple as possible

A) when using a function, the efficiency of charindex> like> padindex decreases sequentially.

B) When querying whether a field contains an in, separated field string, it is best not to use in for a very slow speed.

There are many other things that can be summarized. I will not describe them here.

3. EnableViewState (view State of the page ). Set this parameter to false unless otherwise specified.

When ViewState is used, each object must be first serialized to ViewState, and then deserialized by returning the object. Therefore, ViewState is costly. Minimize the number of objects in ViewState. ViewState can be disabled in the following cases:

(1) Page Control (. ascx ).

(2) The page is not returned to itself.

(3) You do not need to process control events.

(4) The control does not have dynamic or data-bound attribute values (or each postpack is processed in code ).

ViewState is disabled on a single page or every page, as shown below:

SINGLE Page: <% @ Page EnableViewState = "False" %>

Each page: In web. config, <Pages EnableViewState = "false"/>

EnableSessionState: retain the default value (if sessionstate is used on the page, it will occupy resources ).

EnableViewStateMac: If there are no special security requirements, keep the default value.

4. Pagelayout page layout model

We recommend that you use Flowlayout (the element is not added with the absolute positioning attribute ). Gridlayout (absolute positioning attribute) uses absolute positioning to produce more code than Flowlayout, mainly the positioning information of controls. Radiobuttonlist and checkboxlist

5. Remember to release the Debug status of the page when the project is released.

6. Select the html control whenever possible.

Functions that can be implemented on the client are implemented on the client (proficient in JavaScript) to reduce the pressure on the server. Data Control selection order: Repeater, DataList, and DataGrid.

7. After a database connection is established, the connection is enabled only when operations are required. Close the connection immediately after use, so as to minimize the time for opening the database connection and avoid exceeding the connection limit.

8. Optimized string operation performance

(1) Use the ToString method of the 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, numbers must be converted to reference types by packing them to be added to strings. 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.

9. cache data or page output as long as 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.

10. Use HttpServerUtility. Transfer to redirect between pages of the same application

Use the Server. Transfer syntax to avoid unnecessary client redirection by using this method on the page. However, the usage of Response. Redirect. Response. Execute should be differentiated as needed.

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

Be careful not to allocate too much memory to each request, because the Garbage Collector must do more work more frequently. In addition, do not point unnecessary pointers to objects because they will keep objects active and avoid objects containing the Finalize method as much as possible, because they will lead to more work in the future. In particular, do not release resources in Finalize calls, because resources may always consume memory before they are recycled by the garbage collector. At last, this problem often causes a devastating impact on the performance of the Web server environment, because it is easy to exhaust a specific resource while waiting for Finalize to run.

12. Do not rely on exceptions in the code

Exceptions greatly reduce performance, so you should not use them as a way to control normal program processes. If the code may cause exceptions, perform this operation. Do not capture exceptions before processing the status. Common solutions include checking null, assigning a value to a String that is analyzed as a numeric value, or checking a specific value before applying a mathematical operation.

13. Use HttpResponse. Write to concatenate strings

This method provides very effective buffering and connection services. However, if you are executing extensive connections, use multiple Response. Write calls. The technology shown in the following example is faster than the connection string using a single call to the Response. Write method.

Response. Write ("atest ");
Response. Write (strString );
Response. Write ("boxbig ");

14. Keep the buffer closed for special reasons.

Disabling Web form page buffering can cause a large amount of performance overhead.

15. Avoid unnecessary round-trip to the server

Use Page. IsPostBack to avoid unnecessary operations on the round-trip process.

Although you may want to use the time-saving and code-saving functions of the Web forms page framework as much as possible, ASP. NET Server controls and sending back events cannot be used in some cases. Generally, you only need to start the round-trip process to the server when retrieving or storing data. Most data operations can be performed on clients during the round-trip process.

16. 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 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. Microsoft used the IIS log viewing tool LogParserLizardSetup. msi and LogParser. msi. You can view the execution time of each page load call.

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.