1. Database access performance optimization

Database connections and close

Access database resources need to create connections, open and close the connection linking several operations. These processes necessary to exchange information with the database to verify identity through more cost server resources. ASP.NET provides a connection pool (Connection Pool) to improve open and close the performance of the database. The database system will connect users on the connection pool, retrieve necessary, to recover the closure of connections, waiting for the next connection request. Connection pool size is limited, if the connection pool to the maximum after calling for the creation of connection is bound to greatly affect performance. Therefore, the establishment of a database is connected only in operation when the real need to open access, closed immediately after use, so as to minimize the time to open the database connection, to avoid restrictions on the situation beyond the connection.

The use of stored procedures

Storage process is stored on the server of a group of pre-compiled SQL statement, similar to DOS batch file in the system. Storage process with immediate access to the database functions, information processing is extremely fast. Storage can be avoided using the process of compiling the many orders, after the implementation of a plan on the implementation of its presence in the cache, whenever the need arises simply direct call cache of binary code. In addition, the storage process in the server-side operation, independent of the ASP.NET procedures for amending the most important thing is that it can reduce the statement in the database in the network transmission.

Optimization of query

ADO connections in ASP.NET considerable consumption of resources, SQL statements in the longer run, the occupation of system resources and the longer the time. Therefore, to make full use of the SQL statement optimized to reduce the execution time. For example, the query is not included in the sub-query, such as full use of the index.

2. String performance optimization

The use of types of ToString method

In connection string, the frequent use of “+” sign directly to the figures added to the string. Although this method simply can also get the right results, but because of the different types of data, the number of operations required by packing into use before they can add to the type of string. However, packing a greater impact on operating performance, because in this type of treatment, will be hosting a new distribution of a pile of objects, the value of the original copy to the newly created object. The use of types of ToString method of packing operation can be avoided, thereby improving application performance.

Use StringBuilder class

String class object is irrevocable, the String object of the re-assignment is essentially a re-creation of a String object and a new value to the object and its methods of improving performance ToString is not very significant. In dealing with strings, it is best to use StringBuilder class,. NET namespace is System.Text. Such is not to create a new object, but through Append, Remove, Insert, and other direct means of a string operation, with the return ToString method of operation results. Its definition and the statement as follows:

Int num;

System.Text.StringBuilder str = new System.Text.StringBuilder (); / / create a string

str.Append (num.ToString ()); / / add value num

Response.Write (str.ToString); / / display operating results

3. Optimize the Web server computers and application specific profiles to meet your specific needs

By default under, ASP.NET configuration is set to enable the most extensive features and to adapt to the most common programme. Therefore, application developers can use the application functions, optimization and change some of these configurations to improve application performance. Below is a list you should consider some options.

Only the needs of applications enabled authentication.

By default, authentication model for Windows, or integrated NTLM. In most cases, the need for authentication applications, the best in the Machine.config file to disable authentication, and the opening of the Web.config file authentication. Under the appropriate request and response encoding settings to configure the application. ASP.NET default encoding format for the UTF-8. If your application for strict ASCII, configure applications to use ASCII to get a little bit of performance improvement.

Consider the application to disable AutoEventWireup.

In the Machine.config file attributes will AutoEventWireup is set to false, does not mean that pages will be conducted with the method of matching and linking the two (such as Page_Load). If the page developers to use these events, the need to base class in the rewriting of these methods (for example, the need for pages to load the incident rewrite Page.OnLoad, rather than use Page_Load method). If disabling AutoEventWireup, page by page from left to connect the incident and not self-executing it, get a little bit of performance increase.

Request from the pipelines do not have to remove the module.

By default, the Machine.config file server computer nodes in all of the features are reserved for activation. According to the application used by the function, you can request from the pipeline do not have to remove the module to get a little bit of performance increase. Inspection of each module and its functions, and you need to customize it. For example, if you do not use applications in the session state and output cache, you can remove them from the list, in order to request not to implement other meaningful treatment, not the implementation of each module to enter and leave the code.

