MasterofProject
return Sign in
Ten

24 methods and techniques to improve the performance of Web ASP.NET applications

In this article, some methods and techniques to improve the performance of Web ASP.NET applications are presented. As is known to all, solving performance problems is a tedious task, and when a performance problem occurs, everyone will blame developers for writing code.

The following is a translation

Picture description

That performance issues in the end how to solve? The following is the application of the system before the release, as.NET developers need to check the point.

1.debug= "false"

When you create a Web ASP.NET application, the default is set to "true". In the development process, set to "true" is very useful, but in the application of the deployment, it is necessary to set it to "false".

DefaultLanguage= <compilation"C#" Debug="False"TargetFramework="4">

2 close tracing (trace)

Tracing is very scary, do you have to forget to close it. If it's no use, please 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 do not use the session session tracking, please be sure to disable it. You can set the following on each asp.net page:

<% @Language= page"C#"Codebehind="Webform1.aspx.cs"Autoeventwireup="False"Inherits="Webapplication1.webform1"

Enablesessionstate="False" % >  

4 deploy applications using the release version

When deploying an application to a production environment, make sure that the release version mode is used, rather than the debug mode. If you are using the debug template, the request timeout is very easy. Deployed into the release version, you will find that there is a great improvement in speed.

5 close the page of State View

State View is mainly used in the submitted later, it is only in the page of the data is submitted to this page only useful. Its default is "true". If you do not use the form data back, you can shut down View State.

<% @EnableViewState= Page"False" % >  

6 avoid using Response.Redirect

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

7 using the StringBuilder class and using the ToString () method

The string class object is immutable, for re assignment of string objects, in essence, is to create a string object and the new value is assigned to the object, the method toString on performance improvement was not is very significant. When processing a string, it is best to use the StringBuilder class, whose.NET namespace is System.Text. This class is not the creation of new objects, but through the Append, Remove, Insert and other methods to directly operate on the string, ToString method to return the results of operations. The definition and operation statement are as follows

IntNum;
System.Text.StringBuilderStr=NewSystem.Text.StringBuilder ();/ / create a string
  Str.Append (num.ToString ());Add value / / num
Response.Write (Str.ToString);The operation results show.

8 avoid throwing exceptions

An exception causes the speed to slow, and the application page displays an exception, making it impossible for other operations to be performed. You can use try / catch to make an exception log to the log file.

9 using the finally method to recycle the resources

If you are using a large number of other database connections and access files in your application development, make sure you close them after they are used up. Finally block is the last to be executed in the program, so the code inside the program will ensure that it will be executed.

10 using client script validation

Client side validation instead of server development side validation. Development server data validation will consume a lot of resources development for your server, and will return a large number of page data.

11 using Page.IsPostback

Please make sure not too much return code execution. Use the Page.IsPostBack property to ensure that only the page initialization logic is executed, and when a page is loaded for the first time, it does not respond to the customer back.

12 use paging

Most Web application data are displayed in tabular form. Paging has the efficiency of application development program. Each try to show a small part of the data, this will speed up the page display speed.

13 using Ajax asynchronous call

Using the Ajax method, the asynchronous call.

14 delete unused HttpModules

For httpModules, we can understand that the establishment of a generic HttpApplication event hooks that can be inserted into any Web application. The use of HttpModule is a reusable, do not require a special attribute application code, only one of the web.config in the entry. In the web.config file, delete the unused HttpModules.

15 avoid recursive functions / nested loops

In any programming language, it is necessary to avoid nested loops and recursive functions to improve performance.

16 do not use unnecessary Control Server

ASP.NET, a large number of server side controls to facilitate the development of the program, but it may also bring the loss of performance, because the user once every time the server control, it produces a round-trip between the server and the server. Therefore, it is not necessary, should use less Control Server.

17 when you call multiple operations, use the multi thread

When problems arise, the single line card on this issue for a long time to run. Therefore, multiple threads can be used to improve the response speed of the application.

18 database connection and shutdown

Accessing database resources requires creating connections, opening connections, and closing several operations. These processes need to exchange information with the database many times in order to pass authentication, compare the cost of server resources. ASP.NET provides a connection pool (Pool Connection) to improve the performance of the open and close the database on the impact. System will be the user's database connection in the connection pool, the need to take out when closed, to recover the connection, waiting for the next connection request. Connection pool size is limited, if the connection pool to reach the maximum limit is still required to create a connection, will greatly affect the performance of. Therefore, after the establishment of a database connection only in real operation is needed to open the connection, after use, immediately shut down, so as to minimize the database connection open time and avoid exceeds the connection limit.

19 SqlDataReader class for fast forward only data cursor

The SqlDataReader class provides a way to read only the data stream retrieved from the Server SQL database. If you appear to allow you to use it when you create a ASP.NET application, the SqlDataReader class provides a higher performance than the DataSet class. This is the case, because SqlDataReader uses Server SQL's native network data transfer format to read data directly from a database connection. In addition, the SqlDataReader class implements the IEnumerable interface, which allows you to bind data to a server control. For more information, see the SqlDataReader class. For information about how ASP.NET access data, see accessing data via ASP.NET.

20 high performance SQL statement rules

  • As far as possible to avoid full table scan
  • Try to avoid the where clause in the field of null value judgments
  • Try to avoid using the where clause or operator = < >!
  • Try to avoid using or to connect the condition in the where clause
  • In and not in should be used with caution
  • Do not perform functions, arithmetic operations, or other expressions on the left of the where clause.
  • Update statement, if you only change 1, 2 fields, do not all fields Update
  • For more than a large amount of data (here hundreds of even bigger) table JOIN, the first page and then JOIN, otherwise the logic will be very high, very poor performance
  • As far as possible to use varchar/nvarchar instead of char/nchar more rule method, please refer to:Http://database.51cto.com/art/201407/445934.htm

21 cache

Caching is a trade space for time technology, popular point is to get your data stored in memory for a period of time, in the short time of the server not to read the database, or is the true source of data, but read your data stored in memory. Caching is an indispensable data processing mechanism for website performance optimization, which can effectively alleviate the pressure of the database. ASP.NET in the cache is mainly divided into:

  • Page cache
  • Data source cache
  • Custom data cache

22 do load balancing and server addition

Load balancing should not only be considered as a means to achieve scalability. Although it certainly improves scalability, a lot of time it has increased the performance of Web applications, because requests and users are distributed to multiple servers.

23 code check and optimize by FxCop

FxCop is a code analysis tool that uses a rule based engine to check out the parts of your code that are not part of the code, and you can customize your own rules to add to the engine. Some of the rules are:

  • Avoid too many local variables
  • Avoid using private code that is not called
  • Avoid non class internal classes
  • Avoid the use of non sealing characteristics
  • Avoid unnecessary forced conversions.
  • Static fields for initialization of reference types in inline mode
  • Using NeutralResourcesLanguageAttribute tag assembly
  • Mark the member as Static, etc..

24.ASP.NET performance monitoring tool

These are tools for monitoring the performance of the code.

  • .NET memory analyzer
  • Gate ANTS Red performance analysis tool
  • Fiddler
  • Performance counter

Conclusions: These are some of the tips for performance tuning. Performance tuning is not a day for two days of work, but an iterative process. For web developers, in the preparation of an ASP. Net application note performance problems, to develop good habits, improve application performance, at least can delay the necessary hardware upgrades, reduce site cost. (text:OneAPMEngineer / commissioning editor Zhong Hao))

comment