Optimizing the performance of ASP programs

Source: Internet
Author: User
Tags connection pooling http request iis include connect odbc ole variable
Active Server pages enables dynamic pages to be executed quickly, but by adding skills to the connection to the database in your code, you can make your program more
Execute quickly. This is an article on how to refine the script and ASP features to achieve maximum speed. Anything in the user click button to appear on the screen results
Latency can leave users dazed, and for a commercial site, this means potential user losses.
Dimensions of ASP scripts


We may not be able to control the bandwidth of the user, but by optimizing the ASP site, we can actually achieve the best performance for the application. Many of the potential performance gains can be
To adjust through the system instead of changing the code.

Factors

First, what factors may affect the performance of an ASP program? Unfortunately, is there a lot of it? Here are just a few:

Limited bandwidth
Speed of server-side processors and other hardware
Other processes running on the server (for example, one of those OpenGL screen saver programs)
Database connection mode, connection pooling, or a separate database (such as Oracle, SQL Server, Access)
Language of Use
Stored Procedures and SQL
Using an precompiled component, not VB or Javsscript ASP
Good programming habits, such as dealing with errors
Some of these features are common to an experienced developer with good knowledge of IIS, but others can be very complex. In this article
In this article, let's look at how we can save milliseconds of time rather than trying to be exhaustive.

Does your ASP script look too long for the functionality you implement? This will affect the performance of the program from the beginning. For the collection of information and formatted output, ASP foot
This is useful, but the script is interpreted as a line, so the longer the code, the longer the execution time.

If you have a very long ASP script, how can you reduce the length? Here are some suggestions.

You can convert them to server-side components, in other words, create a visual Basic DLL file, or use any modern Windows path
Sequential languages and COM-compliant languages to establish an precompiled component and register on the server. For a tutorial on this feature, see
Http://www.webdevelopersjournal.com/articles/activex_for_asp.html. A well-written ActiveX component is not only a great way to mention
High speed, and it can greatly improve the protection of your software, especially if you are developing ASP programs for 3rd parties.

Because the script is executed on a line of lines, eliminating redundant scripts or creating efficient scripts can improve program performance. If you are on a single page
With hundreds of rows of ASP scripts, you may well be able to split the program into user, business, and data service sections. In fact, if you do, you'll find
Some extra code. For example, if you need to display a few tables, you can write a generic table display function so that you can call it where you need it
Out.

Another problem with dimensions is the length of the containing file. When a #include file is executed, the entire file is transferred, as if it were included in the
exists in the file. So, if you have a redundant inclusion file that contains a number of global methods and variable definitions, notice that they will be transferred into the
Each file that contains them, regardless of whether they are available or not. The ASP caches all the extension code, resulting in inefficient searches. In this case, the inclusion of the text
The pieces must be smaller. Although the containing file is processed by the server as a separate request, excessive use of #include can lead to an excessive download time.

<!--#include file= "header.asp"-->
<!--#include file= "footer.asp"-->
< SCRIPT language= "VBScript" runat= "Server" >

Sub Main ()
Writeheader
Writebody
Writefooter
End Sub

Sub Writebody ()
...
End Sub

Main ' Call Sub Main '
</script >

If the script is very long, note that you want to use the response.isclientconnected command, which will prevent the CPU from wasting running cycles when the client is no longer connected.

<%
' Check to ' if the ' client is connected
If not response.isclientconnected Then
' Still connected so proceed
Else
' Disconnected
End If
% >

Disperse ASP code and HTML language commands
Does anybody do that? When describing a table, switch between ASP and HTML, for example:

< HTML >
< BODY >
<%
Set myconn = Server.CreateObject ("ADODB. Connection ")
Mdbfilepath = Server.MapPath ("Sample.mdb")
MyConn.Open "Driver={microsoft Access Driver (*.mdb)}; Dbq= "& Mdbfilepath &";
Sql_query = "SELECT * FROM Friends"
Set RS = Myconn.execute (sql_query)
While not RS. Eof
% >
< LI ><%=rs ("Name"): < A href= "<%=rs (" Link ")% >" >Homepage</a >
<%
Rs. MoveNext
Wend
% >
</body >

Another example is using the IF command:

<%
If not session ("dbopen") Then
% >
< H1 >database not connected

<%
Else
% >
< H1 >database open

<%
End If
% >

In the above 2 examples, script performance improves performance by keeping ASP blocks on the server side and using Response.Write to generate HTML code.
Just like the following:

<%
If not session ("dbopen") Then
Response.Write "< H1 >database not connected

Else
Response.Write "< H1 >database open

End If
% >

For large and long scripts, you can see significant performance improvements through the above methods.
Session state

Undoubtedly, maintaining State in the ASP through session is a very important feature. However, this will affect the performance of the program. It is obvious that the extension of the server
Shrinkage because the session can only be limited to a single server and become a problem, a session for each user to consume resources.

If you do not use the session variable, and you may not actually need it, because you are using hidden form fields, storing values in a database, and using query characters
The string can complete the session function, at this time, should screen session state, the code is as follows:

@EnableSessionState = False

This way, the ASP will no longer check session information.

If you do rely on session state, avoid storing large amounts of data in the Session object. If you set up the HTTP Cookies for the client, that
The session in IIS is in effect, and the memory space allocated for each session is retained until the session is aborted or expired. Because of this
The reason is that when there are many concurrent users accessing the site, the resources will soon be depleted.


Database access