4. Debug mode must be disabled

The deployment of production applications or any performance measurement before, and always remember to disable debug mode. If you enable debug mode, the application’s performance may be a very big impact.

5. Reliance on external resources for the extensive application process, please consider the opening of multi-processor computer network Horticulture

ASP.NET process model to help enable multi-processor computer’s scalable, will be distributed to the various work processes (one for each CPU), and each process will be set up between processors for CPU. The technology known as the network of Horticulture. If an application using the database server or slower calls with the external dependence of the COM object (referred to here only two possibilities), your application for the opening of network gardening is beneficial. However, in a decision before the opening of horticulture network, you should test applications in the network in the implementation of the park.

6. As long as possible, data and page output cache

ASP.NET provides a simple mechanism, they will need for each page request dynamic calculation of the output page or pages of these data cache, or output data. In addition, through the design of the cache page and data requests (especially in the site is expected to have greater regional traffic), you can optimize the performance of these pages. And. NET Framework Web form to any function compared to the proper use of the cache site can better improve the performance, sometimes raising the number of super-class. ASP.NET cache mechanism for the use of two points need attention. First of all, not too much of the cache. Each cache of all expenses, particularly in the use of memory. Do not easy to re-cache and seldom-used items. Second, the distribution to the cache is valid not too short. Will soon expire will lead to the cache of unnecessary turnover, and often lead to more code removal and garbage collection. If concerned about this problem, surveillance and ASP.NET Applications performance targets associated Cache Total Turnover Rate performance counters. High turnover rate may indicate problems, especially when the items were removed before the expiration time. This is also known as memory pressure.

7. Select the appropriate page or application of the mechanism for data Show

According to the form you choose the Web page of data, in convenience and performance often exists between an important balance. For example, DataGrid Web server controls may be a display of data efficiently, but on its performance in terms of spending is often the greatest. In some simple cases, you can generate the appropriate HTML data showed themselves may be very effective, but custom and browser orientation will soon be offset by the additional effect. Repeater Web server control and performance is to facilitate a compromise. It efficient, customizable and programmable.

8. SqlDataReader category will be used for rapid data only into the cursor

SqlDataReader class provides a read from the SQL Server database search of the only ways into the data stream. If the creation of ASP.NET application procedures allow you to use it, then SqlDataReader class DataSet class higher than the performance. The reason why this situation is that SqlDataReader use SQL Server’s local data transmission network connecting directly from the database format to read data. In addition, SqlDataReader class implements IEnumerable interface also allows you to control data binding to the server. For more information, please see the SqlDataReader category. ASP.NET how to access data on the information, see through ASP.NET access to data.

9. SQL Server stored procedure will be used for data access

In. NET Framework to provide all the data access methods, based on SQL Server data access is the generation of high-performance, scalable Web application of the recommended option. The use of SQL Server hosting provider, they can build through the use of the storage process and not given extra special for improving the performance.

10. Avoid single-threaded unit (STA) COM components

By default under, ASP.NET does not allow any STA COM components in the running of the page. To run them, must be in. Aspx paper ASPCompat = true attributes will be included in the @ Page directive. This will be the implementation by the switch to the STA thread pool thread pool, but also HttpContext and other objects can be used to built-in COM object. The former is a performance optimization, because it would avoid the multi-threaded unit (MTA) to the closure of any call STA thread. The use of STA COM components may greatly damage to property, should be avoided as far as possible. If necessary the use of STA COM components, such as in any interop programmes should be conducted during the implementation of the large number of calls and sent during each call as much information as possible. In addition, careful not to create any structure pages during the STA COM components. For example, the code below, in the pages of examples will be constructed by a thread created MySTAComponent, and the thread is not running pages of STA thread. This may adversely affect the performance because we must complete structural pages on the MTA and the STA thread between the Marshaling.

