Asp. NET 10 Effective performance optimization methods (from someone else)

Source: Internet
Author: User
Tags add numbers connection pooling

1. Database Access Performance optimization

Connection and shutdown of the database

Accessing a database resource requires creating a connection, opening a connection, and closing the connection several operations. These processes need to exchange information with the database multiple times to pass authentication, which consumes server resources more often. Asp. NET provides connection pooling (Connection pool) to improve the performance impact of opening and shutting down the database. The system places the user's database connection in the connection pool, takes out when needed, reclaims the connection when it closes, and waits for the next connection request. The size of the connection pool is limited, and if you still need to create a connection when the connection pool reaches the maximum, you will inevitably have a significant impact on performance. Therefore, after establishing a database connection only when the actual need to open the connection, the use is closed immediately, so as to minimize the database connection open time, to avoid the situation beyond the connection limit.

Using Stored Procedures

A stored procedure is a set of precompiled SQL statements stored on the server, similar to a batch file in a DOS system. Stored procedures have immediate access to the database and information processing is extremely rapid. You can use stored procedures to avoid multiple compilations of commands, and after execution, their execution plans reside in the cache, and you need to call the binary code in the cache directly later. In addition, stored procedures run on the server side, independent of the ASP. NET program, easy to modify, most importantly, it can reduce the database operation statements in the network transmission.

Refine query statements

Asp. NET ADO connections consume a considerable amount of resources, the longer the SQL statement runs, the longer it takes to take up system resources. Therefore, use optimized SQL statements as much as possible to reduce execution time. For example, do not include subqueries in query statements, make full use of indexes, and so on.

2. String Manipulation Performance optimization

Use the ToString method of a value type

When you connect strings, you often use the "+" sign to add numbers directly to a string. This method, while simple, can get the correct results, but because of the different data types involved, the numbers need to be converted to reference types by boxing to be added to the string. However, boxing operations have a large impact on performance because, when such processing occurs, a new object is assigned to the managed heap and the original value is copied to the newly created object. Use the ToString method of a value type to avoid boxing operations, which can improve application performance.

Using the StringBuilder class

The string class object is immutable, and the re-assignment of a string object is essentially a re-creation of a string object and assigning the new value to the object, and its method of ToString does not significantly improve performance. When working with strings, it is best to use the StringBuilder class, whose. NET namespace is system.text. Instead of creating a new object, the class directly operates on the string using methods such as Append,remove,insert and returns the result of the operation through the ToString method.

The definitions and operation statements are 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); Show operation result 3.

Optimize configuration files for Web server computers and specific applications to meet your specific needs

3. By default, the ASP. NET configuration is set to enable the widest range of features and is as adaptable as possible to the most common scenarios. As a result, application developers can optimize and change some of these configurations to improve application performance based on the functionality used by the application. The following list is some of the options you should consider.

Enable authentication only for applications that you need

By default, the authentication mode is Windows, or integrated NTLM. In most cases, for applications that require authentication, it is a good idea to disable authentication in the Machine.config file and enable authentication in the Web. config file. Configure the application according to the appropriate request and response encoding settings. The ASP. NET default encoding format is UTF-8. If your application is strictly ASCII, configure the application to use ASCII for a little performance improvement.

Consider disabling AutoEventWireup for your application

Setting the AutoEventWireup property to False in the Machine.config file means that the page does not match the method name to the event and hooks the two (for example, Page_Load). If page developers want to use these events, they need to override them in the base class (for example, you need to override page.onload for a page load event instead of using the Page_Load method). If AutoEventWireup is disabled, the page gets a slight performance boost by leaving the event connection to the page author instead of automating it.

Removes unused modules from the request processing pipeline.

By default, all the features of a node in the server computer's Machine.config file remain active. Depending on the functionality used by your application, you can remove unused modules from the request pipeline for a slight performance boost. Check each module and its capabilities and customize it as you want. For example, if you do not use session state and output caching in your application, you can remove them from the list so that requests do not have to execute each module's entry and exit code when no other meaningful processing is performed.

4. Be sure to disable debug mode

Always remember to disable debug mode before you deploy a production application or perform any performance measurements. If debug mode is enabled, the performance of the application can be very much affected.

