Performance tuning tips

  Performance tuning tips have any common programming model performance defects, ASP.NET is no exception.    This section describes some can be avoided in the code performance bottlenecks in the way. 

  1. Not disable session state use: not all applications or page for each user based on the requirements of the session state.    If not, can be completely banned.    This can be achieved through the following page-level directive easily: 
  <% @ Page EnableSessionState = "false"%> 

  Note: If you need access to session variables page but not create or modify them, I would be grateful if value is set to command ReadOnly.    XML Web services can also provide ways to disable session state.    Please refer to XML Web services an object and the use of internal targets. 

  2. Carefully chosen session state to provide procedures: ASP.NET application for the storage of session data provided three different ways: in the process session state, as a Windows service, the process session state and SQL database in the process, 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 session state stored easily stolen data, the process should be used to provide procedures.    Process, the main solution for the Web and Web Farm Garden programme, or for the server / restart process can not lose data when the situation. 

  3. Server and avoid the excessive round-trip: Web Forms page framework is one of the best features of ASP.NET because it can significantly reduce the requirements for the completion of a task of the preparation of the code.    The use of server controls and event handling model to the page elements to programming is undoubtedly the most time-saving function.    However, the use of these features there are appropriate and inappropriate ways to understand when it is appropriate to use them is very important. 
  Application procedures are usually only in the data storage or retrieval of data from the server when necessary.    Much of the data can be operated round-trip between client.    For example, in the submission of data users, the client can usually in the form of certification.    Usually, if there is no need to relay information back to the server, not from the server. 

  If the preparation of their own server controls, please consider allowing them to a higher level (support ECMAScript) showed browser client code.    Through the use of "smart" controls, can significantly reduce the unnecessary Web server hits. 

  4. Page.IsPostback avoid the use of round-trip on the additional work: If back to the server controls usually need in the first page request to execute code, the code used in the event of different excitation round-trip code.    If the inspection Page.IsPostBack attributes, the condition code can be implemented, depending on whether there is a page on the initial request or on the server controls incident response.    This appears to be very obvious, but in fact can be ignored without changing the inspection of the page.    For example: 

  <script Language="C#" runat="server"> 

  Public DataSet ds; 

  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 (); 
  ) 
  ) 

  Void Button_Click (Object sender, EventArgs e) ( 
  String query = "select * from Authors where FirstName like '%% BRAD'"; 
  MyCommand.Fill (ds, "Authors"); 
  MyDataGrid.DataBind (); 
  ) 

  </ Script> 

  <form Runat="server"> 
  <Asp: datagrid datasource ='<%# ds.DefaultView%> 'runat = "server" /> <br> 
  <asp:button Onclick="Button_Click" runat="server"/> 
  </ Form> 

  <script Language="VB" runat="server"> 

  Public ds As DataSet 

  Sub Page_Load (sender As Object, e As EventArgs) 
  '… Set up a connection and command here … 
  If Not (Page.IsPostBack) 
  Dim query As String = "select * from Authors where FirstName like '%% JUSTIN'" 
  MyCommand.Fill (ds, "Authors") 
  MyDataGrid.DataBind () 
  End If 
  End Sub 

  Sub Button_Click (sender As Object, e As EventArgs) 
  Dim query As String = "select * from Authors where FirstName like '%% BRAD'" 
  MyCommand.Fill (ds, "Authors") 
  MyDataGrid.DataBind () 
  End Sub 

  </ Script> 

  <form Runat="server"> 
  <Asp: datagrid datasource ='<%# ds.Tables [ "Authors"]. DefaultView%> 'runat = "server" /> <br> 
  <asp:button Onclick="Button_Click" runat="server"/> 
  </ Form> 

  <script Language="JScript" runat="server"> 

  Public var ds: DataSet; 

  Function Page_Load (sender: Object, e: EventArgs): (void 
  / / … Set up a connection and command here … 
  If (! Page.IsPostBack) ( 
  Var query: String = "select * from Authors where FirstName like '%% JUSTIN'"; 
  MyCommand.Fill (ds, "Authors"); 
  MyDataGrid.DataBind (); 
  ) 
  ) 

  Function Button_Click (sender: Object, e: EventArgs): (void 
  Var query: String = "select * from Authors where FirstName like '%% BRAD'"; 
  MyCommand.Fill (ds, "Authors"); 
  MyDataGrid.DataBind (); 
  ) 

  </ Script> 

  <form Runat="server"> 
  <Asp: datagrid datasource ='<%# ds.DefaultView%> 'runat = "server" /> <br> 
  <asp:button Onclick="Button_Click" runat="server"/> 
  </ Form> 

  Page_Load event for all requests and, therefore, examined the Page.IsPostBack to Button_Click in dealing with the incident will not return to the implementation of a query.    Please note that even in the absence of the screening, page actions will not change, because the first query will be bundled in the procedures for handling incidents call DataBind overthrow.    Remember, in the preparation of the page will be very easy to lose sight of this simple performance improvements. 

  3. Cautious appropriate use of server controls: Although the server is very easy to use controls, but it is not always the best option.    Many cases, simple presentation of data binding or replacement can be completed the same thing.    For example: 
  <script Language="C#" runat="server"> 

  Public String imagePath; 
  Void Page_Load (Object sender, EventArgs e) ( 
  //… Retrieve data for imagePath here … 
  DataBind (); 
  ) 

  </ Script> 

  <% - The span and img server controls are unecessary …–%> 
  The path to the image is: <span innerhtml ='<%# imagePath%> 'runat = "server" /> <br> 
  <Img src ='<%# imagePath%> 'runat = "server" /> 

  In this case the server controls do not value will be sent back into the client the results of HTML.    In many other circumstances this method is also applied, even in the server controls template.    However, if we attribute programming operated controls, which had handled the incident or the preservation of its status, then the server controls more appropriate.    Should check the use of server controls, and find the code can be optimized. 

  6. Avoid excessive server controls view state: automatic management is a state function, which can control the server round-trip in the value of re-filling them, and not requested by any code.    However, this feature can not be used arbitrarily, because the state is in control of the hidden form fields incoming and outgoing servers.    ViewState should understand that when help when not.    For example, if in each round-trip data will be bound to controls (such as the fourth version of the Data Grid examples below), do not require controls to safeguard its view state, because in any case will erase any re-fill data. 
  By default, the server controls for all the opening ViewState.    To disable it, you control the EnableViewState property is set to false, as in the example below: 

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

  Also in the page-level closed ViewState.    This is not from the back pages of very useful when, as in the example below: 

  <% @ Page EnableViewState = "false"%> 

  Attention, User Control Directive also support this property.    Analysis page to the server controls the use of view state, the opening track and Show "hierarchical structure of control" in the table "view state" out.    The tracking, and how it opened more information, please refer to the application-level track record function. 

  7. Use of the string connecting Response.Write: page or user controls on the use of the string connecting HttpResponse.Write methods.    The method provides a very effective buffer and connectivity services.    However, if you intend to implement a large number of connection, use the following examples of the method (that is, repeatedly calling Response.Write) to connect than just call a string Response.Write methods must be quick. 

  Response.Write ( "a"); 
  Response.Write (myString); 
  Response.Write ( "b"); 
  Response.Write (myObj.ToString ()); 
  Response.Write ( "c"); 
  Response.Write (myString2); 
  Response.Write ( "d"); 

  8. Code should not rely on the unusual: abnormal is a waste of resources, should be avoided as far as possible in the code.    No way to control conventional procedures as abnormal flow method.    If in the code would lead to the detection of abnormal conditions, it should do so and not wait until after the capture deal with the abnormal condition.    Common options include: inspection of space allocated to the analysis of the figures for the strings, or in the application of mathematical calculations checked before the specific value.    For example: 

  / / Consider changing this: 

  Try ( 
  Result = 100 / num; 
  ) 
  Catch (Exception e) ( 
  Result = 0; 
  ) 

  / / To this:: 

  If (num! = 0) 
  Result = 100 / num; 
  Else 
  Result = 0; 

