<Reprinted> 24 methods and skills for improving the performance of ASP. NET Web applications, asp. netweb
In this article, we will introduce some methods and techniques to improve the performance of ASP. NET Web applications. As we all know, solving performance problems is a tedious task. When a performance problem occurs, everyone will blame the developers who write the code.
The following is the translation
How can we solve the performance problem? The following are the points to check before the application system is released. NET developers.
1. debug = "false 」
When you create an ASP. NET Web application, the default value is "true 」. It is very useful to set "true" during development, but you must set it to "false" during application release and deployment 」.
<compilation defaultLanguage="C#" debug="false" targetFramework="4.0" />
2. Disable tracing)
Tracing is terrible. Have you forgotten to close it. If it is useless, edit web. config and disable it. It will occupy a lot of your program resources.
<trace enabled="false" requestLimit=”10” pageoutput=”false” traceMode=”SortByTime” localOnly=”true”>
3. Disable session
If you cannot use session tracing, disable it. You can set the following in each asp.net page:
<%@ page language="c#" codebehind="webform1.aspx.cs" autoeventwireup="false" inherits="webapplication1.webform1"enablesessionstate="false" %>
4. Deploy the application using the released version
When deploying an application to the production environment, make sure that the release mode is used instead of the debug mode. If the debugging template is used, request timeout is very likely. Deploy it into a release version, and you will find that the speed is greatly improved.
5. Close the View State of the page
View State is mainly used for Echo after submission. It is useful only when data on the page is submitted to this page. The default value is "true 」. If you do not use form data return, you can disable View State.
<%@ Page EnableViewState="false" %>
6. Avoid using Response. Redirect
Redirect (redirection) is very troublesome. It is only used to jump from the current physical server development to other servers. Use the Server. Transfer syntax to redirect the webpage only during the development of the Server, which reduces unnecessary client redirection.
7. Use the StringBuilder class and the ToString () method
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
8. Avoid throwing exceptions.
An exception will slow down the speed and cause an exception on the application page, making it impossible to perform other operations. You can use try/catch to record exceptions to log files.
9. Use finally to reclaim Resources
If you are using a large number of other databases in application development to connect to and access files, please be sure to close them after use. The finally block is the final execution in the program. Therefore, the code in the finally block must be executed and the code to be disabled must be executed in the development method block.
10. Use client script for verification
Use client verification instead of server development-side verification. Data verification on the server development end will consume a lot of resources on your server development and return a lot of page data on behalf of you.
11. Use Page. IsPostback
Make sure that you do not execute excessive return code. Use the Page. IsPostBack attribute to ensure that only the Page initialization logic is executed. When a Page is loaded for the first time, it is not sent back to the responding customer.
12. Use pagination
Most Web application data is displayed in tables. By page, you can use the application development program efficiency. Try to display a small part of data each time, which will speed up the page display.
13. asynchronous call using Ajax
Use Ajax for asynchronous calls.
14. delete unused HttpModules
For httpModules, we can understand that: Create a generic HttpApplication event hook that can be inserted into any Web application. HttpModule is reusable and requires no specific language application code. You only need an entry in web. config. In the web. config file, delete unused HttpModules.
15. Avoid recursive functions/nested loops
Nested loops and recursive functions must be avoided in any programming language to improve performance.
16. Unnecessary Server Control
ASP. NET, a large number of server-side controls facilitate program development, but may also cause performance loss, because each time a user operates a server-side control, a round-trip process with the server is generated. Therefore, Server Control should be used less unless necessary.
17. Use multiple threads to call multiple operations.
When a problem occurs, a single thread is stuck on this issue for a long time. Therefore, multiple threads can be used to increase the response speed of applications.
18. database connection and Closure
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.
19. Use the SqlDataReader class for fast data-only cursor
The SqlDataReader class provides a method for reading data streams only retrieved from the SQL Server database. If you are allowed to use an ASP. NET application when creating it, The SqlDataReader class provides higher performance than the DataSet class. This is because SqlDataReader uses the Local Network Data Transmission Format of SQL Server to directly read data from the database connection. In addition, the SqlDataReader class implements the IEnumerable interface, which allows you to bind data to server controls. For more information, see SqlDataReader class. For information about how ASP. NET accesses data, see access data through ASP. NET.
20. High-Performance SQL statement rules
- Avoid full table scan whenever possible
- Try to avoid null value determination for the field in the where clause
- Try to avoid using it in the where clause! = Or <> Operator
- Avoid using or in the where clause to connect conditions.
- Use in and not in with caution.
- Do not perform functions, arithmetic operations, or other expression operations on the left side of "=" in the where clause
- Update statement. If only one or two fields are modified, do not Update all fields.
- For JOIN operations on tables with large data volumes (hundreds of rows are larger), the JOIN operation must be performed by page. Otherwise, the logical read operation will be high and the performance will be poor.
- Use varchar/nvarchar as much as possible to replace char/nchar for more rule methods refer to: http://database.51cto.com/art/201407/445934.htm
21. Cache
Cache is a technology that exchanges space for time. In other words, it stores your data in the memory for a period of time, in this short period of time, the server does not read the database or the real data source, but reads the data you store in the memory. Cache is an indispensable data processing mechanism for website performance optimization. It can effectively relieve database pressure. The cache in ASP. NET is mainly divided:
- Page Cache
- Data Source Cache
- Custom Data Cache
22. Load Balancing and server Addition
Server Load balancer should not be regarded as a means to achieve scalability. Although it certainly improves scalability, it often increases the performance of Web applications because requests and users distribute multiple servers.
23. Code check and optimization through FxCop
FxCop is a code analysis tool that uses a rule-based engine to check out the nonstandard parts of your code. You can also customize your own rules to add them to this engine. Some of the rules are as follows:
- Avoid excessive local variables
- Avoid using private code that is not called
- Avoid uninstantiated internal classes
- Avoid using unsealed features
- Avoid unnecessary forced conversions
- Initialize static fields of the reference type in the inline mode
- Tag an assembly with NeutralResourcesLanguageAttribute
- Mark a member as Static.
24. ASP. NET performance monitoring tool
These are tools used to monitor code performance.
- . NET memory Analyzer
- Red Gate ANTS Performance Analysis Tool
- Fiddler
- Performance counters