24 ways to improve the performance of ASP.NET Web applications

  • How to solve the performance problem? The following are the points that need to be checked as a .NET developer before the application system is released

1.debug = [false]

  When creating an ASP.NET Web application, the default setting is [true] . During the development process, setting to [true] is very useful, but when the application is released and deployed, it needs to be set to [false]

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

2. Turn off tracing (tracking)

  Tracing is very scary, have you forgotten to turn it off. If it doesn't work, make 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 need session tracking, be sure to 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. Use the release version to deploy the application

  When deploying an application to a production environment, be sure to use the release version mode, not the debug mode. If you use the debug template, request timeouts are extremely likely to occur. Deploy as a release version, you will find that the speed is greatly improved

5. Close the View State of the page

View State is mainly used for echoing after submission. It is only useful when the data on the page is submitted to this page. The default is [true]. If you do not use form data return, you can turn off 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. If you just use the Server.Transfer grammar if you only jump to pages within the development of this server , this will reduce a lot of unnecessary client redirects

7. Use StringBuilder class and use ToString() method

  String objects are immutable. Re-assignment of String objects essentially re-creates a String object and assigns a new value to the object. Its method ToString does not significantly improve performance.
  When dealing 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 manipulates the string through Append, Remove, Insert and other methods, and returns the operation result through the ToString method. Its definition and operation statements are as follows

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

8. Avoid throwing exceptions

  Exceptions will slow down the speed and make the application page display abnormally, making it impossible to perform other operations. You can use try / catch to record the exceptions in the log file

9. Use the finally method to reclaim resources

  If you use a lot of other database connections and access files in your application development, please make sure to close them after using them. The finally block is the last to be executed in the program, so the code inside will be guaranteed to be executed, and the closing code must be executed in this development method block

10. Use client script verification

  Use client side verification instead of server development side verification. Server development data verification will consume a lot of resources on your server development, and will send back a large amount of page data on your behalf

11. Use Page.IsPostback

  Please make sure not to execute too much postback code. Use the Page.IsPostBack property to ensure that only the page initialization logic is executed. When a page is loaded for the first time, it does not send back to the responding client

12. Use paging

  Most web application data is displayed in tabular form. Paging has the efficiency of using application development programs. Try to display a small part of the data each time, this will speed up the page display speed

13. Use Ajax asynchronous call

  Use Ajax methods to make asynchronous calls.

14. Delete unused HttpModules

  For httpModules , we can understand it as: establishing a universal HttpApplication event hook that can be inserted into any Web application . Using HttpModule is reusable, no specific language application code is required, only one entry in web.config is required . In the web.config file, delete unused HttpModules.

15. Avoid recursive functions/nested loops

  Avoid nested loops and recursive functions in any programming language to improve performance

16. Don't use unnecessary Server Control

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

17. When calling multiple operations, use multiple threads

  When the problem occurred, the single thread stuck on this problem for a long time. Therefore, multiple threads can be used to improve the responsiveness of the application

18. Database connection and closure

  Access to 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 identity verification, which consumes server resources.
  ASP.NET provides Connection Pool to improve the performance impact of opening and closing the database. The system puts the user's database connection in the connection pool, takes it out when needed, retracts 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 its maximum limit, performance will be greatly affected. Therefore, after establishing a database connection, only open the connection when it is really necessary to operate , and close it immediately after use, so as to minimize the time of database connection opening 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 an inbound data stream retrieved from a SQL Server database. If there are circumstances that allow you to use it when creating an ASP.NET application, the SqlDataReader class provides higher performance than the DataSet class.
  This is the case because SqlDataReader uses SQL Server's native network data transfer format to read data directly from the database connection.
  In addition, the SqlDataReader class implements the IEnumerable interface, which also allows you to bind data to server controls.
  For more information, see SqlDataReader class. For information on how ASP.NET accesses data, see Accessing data through ASP.NET

20. High-performance SQL statement rules

  • Try to avoid full table scan
  • Try to avoid the null value judgment of the field in the where clause
  • Try to avoid using the where clause !=or <>operator
  • Try to avoid using or in the where clause to connect conditions
  • inAnd not inshould be used with caution
  • Do not where clause of =a function, expression of arithmetic or other operations on the left
  • update statement, if only one or two fields are changed, do not update all fields
  • For multiple tables with a large amount of data ( here hundreds of them are too large ) join, paging and then join are necessary, otherwise the logical read will be very high and the performance will be poor
  • Use as much as possible varchar / nvarcharinstead ofchar / nchar

21. Cache

  Caching is a technology that exchanges space for time. The popular point is to store the data you get in memory for a period of time. During this short period of time, the server does not read the database or the real data source, but reads it. Take the data you store in memory.
  Caching is an indispensable data processing mechanism for website performance optimization. It can effectively relieve database pressure

The cache in ASP.NET is mainly divided into:
  • Page cache
  • Data source cache
  • Custom data cache

22. Do load balancing and server bonus

  Load balancing should not only be seen as a means to achieve scalability.
  Although it certainly improves scalability, it often increases the performance of Web applications because requests and users are distributed to multiple servers.

23. Code inspection and optimization through FxCop

  FxCop is a code analysis tool that uses a rule-based engine to detect irregular parts of your code.
  You can also customize your own rules to join this engine.

Some of these rules are:
  • Avoid too many local variables
  • Avoid using uncalled private code
  • Avoid uninstantiated inner classes
  • Avoid using unsealed features
  • Avoid unnecessary coercion
  • Initialize static fields of reference types inline
  • With a NeutralResourcesLanguageAttributeset program mark
  • The members of the mark static, etc.

24. ASP.NET performance monitoring tool

These are the tools used to monitor the performance of the code:
  • .NET memory analyzer
  • Red Gate ANTS performance analysis tool
  • Fiddler
  • Performance counter

in conclusion:

  The above are some tips for performance tuning. Performance tuning is not a day or two work, but an iterative process.
  For website developers, pay attention to performance issues when writing ASP.NET applications, develop good habits, improve application performance, at least postpone necessary hardware upgrades, and reduce website costs

Guess you like

Origin blog.csdn.net/qq_43562262/article/details/109162905