Asp.Net performance tuning tips summary

  • 2021-01-06 00:30:59
  • OfStack

Asp.net performance optimization techniques collected in this paper, for Asp.Net developers have a good reference value. The specific content is as follows:

1. Select the way to store session state

In Webconfig file configuration:


<sessionState mode="???" stateConnectionString="tcpip=127.0.0.1:42424" 
 sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" 
cookieless="false" timeout="20"/> 

ES14en. ES15en has three ways to store session state information:

1. Stored in process: property mode = InProc
Features: Has the best performance, the fastest, but can not be shared across multiple servers.

2. Stored in the status server: property mode = "StateServer"
Features: This method is used when user session information needs to be maintained across servers.
However, the information is stored on the state server. Once the state server fails, the information will be lost

mode="SqlServer"; mode="SqlServer"
Features: Workloads get larger, but information is not lost.

Added 1 point:
I. Because some pages do not require session state, it is possible to disable session state:
The following code looks like this:


<%@ Page EnableSessionState="false" %>

II. If the page needs to access session variables but is not allowed to modify them, you can set the page session state to read-only:
The following code looks like this:


<%@ Page EnableSessionState="false" %>

When using, you can choose a certain way according to the specific situation

2. Use Page. IsPostBack

When first run, it is not returned from the client, and its value is false. When triggering an event on the page or refreshing the page, the value of Page.IsPostBack changes to true because it is postback.

Page_Load = Page_Load = Page_Load


private void Page_Load(Object sender,EventArgs e)
{
if(!Page.IsPostBack)
{
   ....; // The code to initialize the page. These codes are regulated 1 Executed when the second page is initialized, when the 2 On the next postback, 
   // It will not be executed again. Improve efficiency.  
}
}

Many times you have to use ES67en because some controls need to remain in state after they are initialized.
For example, DropDownList, if initialized each time, will be initialized to default values regardless of the options selected by the user.

3. Avoid server controls

1.1-like static display information, try not to use the server control display, because the server control needs to send back to the server execution, will reduce the efficiency of program execution,1 common use < DIV > If server controls are used, remove: runat="server" to improve efficiency.

2. Disable the state view of server controls, some controls do not need to maintain their state, you can set its properties: EnableViewState=false;
If the entire page control does not need to maintain a state view, then the state vision for the entire page can be set to false:
The following code looks like this:


<%@ Page EnableViewState="false"%>

3. Configure Web.Config file:
ASP.NET Sessionss can be configured in the Sessionsstate element in Web.config or Machine.config.
Here is an example of the setup in Web.config:


<Sessionsstate timeout="10" cookieless="false" mode="Inproc" /> 

4. Avoid using DataGrid

Everyone knows that the DataGrid is powerful. But powerful at the same time, increased the cost of performance. Common use of other controls: DataList
Or Repeater control can be implemented, as far as possible without DataGrid.

5. String manipulation

1. Avoid packing operation because packing operation has low operating efficiency.
For example, run two code snippets:


string test="";
for(for int i=0;i<10000;i++)
{ 
  test = test + i;       
}

and


string test="";
for(for int i=0;i<10000;i++)
{ 
  test = test + i.ToString();       
}

The following code snippet is obviously more efficient. Because i is an integral type, the system needs to convert i into string by boxing, and then connect it. It takes time to
Readers can test Copy on their own machine 1.

2. Use the StringBulider class

For string concatenation: string str = str1 + str2 +.... ;
If there are more than three connections, it is better to use StringBuilder instead of String. StringBuilder can avoid creating String objects
The performance penalty.
1 a Sql statement used when used for assembly: StringBulider. Readers can test to 1.

3. Use them sparingly:


try
{}
catch
{}
finally
{}

Statement. This statement executes inefficiently.

6. ADO. Optimized Net usage

1. Database connection open and close. Open when you need to connect, and close the connection as soon as you have finished accessing the database.
For example, let's look at two code snippets:

Code 1:


DataSet ds = new DataSet();
  SqlConnection MyConnection = new SqlConnection("server=localhost; uid=sa; pwd=; database=NorthWind");
  SqlCommand myCommand = new SqlCommand(strSql,MyConnection);  
  SqlDataAdapter myAdapter=new SqlDataAdapter(queryStr,connectionStr);
MyConnection.Open();   // Open the connection 
 for(int i=0;i<1000;i++)  //for Loop simulates business logic operations before data is retrieved 
 {
  Thread.Sleep(1000);
}
myAdapter.Fill(ds);
for(int i=0;i<1000;i++)  //for The loop simulates the operation of the business logic after the data is obtained 
{
  Thread.Sleep(1000);
}
MyConnection.Close();   // Close the connection 

Code 2:


<%@ Page EnableSessionState="false" %>
0

II code is much better than I code, I early in the connection not put, if the user is a lot of words, prone to full connection pool situation. Serious crashes occur.

2. Database query

I. Generate SQL statements directly. Sql Server is compiled every time and does not provide a significant performance improvement. And it's not safe enough. Easy to attack.
SQL. Use the SQL command with arguments. Sql Server compiles it only once, and you can reuse the compiled command for different parameters. Improved performance.
III. Use Sql stored procedures. Compile once. Independent, easy to modify and maintain. One time can complete the function of sending multiple sentences. Reduced network traffic. Stored procedures are not necessarily more efficient than statements. If the business logic is complex, sometimes statements are more efficient than stored procedures.

7. Cache optimization

There are two types of caching: page caching and ES196en caching.

1. Use page caching and fragment caching:


<%@ Page EnableSessionState="false" %>
1

Duration is used to set the expiration time of Cache.
VarByParam is the setting of whether to change according to parameters. None is the setting of all parameters using the same 1Cache.
When TextBox1 is set, it is cached according to different values of TextBox1. When there are multiple parameters, the cache should be combined;

2. API cache. For use in an application

I. Examples of Cache usage:
https://www.ofstack.com/article/52399.htm

Page.Cache and HttpContext.Current.Cache: Page.Cache: Page.Cache: Page.Cache: HttpContext.

Page.Cache.asax. HttpContext.Current.Cache. HttpContext.Current.Cache. Page.Cache. HttpContext.Current.Cache. Page.Cache. HttpContext.Current.Cache
In some cases, since HttpContext is not available, HttpRuntime.Cache is used.


Related articles: