24 methods of high performance in asp.net

How to solve the performance problem? Here are the points you need to check as a .NET developer before your application is released.

1.debug=「false」

When creating an ASP.NET web application, it is set to "true" by default. During development, it is very useful to set it to "true", but when the application is deployed, it needs to be set to "false".

<compilation defaultLanguage="C#" debug="false" targetFramework="4.0" />  

2. Turn off tracing

Tracing is horrible, did you forget to turn it off. If it doesn't work, be sure to edit web.config and close it. It will take up a lot of your program resources.

<trace enabled="false" requestLimit=”10” pageoutput=”false” traceMode=”SortByTime” localOnly=”true”> 

3. Disable session

If you don't use session session tracking be sure to disable it. You can set in each asp.net page as follows:

<%@ page language="c#" codebehind="webform1.aspx.cs" autoeventwireup="false" inherits="webapplication1.webform1" enablesessionstate="false" %> 

4. Deploy the app using the release version

When deploying your application to production, make sure to use release mode, not debug mode. Request timeouts can easily occur if debug templates are used. Deploy as a release build and you'll notice a huge speedup.

5. Close the View State of the page

View State is mainly used for echoing after submission. It is only useful when the data in the page is submitted to this page. It defaults to "true". If you are not using form data postback, then you can turn off View State.

<%@ Page EnableViewState="false" %>  

6. Avoid Response.Redirect

Redirect (redirect) is very troublesome, it is only used to jump from the current physical server development to other servers. If you just jump to the page in this server development, please use the Server.Transfer syntax, which will reduce a lot of unnecessary client redirection.

7. Use the StringBuilder class and use the ToString() method

The String class object is immutable. The reassignment of the String object essentially recreates a String object and assigns the new value to the object. Its method ToString does not improve performance significantly. When working with strings, it is best to use the StringBuilder class, whose .NET namespace is System.Text. This class does not create a new object, but directly operates on the string through Append, Remove, Insert and other methods, and returns the operation result through the ToString method. Its definition and operation statement are as follows

int num;  
  System.Text.StringBuilder str = new System.Text.StringBuilder(); //创建字符串 
  str.Append(num.ToString()); //添加数值num   Response.Write(str.ToString); //显示操作结果

8. Avoid throwing exceptions

Exceptions cause slowdowns and make application pages appear abnormal, making other operations impossible. Exceptions that occur can be logged to the log file using try / catch.

9. Use the finally method to recycle resources

If you use other database connections and access files a lot during application development, be sure to close them when you're done. The finally block is the last to be executed in the program, so the code in it will ensure that it will be executed, and the closing code must be executed in this development method block.

10. Use client-side script authentication

Replace server-side validation with client-side validation. Data validation on the server development side will consume a lot of resources on your server development, and will generate a large amount of page data back.

11. Use Page.IsPostback

Make sure not to execute too many passback tags. Use the Page.IsPostBack property to ensure that only page initialization logic is executed when a page is first loaded, without postbacks to the responsive client.

12. Use pagination

Most web application data is displayed in tabular form. Paging has the advantage of using application development program efficiency. Try to display a small amount of data at a time, which will speed up the page display speed.

13. Use Ajax to make asynchronous calls

Use Ajax methods to make asynchronous calls.

14. Remove unused HttpModules

For httpModules, we can understand it as: establishing a general HttpApplication event hook that can be inserted into any Web application. Using HttpModule is reusable and requires no application-specific code, just an entry in web.config. In the web.config file, delete unused HttpModules.

15. Avoid recursive functions/nested loops

Nested loops and recursive functions need to be avoided in any programming language to improve performance.

16. Do not use unnecessary Server Control

In ASP.NET, a large number of server-side controls facilitate program development, but may also bring performance losses, because each time a user operates a server-side control, a round-trip process with the server-side is generated. Therefore, it is not necessary to use Server Control sparingly.

17. When calling multiple operations, use multithreading

The problem arises when a single thread is stuck on this issue running for a long time. Therefore, multiple threads can be used to improve the responsiveness of the application.

18. Database connection and shutdown

Accessing database resources requires several operations to create a connection, open a connection, and close a connection. These processes require multiple exchanges of information with the database to pass authentication, which consumes server resources. ASP.NET provides a connection pool (Connection Pool) to improve the performance of opening and closing the database. The system puts the user's database connection in the connection pool, takes it out when needed, reclaims the connection when it is closed, and waits for the next connection request. The size of the connection pool is limited. If a connection is still required to be created after the connection pool reaches the maximum limit, performance will be greatly affected. Therefore, after establishing a database connection, only open the connection when the operation is really needed, and close it 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 forward-only data cursors

The SqlDataReader class provides a way to read a stream of forward-only data retrieved from a SQL Server database. The SqlDataReader class provides higher performance than the DataSet class if a situation arises that allows you to use it when creating an ASP.NET application. This is the case because the SqlDataReader uses SQL Server's native network data transfer format to read data directly from the database connection. Additionally, the SqlDataReader class implements the IEnumerable interface, which also allows you to bind data to server controls. See the SqlDataReader class for more information. For information about how ASP.NET accesses data, see Accessing Data with ASP.NET.

20. High-performance SQL statement rules

  • Try to avoid full table scans
  • Try to avoid null value judgment for fields in the where clause
  • Try to avoid using the != or <> operators in the where clause
  • Try to avoid using or to join conditions in where clauses
  • in and not in should also be used with caution
  • Do not perform functions, arithmetic operations, or other expression operations to the left of the "=" in the where clause
  • Update statement, if only 1 or 2 fields are changed, do not update all fields
  • For multiple JOIN tables with a large amount of data (hundreds are considered large here), it is necessary to paginate and then JOIN, otherwise the logical read will be very high and the performance will be very poor.
  • Use varchar/nvarchar instead of char/nchar as much as possible. For more rules and methods, please refer to: http://database.51cto.com/art/201407/445934.htm

21. Cache

Caching is a technology that trades space for time. Generally speaking, it means that the data you get is stored in 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. Fetch the data you store in memory. Caching is an indispensable data processing mechanism for website performance optimization, which can effectively relieve database pressure. Caching in ASP.NET is mainly divided into:

  • page cache
  • data source cache
  • custom data cache

22. Do load balancing and server additions

Load balancing should not be seen only as a means to achieve scalability. While it certainly improves scalability, many times it increases the performance of a web application because requests and users are spread across multiple servers.

23. Code inspection and optimization through FxCop

FxCop is a code analysis tool that uses a rule-based engine to check out non-standard parts of your code; you can also customize your own rules to add to this engine. Some of these rules are:

  • Avoid too many local variables
  • Avoid using private code that is not called
  • Avoid uninstantiated inner classes
  • Avoid using unsealed features
  • Avoid unnecessary casts
  • Initialize static fields of reference types inline
  • Mark assemblies with the NeutralResourcesLanguageAttribute
  • Mark members as Static and so on.

24. ASP.NET Performance Monitoring Tool

These are tools for monitoring the performance of your code.

  • .NET Memory Profiler
  • Red Gate ANTS Performance Analysis Tool
  • Fiddler
  • performance counters

Conclusion: These are some tips for performance tuning. Performance tuning is not a day or two work, but an iterative process. For website developers, being aware of performance issues when writing ASP.NET applications, and developing good habits to improve application performance can at least delay necessary hardware upgrades and reduce website costs.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324974657&siteId=291194637