Optimize your asp performance to the maximum extent

Source: Internet
Author: User

ASP can quickly execute your dynamic web pages, but you can also compress code and database connections to make them run faster. This is a detailed article on how to streamline code and ASP features for the fastest execution speed. For a dry user, any latency between pressing the user button and the result on their screen may mean that they will go to another site? If you are a commercial site, this may mean losing potential sales.
We have no way to control the user's bandwidth, but we can indeed achieve the best performance by optimizing the ASP site. Most of the potential performance improvement is through system changes rather than code tightening. an inappropriate idea is to ask the System Manager to upgrade the system once a system efficiency problem occurs.
First, which factors may affect ASP performance? Unfortunately, there are many factors? The following are only part of the list:

Available Bandwidth
Server processor and other hardware speed
Other programs running on the server (such as those OpenGL screen saver !)
Database connection mode, connection pool, and database system (for example, Oracle is better than SQL Server and SQL Server is better than access)
Language used
Stored procedures are superior to row-based SQL statements
Good ASP programming experience, such as error handling, using compiled components instead of VB or JavaScript.

Some of the above factors may have been noticed by developers with IIS knowledge and experience, but other factors may be very complicated for them. In this article, I will try to explain all the factors that affect ASP performance. Let's take a look at the main things we can do within milliseconds of shaving.

ASP script size
Is it longer than the required length when you use a webpage (or other pages? This is something that will reduce ASP performance at the beginning. ASP scripts are useful when used to obtain information and format output, but they are also interpreted and executed line by line. Therefore, the longer your script is, the longer it will be executed.
If your script is huge, how can we reduce the script length? Here are some suggestions:
You can convert them into server-side components, that is, to convert them into VB dynamic link library DLL or to uncompiled components through advanced windows programming language or appropriate COM interface language? And register them on the server. The Quick Guide can be found
Http://www.websitesjournal.com/articles/activex_for_asp.html. Compiling a well-written ActiveX component not only greatly improves performance, but also protects your software (scripts), especially when you publish your asp site on a third-party host.
Because scripts are interpreted and executed line by line, removing unnecessary scripts or creating more efficient scripts can improve performance. If you have hundreds of lines of code in a single ASP file, you may be able to well classify users, sales, and data services. In fact, if you do this, you may find some redundant code: If you need to output several tables, you can write a common function to output a table, just call it multiple times.
When talking about the ASP script size, you have to mention the file size. When you use an include file, the entire include file is loaded. When the include file is included, it is equivalent to writing the part of code in the ASP file itself. Therefore, if you define many common methods and definitions in a lengthy inclusion file, you must understand that when you include the file, whether you want to use each method or definition, it is fully loaded. ASP caches all the expanded code, which reduces the search efficiency. In this case, the contained files must be split into smaller, modular files. You must also understand that a page request containing a file is regarded as a separate page request by the server. using too many contained files will affect the 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? 'Calling Process main
</SCRIPT>

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

<%
'Check whether the client is still connected
If not response. isclientconnected then
'Still connected, Handler
Else
'Disconnected
End if
%>

Interspersing ASP and HTML
Does everyone do this? When outputting a table, we convert it between ASP and 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 the if statement is used:

<%
If not SESSION ("dbopen") then
%>
<H1> database not connected <%
Else
%>
<H1> database open <%
End if
%>

In these cases, the script performance can be improved by writing the server-side scripts together and using response. Write to generate HTML code. For example:

<%
If not SESSION ("dbopen") then
Response. Write "Else
Response. Write "End if
%>

In the case of large scripts and many scripts, you can see the performance improvement. Note that the <% flag is avoided here, so as to improve the performance, ASP does not need to calculate the ASCII code of characters when executing the script.

Session Status
In ASP, the ability to maintain a certain State through session is very powerful. However, it will affect your performance. Obviously, the scalability of your site becomes another problem if you restrict the use of sessions. However, sessions consume server resources for each user.
If you do not use the session variable, or do you actually not need to use it? Using a hidden form field to save data in the database, is it a trick to query strings? So you should disable the session status. You can use the following statement to disable session:

@ Enablesessionstate = false

In this way, ASP no longer checks session information.

If you have to rely on the session state, avoid storing a large amount of data in the session object. Sessions in IIS are maintained when the client's HTTP cookie is available. As a result, the session occupied by the session exists and is occupied until the session ends or times out. In this way, if many users use your program at the same time, your server resources may be exhausted.

Database Access
Is database access necessary? Accessing the database will drastically slow down your program, but it is clear that without a database, many sites will become worthless. However, by accessing the database through stored procedures instead of using Embedded SQL statements, you can improve the potential performance. By using stored procedures and ActiveX Data Objects (ADO), they are also flexible. Output data from stored procedures as much as possible.

Check that your database has an index, because this can directly improve the efficiency of your program. Similarly, try to run update statistics on your database server to help track your data distribution. In this way, your database can modify the query execution based on the information. Note that some databases, such as MS Access, can be accepted in enterprise-level programs? SQL Sever 7.0 or Oracle is a better bet.
Let SQL work as it is designed. It can count, join, sort, and group data. When you can write a query statement to do these things, do not use other languages.
Below is a simple 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), City from 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 oledb provider technology to connect to your database instead of using the DSN connection. You no longer need to ask your ISP (or database administrator/Network Administrator) to create a system DSN for you. You do not need to change the configuration when removing web files.