<% @ Page Language = “VB” ASPCompat = “true”%>

<script. Runat=server>

Dim myComp as new MySTAComponent ()

Public Sub Page_Load ()

MyComp.Name = “Bob”

End Sub

</ Script>

<html>

<%

Response.Write (myComp.SayHello)

%> Response.Write (myComp.SayHello)

%>

</ Html>

The preferred mechanism is created to postpone the target until after the implementation of the STA thread under the code, such as the following example shows.

<% @ Page Language = “VB” ASPCompat = “true”%>

<script. Runat=server>

Dim myComp

Public Sub Page_Load ()

MyComp = new MySTAComponent ()

MyComp.Name = “Bob”

End Sub

</ Script>

<html>

<%

Response.Write (myComp.SayHello)

%> Response.Write (myComp.SayHello)

%>

</ Html>

The recommended practice is necessary, or in the Page_Load method in the structure of any COM components and external resources. Never any STA COM components can be stored in the structure of its thread other than the threads of the visit, the sharing of resources. Such as cache and resources, including session state such resources. Even if STA thread call STA COM components, and only this structure STA COM components of the threads can actually call for the services, which require Marshaling the founder of the thread call. This Marshaling likely to have significant loss of performance and scalability issues. In this case, please look at the COM components into the possibility of MTA COM components, or a better way is the migration code to enable the targets of a managed objects.

11. Call-intensive COM components moved to managed code

. NET Framework provides a simple way with the traditional COM components interact. The advantage is the retention of the existing investment in the same time take advantage of new platforms. However, in some cases, retain the old components of the cost of components will be made to move to managed code is worth it. Each case is different, deciding whether to move components on the best way to run Web site performance measurement. Recommend that you will need to look at how to interact with a large number of calls to any COM component to move to managed code. Many cases of old components will be impossible to move to managed code, especially in the initial relocation of Web application procedures. In such circumstances, one of the biggest obstacles is the performance data from non-hosted environment closure to the hosting environment. Therefore, the interactive operation, in any one end as much as possible the implementation of the mandate, and then a big call rather than a series of small call. For example, the common language runtime in all Unicode strings are, therefore, should be called managed code before all the components in the string converted to Unicode format. In addition, a processed any COM object or local resources to release them. In this way, other requests will be able to use them, and to minimize the result of a later request for the release of their garbage collection caused by performance problems.

12. Visual Basic. NET or JScript. Code used in the early binding

Previously, developers prefer to use Visual Basic, VBScript. And JScript. One of the reasons is their so-called “no-type” in nature. Variables do not need explicit statement type, and be able to simply by using them to create. When from one type to another type of distribution, the conversion will run automatically. However, this would greatly facilitate the application of the damage to property. Now through the use of Visual Basic compiler Option Strict instructions to support the type of security programming. In order to backward compatibility, the default situation, ASP.NET does not enable this option. However, in order to get the best performance, strongly recommended that the opening page of this option. To enable Option Strict, please Strict properties included in the @ Page directive, or, for the user controls, the properties included in the @ Control directive. Here’s an example demonstrating how to set up the properties, and the four variables call to show how the use of the property caused compiler wrong.

<% @ Page Language = “VB” Strict = “true”%>

<%

Dim B

Dim C As String

‘This will cause a compiler error.

A = “Hello”

‘This will cause a compiler error.

B = “World”

‘This will not cause a compiler error.

C = “!!!!!!”

‘But this will cause a compiler error.

C = 0

%> Dim B

Dim C As String

‘This will cause a compiler error.

A = “Hello”

‘This will cause a compiler error.

B = “World”

‘This will not cause a compiler error.

C = “!!!!!!”

‘But this will cause a compiler error.

C = 0

