Tip: Maximize the performance of your ASP program

Source: Internet
Author: User
Tags array definition error handling execution connect odbc ole variables sort
Program | skills | performance | optimization

ASP can quickly execute your dynamic Web pages, but you can also tighten code and database connections to make them perform faster. This is a detailed article on how to streamline code and ASP features to get the fastest execution speed. For an impatient user, any delay between pressing the user button and appearing on their screen may mean that they will go to other sites? If you are a commercial site, this could mean losing potential sales.

We don't have any way to control the bandwidth of our users, but we do get the best performance by optimizing the ASP site. Most of the potential performance improvement is through system change rather than tightening code, an inappropriate idea is that once the system efficiency problems, the System Manager to make comments to upgrade the system.

First, which factor may affect the performance of the ASP? Unfortunately, there are a number of factors? Here are just a few of them:

Available bandwidth

Speed of processors and other hardware on the server

Other programs running on the server (like those OpenGL screen saver!)

Database connection mode, connection pooling, database system itself (Oracle better than SQL Server,sql Server better than Access)

The language you are using

Stored procedures are superior to line-SQL statements

Using compilation components instead of VB or JavaScript, good ASP programming experience, such as error handling

Some of the above factors may have been widely noticed by developers with knowledge of IIS, but others may be a very complex issue for them. In this article, we'll try to explain all the factors that affect ASP performance, and let's take a look at the main things we can do in a few milliseconds of shaving.

ASP script Size

Are you a script page (and other pages) longer than you must? This is something that will degrade the performance of the ASP at the start of the execution. ASP scripts are useful when it comes to getting information and formatting output, but scripts are also interpreted on a row-by-line basis, so the longer your script is, the longer it takes to execute it.

If your script is huge, what can you do to reduce the length of the script? Here are a few suggestions:

You can convert them to server-side components, that is, to make a VB Dynamic link library dll or convert it to an precompiled component through an advanced Windows programming language or the appropriate COM interface language, and register them on the server side. The quick Guide is available in

Http://www.webdevelopersjournal.com/articles/activex_for_asp.html find. Compiling a well written ActiveX component can greatly improve performance and protect your software (scripts), especially if you publish your ASP site on a third-party host.

Because scripts are executed on a row-by-line basis, eliminating redundant scripts or creating more efficient scripts can improve performance. If you have hundreds of lines of code in a single ASP file, you can do a good job of dividing users, trading and data services. In fact, if you do this, you might find some redundant code: If you need to output a few tables, you can write a generic function to output a table, just call it multiple times.

When you talk about the size of an ASP script, you have to mention the size of the included file. When you use a containing file, the entire containing file is loaded, and when the containing file is included, it is equivalent to writing that part of the code in the ASP file itself. So if you define a lot of common methods and definitions in a lengthy inclusion file, realize that when you include the file, whether you want to use every method or definition inside it, it's all loaded. ASP caches all of the unwind code, which reduces lookup efficiency in this case, the inclusion file must be split into smaller, modular files. Also understand that the inclusion file is considered a separate page request by the server, and using too many include files can affect download times.

!--#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 procedure Main
〈/script〉

If your script is lengthy, use response.isclientconnected. This means that your server CPU can avoid looping while the client is no longer connected to the server.

〈%
' Check if the client is still connected
If not response.isclientconnected Then
' Still connected, ' the handler
Else
' Disconnect
End If
%〉

Interspersing ASP and HTML

Everybody does this? When we output the table, we switch between the ASP and the HTML code, which is a bad habit. 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= "" 〉homepage〈/a〉
〈%
Rs. MoveNext
Wend
%〉
〈/body〉
〈/html〉

Another common example is when you use the IF statement:

〈%
If not session ("dbopen") Then
%〉
〈h1〉database not connected〈/h1〉
〈%
Else
%〉
〈h1〉database open〈/h1〉
〈%
End If
%〉

In these cases, script performance improves performance by writing server-side scripts together and generating HTML code with Response.Write. Like what:

〈%
If not session ("dbopen") Then
Response.Write "〈h1〉database not connected〈/h1〉"
Else
Response.Write "〈h1〉database open〈/h1〉"
End If
%〉

In the case of large scripts and a lot of scripts, you will be able to see performance improvements. Note that this avoids the use of the 〈% tag, which improves performance, and the ASP does not need to compute the ASCII code of the characters when executing the script.

Session Status

Undoubtedly, the ability to maintain a certain state through session in an ASP is a very powerful feature. However, it can affect your performance. Obviously, the scalability of your site becomes another problem, if you limit the use of sessions. However, session consumes server resources for each user.

If you don't use session variables, or in fact you don't have to use them? Using hidden form fields to save data in a database, is the query string a trick? So you should ban session status. You can use the following declaration to prohibit the use of session:

@EnableSessionState = False

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

If you have to rely on session state, you should avoid storing large amounts of data in the Session object. The session in IIS is persisted when the HTTP cookie of the client is available, causing the memory occupied by the session to be occupied until the session terminates or times out. This way, if many users use your program at the same time, your server resources may be depleted.

Database access

Database access is a must-have trouble? Accessing the database will slow down your program intensely, but obviously, without a database, many sites will become worthless. But by using stored procedures to access databases instead of embedded SQL statements, you can improve potential performance. They also have good flexibility by using stored procedures and ActiveX Data Objects (ADO). Output data from stored procedures whenever possible.