9.
  In Visual Basic or JScript code used in the early binding: Visual Basic, VBScript and JScript One of the advantages is that they no type of characteristics.    Can only use them to create variables that do not need the type of explicit statement.    From one type to another type distribution, the same will be converted automatically.    This is both an advantage is shortcomings, because in terms of performance, advanced bundled Although very convenient but very expensive. 
  Visual Basic language is through the use of special Option Strict compiler directives to support type-safe programming.    In order to backward compatibility, not default under ASP.NET opening Option Strict.    However, in order to get the best performance, should be used in page attributes or use Strict Control instructions for the opening of Option Strict page: 

  <% @ Page Language = "VB" Strict = "true"%> 

<%

  Dim B 
  Dim C As String 

  'This causes a compiler error: 
  A = "Hello" 

  'This causes a compiler error: 
  B = "World" 

  'This does not: 
  C = "!!!!!!" 

  'But this does: 
  C = 0 

%>

  JScript also support non-type programming, but it is not mandatory to provide early binding compiler directives.    The following conditions are met late bind variables are: 

  Explicit statement was targeted. 
  No statement is the type of field. 
  Is no explicit statement of the type of dedicated functions / methods members, and can not be inferred from the use of type. 
  The last characteristic is very complicated.    If JScript compilers can use variables under way will be inferred from the type optimization.    In the following example, for early bind variables A, B for the late bind variables: 

  Var A; 
  Var B; 

  A = "Hello"; 
  B = "World"; 
  B = 0; 

  For best performance, you JScript statement for a variable type.    For example, the "var A: String." 