Oledb is between the ODBC layer and applications. On your ASP page, ADO is located on top of odedb ". Your ADO call is first sent to oledb and then to ODBC layer. However, you can directly connect to the oledb layer, and if you do so, you can see the performance improvement on the server side. However, how to directly connect to oledb?
If you use sqlserver 7, use the following connection code to connect to the database:

Strconnstring = "DSN =''; driver = {SQL Server };"&_
"Uid = myuid; Pwd = mypwd ;"&_
"Database = mydb; server = myserver ;"

The most important parameter is driver =. If you want to bypass ODBC and use oledb to connect to SQL Server (this 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 anything wrong?
Now you may feel a little strange: What are the key points of our new connection method? Why not use the standard DSN-less/system DSN path? Well, according to the test results of wrox in his book ADO 2.0 programmer's reference, if you use the oledb connection to compare with the DSN or DSN-less connection method, you will find the following improvements:

Performance Comparison:
SQL access
Oledbdsnoledbdsn
Connection time: 18? 82? Connection time: 62? 99
Time for querying 1,000 records: 29005400 time for querying 1,000 records: 100950

Note: This result can be found on pages 2.0 and 232nd of wrox's book ADO 233 programmer's reference. The unit of time is millisecond, and the query 1,000 record time is calculated by the server-side cursor (when the client cursor is used, the performance of oledb and DSN record set is slightly different ).

ASP decoding problems:
Try to confirm user input on the client to reduce the number of HTTP back-and-forth requests. If the browser supports JavaScript or other scripts, use their power to release more server resources.
The following VBScript runs in the client browser and is used to verify user information before it is submitted 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>

<Forpolichod = "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 faster accessed by the ASP script engine than global variables because you do not need to search the entire name field. Avoid changing the array definition. In the first initialization, it is more efficient to allocate enough space. In this case, you may waste some memory, but you get the speed advantage. This technology is obviously effective when the server load is heavy.

If you need to reference objects that are not necessarily used, you 'd better use the <Object> flag instead of the server. Createobject method. Using server. Createobject causes the object to be created immediately. Otherwise, the <Object> flag does not create the object immediately. If you do not use this object after using the <Object> definition, you will not waste resources.

For example, the following example uses the <Object> flag to create an application-scope ad wheel ad rotator object.
Example:

<Object runat = server scope = Application ID = myads progid = "mswc. adrotator">
</Object>

After the ad rotator object is stored in the application, you can use the following syntax to access the object on any program page.

<% = Myads. getadvertisement ("customerads.txt") %>

Turn on the 'option explicit 'switch. In VB and VBScript, you can use variables without explicit declaration. However, enabling this option can identify and define variables, which can write variables well and help improve performance. An undefined local variable slows down because the namespace must be searched before the variable exists. Get rid of it, so that each variable is clearly defined (first defined and then used ).

This is a good habit. It may trap typos, which is faster.

Unless you really need to use it, do not use the server. mappath method. If you know the real path, use the real path. To use mappath, you need IIS to retrieve the current server path, which means you have to send a special request to the server, which reduces the performance. Another method is to store the path to a local variable and use it as needed, so that the server does not need to perform multiple searches.

Check how you did it yourself
You can use tools to measure your system performance, such as the System Performance Monitor, NetMon, and perfmon. Wcat (Web capacity analysis tool) can be used to test web performance ). With 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 guidance for optimizing your server and network configurations. Wcat is designed to estimate the workload of Internet services (or Windows NT) and IIS in Windows 2000.
(Simulation ). For more information, see IIS Resource Kit ). There is also a lengthy wcat User Guide with a download link in the msdn online web sorkshop site. If you take your asp performance seriously, you must obtain this tool.

Strive to optimize application performance, and your web applications will run more smoothly. If not, do not affect the server performance.

Abstract:
The above information about ASP performance is introduced. There are many factors that affect ASP performance. Here we only discuss some of them. As the final idea, do not think that you can handle all the factors well. It is necessary to improve your asp performance. Each application you publish must be considered individually. All of the above techniques are not suitable for each ASP program.

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.