%> JScript.. NET also supports a type of programming, but it does not provide mandatory early binding compiler directives. If any of the following occurred, the variable is the late binding: the explicit statement for the Object, is the type of statement the type of field is no explicit statement of the type of function or method for members, and not from Inferred from the use of type. A final difference is more complicated, because if JScript.. NET compiler can infer the use of variables to type, it will be optimized. In the following example, the variable A is early binding, but the variables B is bundled late.

Var A;

Var B;

A = “Hello”;

B = “World”;

B = 0; In order to get the best performance, when the statement JScript.. NET variables, for the distribution of a type. For example, var A: String.

13. Pipeline to the request of all modules as efficiently as possible

Pipeline request of all requests in each of the modules have the opportunity to be run. Therefore, when the request to enter and leave the module quickly trigger code is essential, especially in the non-use of functional modules in the path of the code. Were in use and not use the module and configuration files when the implementation of throughput testing, to determine the speed of the implementation of these methods is very useful.

14. HttpServerUtility.Transfer use the same method of application pages between redirect

Using Server.Transfer grammar, in the pages of this method can be used to avoid unnecessary redirect the client.

15. Adjusting, if necessary, support the application process for each of the threads

ASP.NET trying to structure the request of the threads in the execution of the request and the number of available resources to achieve a balance between. Known to use a sufficient CPU power applications, the structure will be used to request the CPU power to decide at the same time allow the implementation of the request. The technology known as the thread gated. However, under certain conditions, thread-gated algorithm is not very effective. ASP.NET Applications through the use of performance targets and associated Pipeline Instance Count performance counter, you can monitor PerfMon thread gated. When the pages call external resources, such as database access or XML Web services request, the page request and usually stop the release of CPU. If a request is waiting to be processed and have a thread pool thread is free, then that is awaiting the request will begin to be processed. Unfortunately, sometimes this may lead to Web servers at the same time there are a lot of requests and many are waiting for the thread, and they have an adverse impact on server performance. Usually, if the gating factor is the response time of external resources, by the excessive request for resources, the throughput of the Web server did not help. To alleviate this situation, you can change the Machine.config profile node maxWorkerThreads and maxIOThreads attributes, in the process of manually set the thread limit.

Note: The thread is used to support ASP.NET request, and the IO thread is used to come from documents, databases or XML Web services to provide data services. Allocated to the value of these properties is in the process of each type of threads per CPU the greatest number. For dual-processor computer, the maximum number is twice the value of the set. The four-processor computer, the maximum is four times the value of the set. In any case, there are four or eight computer CPU, the best change the default values. For one or two computer processors, the default can, but there are more computer processor performance, in the process of 100 or 200 thread is more harm than good. Note there are too many threads in the process will often reduce the speed of the server, because the context of additional operating system will lead to the exchange of CPU cycles spent on maintaining threads rather than on the handling of the request.

16. The proper use of common language runtime of garbage collection and automatic memory management

Careful not to over-allocation of memory to every request, because of this garbage for recycling will be more frequent and more work. In addition, not to allow unnecessary indicators point to object, object because they will remain active, and should try to avoid the object of Finalize method, in the back because they will lead to more work. Especially in the Finalize should never call in the release of resources, since resources in the garbage for recycling before recycling may have been consumed in memory. Finally this issue regular Web server environment on the performance of a devastating blow, because waiting for Finalize operation, it is easy to a specific depletion of resources.

17. If there is large-scale Web applications, could consider the implementation of pre-approved compiler

