ASP.NET OPTIMIZATION

Download Report

Transcript ASP.NET OPTIMIZATION

ASP.NET OPTIMIZATION
Why Optimize?

$$$ Whether you build applications for
customers or not, enhanced applications
save money.
Optimize Your Web Applications

Improve your code
Research strategies, methods, techniques
 Revisit old code


Monitor your app
Out-of-Box
 Third party
 Create your own

String Manipulation

Use String Builders for modifying strings
 Mutable Value- can be modified





appending
removing
replacing
inserting characters
More than three changes
http://www.varay.com/Optimization/StringBuilder
Ex.aspx
Server.Transfer


HttpServerUtility.Transfer
Parameters


Path –URL as string
preserveForm





True- preserves QueryString and Form collections
False(default)- clears QueryString and Form collections
Terminates execution of the current page
Begins execution of a new page
Use when redirecting to another page within
same application
 Avoids needless client-side redirection
Option Strict & Option Explicit

For Visual Basic, VBScript and JScript
 Late binding


Option Explicit



expensive performance-wise
forces you to declare your variables/objects before
using them
set to “On” by default
Option Strict

requires you to explicitly declare datatype
conversions by disallowing late binding
Option Strict & Option Explicit
Project Properties- turn them “On”
 In Page or Control Directive ‘<%@ %>’


Strict="true"
Result- faster execution
http://www.varay.com/Optimization/Option
StrictEx.aspx

Avoid Exceptions
Exceptions cause performance to suffer
 Avoid relying on exceptions in your code
 Anticipate exceptions

checking for null
 check for specific values before applying
math operations

Debug Mode

Disable debug mode before you
deploy a release application or
 conduct performance measurements


Web.config <compilation>
Attribute- debug
 Option



true- Specifies compilation of debug binaries
false- Specifies compilation of retail binaries
Debug Mode

Visual Studio

When building change ddl
Debug
 Release


Examples .dll size
From 380kb to 364kb
 From 412kb to 392kb

Data Access
Open fewest possible connections
(connection pooling)
 Data Access Layer- between Business
Services and Data Services
 Make use of

automatic pooling
 automatic transaction handling

Data Access

Data Access Method
SQL Server-based data access method
provided by the .NET Framework
 Recommended for high performance


Stored Procedures
Additional performance boost
 Instead of ad-hoc queries

Data Access

Advantages to Stored Procedures

Database operations
Can be encapsulated in a single command
 Are optimized for best performance
 Provides additional security


ADO.NET Command object
Explicitly defines stored procedure parameters
 Access output parameters and return values

Data Access

Execute Scalar- value of the first column
of the first row of the result set
Retrieves a single value
 Examples- values returned by an aggregate
function

Count ( * )
 Sum ( Field )
 Avg ( Field )

Data Access

SqlDataReader- forward-only data
stream
higher performance than the DataSet class
 Tabular Data Stream ( TDS ) protocol
 IEnumerable interface

Instead of While Reader.Read()
 Databind()

ViewState
Enabled by default
 To disable set MaintainState property to
false


per control
<asp:datagrid EnableViewState="false“ … />

per page
<%@ Page EnableViewState="false" %>

per usercontrol
<%@ Control EnableViewState="false" %>
ViewState

Light Control Group
Label
 TextBox
 Button
 LinkButton
 ImageButton
 CheckBox
 RadioButton
 HyperLink

ViewState
Datagrid
 DataList
 Repeater
 CheckBoxList

Session State
Disable when not using per-user session
state
 Configure Session State

<%@ Page EnableSessionState="false" %>
 ReadOnly
 web.config file <sessionstate mode="off" />

Session State Provider
In-process session state
 Out-of-process session state as a
Windows Service
 Out-of-process session state in a SQL
Server database

Server Round Trips

Avoid unnecessary round trips to the
server
retrieving data
 storing data

Use HTML controls instead of ASP.NET
server controls
 Use appropriate postback event handling
 Use client side script for data
manipulation

Cache
Avoids overhead of retrieving information
from resources outside the application.
 Stores




page output or
application data
Stores data on the client or on the server
Cache
Output caching, which caches the
dynamic response generated by a
request.
 Fragment caching, which caches
portions of a response generated by a
request.
 Data caching, which allows developers
to programmatically retain arbitrary data
across requests

Cache
Sub Page_Load
Dim objItem As DictionaryEntry
For each objItem in Cache
lblContents.Text &= "<li>" & objItem.Key
lblContents.Text &= "=" &
objItem.Value.ToString
Next
End Sub
http://www.superexpert.com/default.aspx?id=371
String Manipulation
http://robz.homedns.org:8080/blog/archive/2004/10/07/173.aspx
http://msdn.microsoft.com/library/default.asp?url=/library/enus/cpref/html/frlrfsystemtextstringbuilderclasstopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/enus/cpref/html/frlrfsystemstringclasstopic.asp
Server.Transfer
http://msdn.microsoft.com/library/default.asp?url=/library/enus/cpref/html/frlrfsystemwebhttpserverutilityclasstransfertopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/enus/cpref/html/frlrfSystemWebHttpServerUtilityClassTransferTopic.asp
Option Strict & Option Explicit
http://authors.aspalliance.com/aspxtreme/
webapps/developinghighperformanceaspnetapplications.aspx
http://robz.homedns.org:8080/blog/archive/
2004/10/07/173.aspx
Avoid Exceptions
http://authors.aspalliance.com/aspxtreme/
webapps/developinghighperformanceaspnetapplications.aspx
Debug
http://msdn.microsoft.com/library/default.asp?url=/library/
enus/vsdebug/html/vxtskdebugmodeinaspnetapplications
.asp
http://authors.aspalliance.com/aspxtreme/aspnet/syntax/
compilationsection.aspx
http://robz.homedns.org:8080/blog/archive/2004/10/07/1
73.aspx
http://dotnetjunkies.com/WebLog/wayfarer/archive/2004/
09/30/27318.aspx
Data Access
http://www.aspfree.com/c/a/VB.NET/Buildi
ng-a-Robust-and-Highly-ScalableDistributed-Architecture-using-VB-NET/1
ViewState
http://webreference.com/programming/asp/
viewstate