5. For applications that rely extensively on external resources, consider enabling Web gardening on a multiprocessor computer

The ASP. NET process model helps enable scalability on multiprocessor computers, distributing work to multiple processes (one per CPU), and each process sets the processor relationship to its CPU. This technique is called Web gardening. If your application uses a slower database server or calls a COM object with external dependencies (only two possibilities are mentioned here), it is beneficial to enable Web gardening for your application. However, before you decide to enable Web gardening, you should test the application's performance in the network garden.

6. Cache data and page output whenever possible

ASP. NET provides a few simple mechanisms for caching page output or data when you do not need to dynamically calculate page output or data for each page request. In addition, you can optimize the performance of these pages by designing the pages and data requests that you want to cache, especially in areas where you expect to have a large amount of traffic in your site. Using caching appropriately compared to any Web forms feature of the. NET Framework can better improve the performance of your site, sometimes at an extra-order level. There are two points to be aware of using the ASP. First, do not cache too many items. There is a cost to caching each item, especially in terms of memory usage. Do not cache items that are easy to recalculate and seldom used.

Second, assign the cached item an expiration date that is not too short. Items that expire soon cause unnecessary turnover in the cache and often lead to more code cleanup and garbage collection work. If you are concerned about this issue, monitor the Cache total turnover rate performance counter associated with the ASP. NET Applications performance object. A high turnover rate may indicate a problem, especially when an item is removed before it expires. This is also known as memory pressure.

7. Select the data viewing mechanism that is appropriate for the page or application

There are often important tradeoffs between convenience and performance, depending on how you choose to display the data on a Web forms page. For example, a DataGrid Web server control might be a quick and easy way to display data, but it is often the most expensive in terms of performance. In some simple cases, it may be useful to render your data yourself by generating the appropriate HTML, but customization and browser orientation will quickly offset the extra benefits you get. Repeater Web server controls are a tradeoff between convenience and performance. It is efficient, customizable, and programmable.

8. Using the SqlDataReader class for fast forward-only data cursors

The SqlDataReader class provides a way to read a forward-only stream of data retrieved from a SQL Server database. The SqlDataReader class provides higher performance than the DataSet class if you are allowed to use it when you create an ASP. 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 the SqlDataReader class. For information about how ASP. NET accesses data, see Accessing Data through ASP.

9. Using SQL Server stored procedures for data access

In all of the data access methods provided by the. NET Framework, SQL Server-based data access is the recommended choice for building high-performance, scalable WEB applications. When you use a managed SQL Server provider, you gain additional performance gains by using compiled stored procedures instead of special queries.

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

By default, ASP. NET does not allow any STA COM components to run inside the page. To run them, you must include the Aspcompat=true property within the. aspx file in the @ Page directive. This switches the execution thread pool to the STA thread pool and makes HttpContext and other built-in objects available to COM objects. The former is also a performance optimization because it avoids any calls to marshal the multithreaded Unit (MTA) to the STA thread. Using STA COM components can greatly impair performance and should be avoided as much as possible. If you must use an STA COM component, such as in any interop scenario, you should make a large number of calls during execution and send as much information as possible during each invocation. Also, be careful not to create any STA COM components during the construction of the page. For example, in the following code,

When the page is constructed, the mystacomponent created by a thread is instantiated, and the thread is not the STA thread that will run the page. This can adversely affect performance because the marshaling between the MTA and STA threads must be completed to construct the page.

Dim MyComp as New mystacomponent () public Sub page_load () mycomp.name = "Bob" End Sub

The preferred mechanism is to defer the creation of the object until the preceding code is executed under the STA thread, as shown in the following example.

Dim MyComp public Sub page_load () MyComp = new Mystacomponent () Mycomp.name = "Bob" End Sub

It is recommended to construct any COM components and external resources when needed or in the Page_Load method. Never store any STA COM components in a shared resource that can be accessed by a thread other than the thread that constructs it. Such resources include resources such as cache and session state. Even if the STA thread calls the STA COM component, only the thread that constructs the STA COM component can actually serve the call, and this requires marshaling the call to the creator thread.

Asp. NET 10 Effective performance optimization methods (from someone else)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.