Whenever there directory on the first request will lease enforcement compiler. If the directory pages of analysis and have not been compiled, the bulk of this feature will be compiled and directory of all the pages in order to make better use of disk and memory. If it takes a long time, they will be quick analysis and compile a single page, so the request can be handled. This brings the benefits of ASP.NET performance, because it will compile many pages to a single assembly. From the loading process has been set to visit each page to load than a new set of procedures should be faster. Approved the shortcomings of the compiler: If the server to receive many pages have not yet been compiled at the request, then the Web server analysis and compile them, the performance may be lower. To address this problem, can be pre-approved the implementation of compilers. To do so, just in the application activated prior to its request for a page, no matter which page can be. Then, when the user first visit your site, pages and its procedures will be set has been compiled. There is no simple mechanism that can be awarded when a compiler. CPU has to be idle or wait until there are no more compiler process (for example, csc.exe (C # compiler) or vbc.exe (Visual Basic compiler)) start. Should also try to avoid changes to the application of the bin directory in the assembly. Changes will lead to re-page analysis and compiling the page, and replace the bin directory in the assembly will lead to completely re-approved compile the directory. In many pages contain the massive site, a better approach might be to replace the pages under the plan or the frequency of collection procedures to design a different directory structure. Rarely changes pages can be stored in the same directory and in the specific time for pre-approved compiler. Frequently changing pages in their own directory (the largest directory of hundreds of pages each) to quickly compile. Web applications can contain many subdirectories. Compile a directory of grant-in, rather than the application level.

18. Do not rely on the code of abnormal

Greatly reduced because of unusual properties, so you should not be used to control their normal program flow approach. If it is possible to detect the code could lead to the abnormal status, the implementation of this operation. Do not deal with the abnormal state before the capture itself. Common options include: check null, will be allocated to the analysis of figures for the value of the String a value, or in the application of math check the specific value. The following examples demonstrate the code may lead to abnormal test whether there is a certain state of the code. Taken produce the same results.

Try

(

Result = 100 / num;

)

Catch (Exception e)

(

Result = 0;

)

/ / … To this.

If (num! = 0)

Result = 100 / num;

Else

Result = 0;

19. HttpResponse.Write the use of methods of string Series

The method provides a very effective buffer and connectivity services. However, if you are running a wide range of connections, use multiple Response.Write call. Below example shows that the technical method than that used on a single call Response.Write connection string faster.

Response.Write ( “a”);

Response.Write (myString);

Response.Write ( “b”);

Response.Write (myObj.ToString ());

Response.Write ( “c”);

Response.Write (myString2);

Response.Write ( “d”);

20. Unless there are special reasons to close the buffer, or to keep it open

Disable the Web page form a buffer performance will lead to a lot of overhead.

21. Only when necessary, preserve server controls view state

Automatic state management is the view of the server control feature that can control the server from the process re-filled their property values (you do not need the preparation of any code). However, because the server controls the view state in a hidden form fields and from the server, so this feature does have an impact on performance. You should know that in view of the circumstances under which state will be helpful to the circumstances in which it affects the performance page. For example, if you will bind to each server control from the process on the data from the data will be bundled with the new operation was the replacement value of the view state of preservation. In this case, disable the view state can save processing time. By default, for all server controls view the opening of state. To disable the view state, you control the EnableViewState property to false, as in the following examples shown DataGrid server controls.

<asp:datagrid EnableViewState=”false” datasource=”…” runat=”server”/>

You can also use the @ Page directive to disable the entire state of the page view. When you’re not from the back page to the server, this will be very useful:

<% @ Page EnableViewState = “false”%>

Note: @ Control directive also support the EnableViewState property, the directive allows you to control whether the user controls for the opening of view state. To analyze a page on the server controls the use of view state the number of requests (by trace = “true” attribute included in the @ Page directive) the opening page of the track and see Control Hierarchy table Viewstate out. On track and how to activate its information, see ASP.NET track.

22. Server to avoid the unnecessary from the process

While you may wish to use more Web page form the framework for those who save time and the function of the code, but in some cases but it is not appropriate to use ASP.NET server controls and to deal with the incident. Usually, only to retrieve or store data, you need only to the server from the process. Most of these data operation in the process from the client on. For example, from the HTML form to authenticate a user can enter data regularly submitted to the server before the client. Usually, if there is no need to transfer information to the server to be stored in a database, then you should not result from the process of the preparation of the code. If you develop custom server controls, please consider allowing them to support the ECMAScript.’s Browser showed the client code. In this way, through the use of server controls, you can significantly reduce unnecessary information was sent to the number of Web servers.

Page.IsPostBack avoid the use of unnecessary from the implementation of the treatment process

If you’re prepared to deal with the server controls the handling of the code, it may sometimes be necessary in the implementation of the first page request of other code, not when the user send the page in HTML form, the implementation of the code. According to the page it is in response to the server control event generated.

Page.IsPostBack attribute the use of conditional implementation of the code

For example, the code below demonstrate how to create a database connection and order, the order for the first time at the request of the page will be data bound to the DataGrid server controls.

Void Page_Load (Object sender, EventArgs e)

(

/ / Set up a connection and command here.

If (! Page.IsPostBack)

(

String query = “select * from Authors where FirstName like ‘% JUSTIN%’”;

MyCommand.Fill (ds, “Authors”);

MyDataGrid.DataBind ();

)

)

As each request is the implementation of Page_Load event, the code inspection IsPostBack is set to attribute false. If yes, then the implementation of the code. If the property is set to true, not the implementation of the code. Note that if you do not run such checks, the back page of behavior will not change. Page_Load event in the implementation of the code before the implementation of the server control events, but only the server controls the outcome of events may be present in the output page. If we do not run the check, and will continue to Page_Load event of any page on the server controls the implementation of handling the incident.

23. When not using the conversation when the state banned it

Not all of the application or page to address the specific needs of the user’s conversation, you should be speaking on condition does not require any application or page disable session state. To disable the pages of conversation, and I should be grateful if @ Page directive in the EnableSessionState attribute set to false. For example:

<% @ Page EnableSessi%>

Note: If you need to visit the page session variables, but do not intend to create or edit them, they will be in the @ Page directive EnableSessionState attribute set to ReadOnly. Can also disable XML Web services approach the session state. For more information, please see the use of ASP.NET and XML Web services client to create the XML Web services. To disable the application of the conversation, please Web.config file in the application of the festival will sessionstate configuration mode attribute is set to off. For example:

<sessionstate Mode=”off” />

24. Conversation carefully chosen to provide state procedures

ASP.NET applications for the storage of the session data provided three different ways: the process of conversation within the state, as a Windows service, the process session state and SQL Server database in the process of session state. Each method has its own advantages, but in the process session state is by far the fastest solution. If only a small amount of storage in the session state easily stolen data, it is recommended that you use the process to provide procedures. The process of solution used across multiple processors or more computer scalable applications, or for servers or restart the process when the situation can not be lost data. For more information, please see the ASP.NET state management.

25. Not to use unnecessary Server Control

ASP.net, a large number of server-side controls to facilitate the development process, but also the possible loss of performance, because each user operating a server-side controls, and have a server-side and from the process. Therefore, the non-essential, it should be less use Server Control.

26. ASP.NET application performance test

In the ASP.NET application performance testing conducted before the application process should ensure that no mistakes, and function correctly. Specific performance test can use the following tools: Web Application Strees Tool (WAS) is Microsoft has released a free testing tool, can be downloaded from the http://webtool.rte.microsoft.com/. It can simulate hundreds or thousands of users at the same time the web application request for a visit, the formation of traffic on the server load and thus achieve the purpose of testing, can generate an average of TTFB, TTLB average performance such as the summary report. Application Center Test (ACT) is a test tools, Visual Studio.NET incidental to the Enterprise Edition, Microsoft is officially supported by web application testing tools. It can directly generate charts results, features more than WAS, but do not have many clients the ability to test at the same time. Server operating systems “management tool” in the “performance” counter, the server can be monitored to understand application performance.

Conclusion:

The site developers, in the preparation of the attention at the ASP.NET application performance issues, to develop good habits, improve application performance, can at least delay the necessary hardware upgrades, reduce the cost of site.