Make sure your database is indexed, as it can directly improve the efficiency of your program. Also, try to run Update statistics (update Statistics) on your database server to help track your data distribution so that your database can transform query execution based on that information. Note that some databases, such as Ms Access, are really acceptable in an enterprise-level program? SQL Sever 7.0 or Oracle is a better bet.

Let SQL work as it was designed, it can count (statistics), connect (join), sort (sort), and group data. When you can write a query to do these things, do not use the other language to implement.

The following is the simplest syntax for counting all columns:

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

If you count a specified column, you must use the GROUP BY statement to group the column, otherwise it will not work:

SELECT count (city), publishers GROUP by city

Data returned by Category:

SELECT * FROM tablename WHERE fieldname〉50 OR fieldname〈100 ORDER by FieldName2, Field Name3

Use ODBC or file DSN to connect to the database? Use the fast OLE DB provider technology to connect to your database instead of using a DSN connection. You no longer have to plead with your ISP (or database administrator/NMS) to create a System DSN for you, and you don't need to change the configuration when you remove the Web file.

OLE DB is between the ODBC layer and the application. In your ASP page, ADO is a "application" above odedb. Your ADO calls are first sent to OLE DB, which is then sent to the ODBC layer. However, you can connect directly to the OLE DB layer, and if you do, you can see server-side performance improvements. However, how do I connect directly to OLE DB?

If you use SQL Server 7, connect to the database using the following connection code:

strconnstring = "dsn= ';D river={sql SERVER};" & _

"UID=MYUID; Pwd=mypwd; "& _

"DATABASE=MYDB; Server=myserver; "

The most important parameter is the driver= part. If you want to bypass ODBC and use OLE DB to connect to SQL Server (which is a faster connection), 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 "

Is there something wrong here?

Now you might find it a little strange: what are the main points of our new connection method? Why not use the standard Dsn-less/system DSN approach? Oh, according to Wrox in his book "ADO 2.0 Programmer ' Reference" The results of the test show that if you use OLE DB connection and DSN or Dsn-less connection method comparison, you will find the following improvements:

Performance comparison:

SQL Access

Oledbdsnoledbdsn

Connection time: 18?82? Connection time: 62?99

Query 1,000 records time: 29005400 query 1,000 record time: 100950

Note: This result is available on pages No. 232 and 233 of Wrox's "ADO 2.0 Programmer's Reference" book. The unit of time is milliseconds, and the query 1,000 record time is computed from a server-side cursor (the difference between the performance of OLE DB and the DSN recordset is small when using a client cursor).

ASP decoding problem:

Reduce the number of HTTP round-trip requests as much as possible by confirming user input at the client. If browsers have the ability to support JavaScript or other scripts, use their power to free more server resources.

The following VBScript runs in the client browser and is used to authenticate user information before submitting to your 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〉
〈formmethod= "POST" name=myformaction= "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 to avoid using global variables. Local variables are accessed faster by the ASP script engine than global variables because the entire name domain is not searched. Avoid changing the array definition. It is more efficient to simply allocate enough size at the first initialization time. In this case, you may waste some memory, but you gain the advantage of speed. This technique is clearly available when the server is heavily loaded.

If you need to refer to an object that is not necessarily used, it is best to use the 〈object〉 tag instead of the Server.CreateObject method. Using Server.CreateObject causes objects to be created immediately, whereas 〈object〉 tags do not create objects immediately if you use the 〈object〉 definition and do not use the object, you will not waste resources.

For example: The following example is an Ad Rotator object that uses the 〈object〉 tag to create a Application-scope ad round

Cases:

〈object runat=server scope=application id=myads progid= "MSWC. AdRotator "
〈/object〉

After you store the Ad Rotator object into application, you can access the object in the following syntax on any program's page

〈%=myads.getadvertisement ("CustomerAds.txt")%〉

Open the ' Option Explicit ' switch. In VB and VBScript, you can use variables without an explicit declaration. However, this option can be used to identify variable definitions, so that you can write variables well and help improve performance. An undefined local variable slows because before it is established, the namespace must be searched before it knows if the variable exists. get rid of it and make each variable clearly defined (first defined and then used).

This is a good habit, it may trap typos, so much quicker.

Do not use the Server.MapPath method unless you really need to use it. If you know the real path, use the real path. Using MapPath requires IIS to retrieve the current server path, which means that you have to send a special request to the server, which means lower performance. Another approach is to store the path in a local variable and use it when it is needed so that the server does not need to be searched more than once.

Check out how you do it.

You can use tools to measure your system performance, such as System Performance Monitor, Netmon and Perfmon. Test Web performance can be WCAT (web Capacity analysis Tool). Using WCAT, you can test your IIS server and network configuration to respond to a variety of customer requests, data or HTML pages. These test results can be used as a guide to optimizing your server and network configuration. WCAT is specifically designed to estimate the amount of customer work that Internet services (or Windows NT) and IIS can respond to in Windows 2000

(simulation). For more information, refer to the IIS Resource Kit (Resource Kit). There are also lengthy WCAT user guides that have a download link in the MSDN Online Web Sorkshop site. If you are serious about your ASP performance, be sure to get the tool.

To optimize application performance, your Web application will run more smoothly. Do not affect the performance of the server if it is not really needed.

Summary: The above describes the ASP performance of the data, there are many factors affecting the performance of ASP, here only a part of the discussion. As a final thought, don't think you can handle all the factors, your ASP performance is still necessary to improve. Each application you publish must be considered individually, and all of the above techniques are not suitable for each ASP program.



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.