Accessing the database will slow down the program quickly, but there are no databases and many sites are not. Do not use the embedded SQL statements to access the database, you can
To access the database by using stored procedures, which will win great program performance, and is also very flexible. Using stored procedures, and with ActiveX
Data Objects (ADO) gives you maximum control over how stored procedures are exported.

Verify that the database is indexed because it directly affects the overall performance of the application. Also, be aware of running the UPDATE Statistics program on the database to
Better track the distribution of data. Note that some databases, such as Ms Access, are not suitable for enterprise applications, while SQL Server 7.0 or Oracle is better suited to
These.

Let SQL do the design work, including computing, adding, sorting, and grouping data. Do not write these features in other languages, you can write a query
To complete.

Any field you want to evaluate is the simple syntax shown below:

SELECT Count (*) from Publishers WHERE state= ' NY '

If you want to calculate a special field, you must group by that field, otherwise, you will not work:

SELECT count (city), publishers GROUP by city

Then determine the sorting method:

SELECT * FROM tablename WHERE FieldName >50 OR fieldname< to by FieldName2, FieldName3

You still use an ODBC system or file DSN to connect to the database, or you can connect using a quick OLE DB technology. Using the latter, when you move a Web file, do not
You need to modify the configuration again.

OLE DB is located between the application and the ODBC layer. In an ASP page, ADO is a program located on top of OLE DB. When you call ADO, you first send it to OLE DB, but
and send it to the ODBC layer. You can connect directly to the OLE DB layer, and when you do this, you will improve server-side performance. How do you connect directly to OLE DB?

If you use SQL Server 7, use the following code as the connection string:

strconnstring = "dsn= ';D river={sql SERVER};" & _
"UID=MYUID; Pwd=mypwd; "& _
"DATABASE=MYDB; Server=myserver; "

The most important parameter is the "driver=" section. If you want to bypass ODBC and use OLE DB to access SQL Server, use the following syntax:

strconnstring = "Provider=SQLOLEDB.1; Password=mypassword; "& _
"Persist security info=true; User Id=myuid; "& _
"Initial catalog=mydbname;" & _
"Data Source=myserver; Connect timeout=15 "

Why is this important?

Now you may wonder why learning this new connection method is critical? Why not use a standard DSN or System DSN method? Well, according to Wrox in his
The tests in the ADO 2.0 Programmer's Reference book, if using OLE DB connectivity, are better than using a DSN or dsn-less connection, the following Performance improvement table
Is:

Performance comparison
SQL Access
OLE DB DSN OLE DB DSN
Connection time: 18 82 Connection Time: 62 99
Repeat 1,000 time: 2900 5400 Repeat 1,000 record time: 100 950

This conclusion is published in Wrox's ADO 2.0 Programmer's Reference. Time is in milliseconds, the time to repeat 1,000 records is in the form of a server oil label
Calculated.

If possible, the user input is validated using client script to minimize the circular operation of the HTTP request.

If your browser has the ability to execute JavaScript or other scripts, be fully used to liberate server-side resources.

The following VBScript script runs on the user's browser side, checks the user information, and then submits it to the server:

< SCRIPT language= "VBScript" >
<!--
Sub Btnenter_onclick
Dim theform
Set theform = Document.myform
If IsNumeric (TheForm.Age.Value) Then
Theform.submit
Else
Msgbox "Please enter a numerical age."
End If
End Sub
-->
</script >

< FORM method= "POST" Name=myform action= "myfile.asp" >
Name: < INPUT typr= "text" name= "name" >
Age: < INPUT type= "text" name= "age" >
< INPUT type= "button" Name= "Btnenter" value= "Enter" >
</form >

Use local variables instead of global variables

Because local variables can be accessed quickly by the ASP script engine, the entire namespace does not have to be searched. Avoid redefining arrays, which are divided when the array is initialized
With enough size, it will greatly improve the efficiency. Doing so may waste some memory, but it does win speed, especially if the server has a lot of load.

Creating objects with < OBJECT > tags

If you need to refer to an object that might not actually be used, use the < OBJECT > tag to sample them instead of Server.CreateObject.
The latter causes the object to be created immediately, while < object > is created when the object is first referenced.

For example, the following code demonstrates using the < OBJECT > tag to create an application-scoped Ad Rotator Object instance:

< OBJECT runat=server scope=application id=myads progid= "MSWC. AdRotator ">
</object >
You can then refer to it on another page:

Open "Option Explicit"

In VB and VBScript, you can use variables without explicitly defining them. However, opening the "Define variables explicitly" option will help improve program performance.
Undefined local variables are slow because the entire namespace is searched before the variable is created to see if the variable already exists. So, please open
"Option Explicit", which is a good programming habit.

Careful with Server.MapPath

Do not use server.mappath! unless you have to. Conversely, if you know the path, you use an explicit literal path to represent it. Using MapPath, you will require
IIS to retrieve the current server path, which means a special request for IIS, which reduces program performance. The other way to do this is to work on the local
Variable, and when you need to use it, the server does not have to be overly requested.

Check How to Do

You can use the following tools to measure server performance: Task Manager,netmon and PerfMon. Using WCAT (WEB Capacity analysis
Tool) measure the capacity of the server. You can use WCAT to check how Internet Information services and network configurations respond to different client-side content, data, and HTML
The request for the page. The results of the tests can be used to determine how to optimize the server and network configuration.

By optimizing the performance of ASP applications, Web site applications will become very fast and ultimately: when not needed, there is no need to do performance-impacting clicks.

Summarize

When it comes to ASP performance, there are a number of aspects that have been discussed in this article. However, for each specific application, there should be separate considerations, and
Not all the techniques discussed here can be properly met.


Related Article

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.