10.
  COM will be a large number of calls for managed code components transplantation:. NET Framework a very easy way with the traditional COM component interoperability.    The advantage is in the retention of the existing code at the same time take advantage of new platforms.    However, in certain circumstances, to retain the old components of the excess of the cost of components will be moved to managed code costs.    Each case is very special, and decided to change the content requirements is the best way to measure site performance.    However, usually COM interactive performance and the impact of the number of function calls or letters from non-managed code to managed code proportional to the volume of data.    As to communications between the several layers, with the need to call a large number of interactive components called "chatty."    This component should be considered for transplantation fully hosted code to the. NET platform provides performance gains benefit.    Or may consider redesigning components, fewer requests to call or a Marshaling more data. 

11.
  SQL stored procedure will be used for data access:. NET framework provides all the data access method, based on SQL data access is the best generation of scalable Web applications the best choice.    Using SQL to provide care procedures can be compiled through the use of the storage process and not special enquiries, the additional performance improvement.    SQL stored procedures relating to the use of examples, please refer to the Guide to the server-side data access one. 

12.
  SqlDataReader use of read-only data access to fast forward cursor: SqlDataReader object database from SQL to retrieve data provide read-only cursor forward.    If SqlDataReader suited to your situation, it is a better choice than the DataSet.    SqlDataReader because IEnumerable interface support, and even can be bundled server controls.    Examples of the use of SqlDataReader, please refer to the Guide to the server-side data access one. 

13.
  Cache data and output as much as possible: ASP.NET programming model provides a simple mechanism, the need for dynamic calculation for each page request output or data cache them.    In the design of the page cache can be considered to optimize the application of those expected to have the greatest traffic areas.    Appropriate use of cache sites can enhance the performance, and sometimes can be increased by one order of magnitude or more, it is. NET Framework any other function uncontested.    On how to use the cache more information, please see the tutorial services a cache. 

14.
  Multiprocessor computer for the opening of Garden Web: ASP.NET process model in the multi-processor computer help enable scalable, will be distributed to the various work processes (1 per CPU), and each process will be between processors settings for CPU.    The technology is called Web gardening, it can significantly improve the performance of some applications.    To learn how to gardening Web, please refer to the use of a process model. 

15.
  Do not forget disable debugging mode: ASP.NET configuration of the control section <compilation> Application procedures are compiled in debug mode.    Debugging mode serious degradation.    Application procedures for the deployment of production or performance measurement before, and always remember disable debug mode.    The debug mode more information, please refer entitled SDK debugger chapters. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags: ,

Releated Articles


0 Comments to “Performance tuning tips”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.