Tutorials to .com

Tutorials to .com » Asp » Optimization » ASP applications to improve the processing performance of the string

ASP applications to improve the processing performance of the string

Print View , by: iSee ,Total views: 5 ,Word Count: 3509 ,Date: Fri, 17 Apr 2009 Time: 7:04 PM

asp applications to improve the processing performance of the string
James Musson
Developer Services, Microsoft UK
March 2003

Apply to:
Microsoft ® Active Server Pages ®
Microsoft Visual Basic ®

Abstract: The majority of Active Server Pages (ASP) applications to connect through to create a string to the user's HTML format. In this paper, several data streams to create the HTML method of comparison, in specific circumstances, some of the methods in the performance is superior to other methods. This article assumes that you already have a certain degree of ASP and Visual Basic programming knowledge.

Contents
Introduction
ASP Design
Connect string
Fast solutions
StringBuilder
Built-in method
Test
Results
Summary
Introduction
Preparation of ASP pages, the developer is to create a formatted text flow, provided through the ASP's Response object into the Web client. Create the text flow a number of means, and the method you choose will be the performance of Web applications and can have a significant impact on scalability. Many times, I help clients optimize their Web application performance, it was found that one of the more effective way is to change the way the creation of HTML flow. This article will introduce some commonly-used technology, and test them on a simple ASP page, the impact of performance.

ASP Design
Many ASP developers follow good software engineering principles, as much as possible of its modular code. This design usually includes the use of a number of documents contained on the pages for some of the specific format is not a function of generation. These functions output string (usually the HTML form code) can be through a variety of combinations to create a complete page. Some developers to improve this method, to HTML function to the Visual Basic COM components in the hope that full use of the code has been compiled to provide additional performance.

Although this design method is very good, but not for the creation of the composition of these components of the string HTML code of the method will be used by Web site performance and scalability impact, regardless of the actual operation is included in the ASP the implementation of the document or in Visual Basic COM components to perform.

Connect string
See the following code snippet WriteHTML function. Data parameter is called a string array, which contains a number of forms to be formatted as a data structure (for example, the data returned from the database).

Function WriteHTML (Data)

Dim nRep

For nRep = 0 to 99
sHTML = sHTML & vbcrlf _
& "<TR> <TD>" & (NRep + 1) & "</ TD> <TD>" _
& Data (0, nRep) & "</ TD> <TD>" _
& Data (1, nRep) & "</ TD> <TD>" _
& Data (2, nRep) & "</ TD> <TD>" _
& Data (3, nRep) & "</ TD> <TD>" _
& Data (4, nRep) & "</ TD> <TD>" _
& Data (5, nRep) & "</ TD> </ TR>"
Next

WriteHTML = sHTML

End Function

This is a lot of ASP and Visual Basic developers to create HTML code commonly used method. sHTML variables included in the text to return to the calling code, and then use Response.Write to write the client. Of course, this also can be expressed as directly embedded WriteHTML function does not contain a similar code of the page. The problem is this code, ASP and Visual Basic to use the string data types (BSTR, or Basic string) length in fact can not be changed. This means that changes when the length of the string, in memory of the original string that will be the destruction of form, but will also create a new string that contains the data that the new form: this will increase the allocation of memory and the lifting of the allocation of memory operation. Of course, ASP and Visual Basic have been for you to solve this problem, so the actual cost will not be immediately apparent. The distribution of the allocation of memory and memory requirements to lift the basic run-time lifting of the various special lock code, requiring a large number of overhead. When the string has become a large and a large memory to be allocated and rapid lifting of continuous distribution, this problem has become particularly evident, as in the connection string during large-scale case. While this issue is the impact of single-user environment, but in the server environment (for example, in the Web server running ASP applications), it will cause serious performance and scalability issues.

Now, we return to the above code fragment: This code should implement the distribution of the number of string operations? The answer is 16. In this case, the "&" operator for each application will lead to the meaning of the string variable sHTML be destroyed and re-create. As mentioned above, the distribution of spending a large string, and increases with the increase of the string, so we can improve the above code.

Fast solutions
There are two ways to mitigate the impact of connection string, the first method is to try to deal with reducing the size of the string, the second way is to try to reduce the number of operations allocated string. Please see the following code shown in the revised version of WriteHTML.

Function WriteHTML (Data)

Dim nRep

For nRep = 0 to 99
sHTML = sHTML & (vbcrlf _
& "<TR> <TD>" & (NRep + 1) & "</ TD> <TD>" _
& Data (0, nRep) & "</ TD> <TD>" _
& Data (1, nRep) & "</ TD> <TD>" _
& Data (2, nRep) & "</ TD> <TD>" _
& Data (3, nRep) & "</ TD> <TD>" _
& Data (4, nRep) & "</ TD> <TD>" _
& Data (5, nRep) & "</ TD> </ TR>")
Next

WriteHTML = sHTML

End Function

At first glance, it may be difficult to find the code and a code example of the difference. In fact, this code only after sHTML = sHTML & outside with the contents of the brackets. This is in fact by changing the order of priority, to reduce the string to connect the majority of operations to deal with the size of the string. In the first code example, ASP compiler will check to equate the right of expression, and calculated left to right. A result, each repeat every 16 to connect to do so, for these operations to sHTML growing. In the new version, we are prompted to change the operation of the order of the compiler. Now, it will by from left to right, from outside the brackets to the brackets of the order of the calculation expression. This technology allows each repeat, including 15 connecting operation, these operations will not increase for the smaller strings, only one is growing big sHTML. Figure 1 shows the optimization method of this connection method with the standard model in the memory usage comparison.



Figure 1: Standard connection with the increase in the use of brackets connected to the memory model comparison

In specific cases, the use of brackets can be performance and scalability have a very significant impact, after the text will explain this further.

StringBuilder
We have found a solution to the issue of string to connect fast-track approach, in most cases, this method can be invested to achieve the best performance and balance. However, If you want to further improve the performance of building a large string, the second approach is needed, that is, the allocation of operations to reduce the number of strings. To this end, the need to use StringBuilder. StringBuilder is a class for the string can be configured to maintain the buffer zone, buffer zone management to insert new text into fragments, and only in the text string is longer than the length of the string buffer to re-distribute. Microsoft. NET framework provides a free type (System.Text.StringBuilder), and suggested that in the environment to connect all the string operations use it. In traditional ASP and Visual Basic environment, we do not have access to such, it needs to create their own. The following is the use of Visual Basic 6.0 to create the StringBuilder type of sample (for the sake of brevity, to omit the error handling code).

Option Explicit

'Default initial buffer size and growth factor
Private Const DEF_INITIALSIZE As Long = 1000
Private Const DEF_GROWTH As Long = 1000

'Buffer size and growth
Private m_nInitialSize As Long
Private m_nGrowth As Long

'Buffer zones and buffer counter
Private m_sText As String
Private m_nSize As Long
Private m_nPos As Long

Private Sub Class_Initialize ()
'Set the size and growth of the default value
m_nInitialSize = DEF_INITIALSIZE
m_nGrowth = DEF_GROWTH
'Initialize the buffer zone
InitBuffer
End Sub

'Set the initial size and growth in the number of
Public Sub Init (ByVal InitialSize As Long, ByVal Growth As Long)
If InitialSize> 0 Then m_nInitialSize = InitialSize
If Growth> 0 Then m_nGrowth = Growth
End Sub

'Initialize the buffer zone
Private Sub InitBuffer ()
m_nSize = -1
m_nPos = 1
End Sub

'Increase the buffer zone
Private Sub Grow (Optional MinimimGrowth As Long)
'Initialize the buffer zone (if necessary)
If m_nSize = -1 Then
m_nSize = m_nInitialSize
m_sText = Space $ (m_nInitialSize)
Else
'Only growth
Dim nGrowth As Long
nGrowth = IIf (m_nGrowth> MinimimGrowth,
m_nGrowth, MinimimGrowth)
m_nSize = m_nSize + nGrowth
m_sText = m_sText & Space $ (nGrowth)
End If
End Sub

'Will be the buffer size used to adjust the size of the current
Private Sub Shrink ()
If m_nSize> m_nPos Then
m_nSize = m_nPos - 1
m_sText = RTrim $ (m_sText)
End If
End Sub

'Add a single text string
Private Sub AppendInternal (ByVal Text As String)
If (m_nPos + Len (Text))> m_nSize Then Grow Len (Text)
Mid $ (m_sText, m_nPos, Len (Text)) = Text
m_nPos = m_nPos + Len (Text)
End Sub

'Add some text strings
Public Sub Append (ParamArray Text ())
Dim nArg As Long
For nArg = 0 To UBound (Text)
AppendInternal CStr (Text (nArg))
Next nArg
End Sub

'Return to the current data string and adjust the buffer size
Public Function ToString () As String
If m_nPos> 0 Then
Shrink
ToString = m_sText
Else
ToString = ""
End If
End Function

'Clear the buffer and re-initialization
Public Sub Clear ()
InitBuffer
End Sub

The use of such basic principles is that in the category-level variables (m_sText) for the string buffer, and use the Space $ function in order to fill this buffer space characters to be set to a specific size. More text if you want to connect with the existing text, then check the size of the buffer zone sufficient to store the new text, use the Mid $ function to insert text in the correct position. ToString storage function will return the current text in the buffer zone, and the size of the buffer zone can be adjusted to accommodate the proper length of the text. The ASP code to use StringBuilder as follows:

Function WriteHTML (Data)

Dim oSB
Dim nRep

Set oSB = Server.CreateObject ( "StringBuilderVB.StringBuilder")
'With the size and growth factor to initialize the buffer zone
oSB.Init 15000, 7500

For nRep = 0 to 99
oSB.Append "<TR> <TD>", (nRep + 1), "</ TD> <TD>", _
Data (0, nRep), "</ TD> <TD>", _
Data (1, nRep), "</ TD> <TD>", _
Data (2, nRep), "</ TD> <TD>", _
Data (3, nRep), "</ TD> <TD>", _
Data (4, nRep), "</ TD> <TD>", _
Data (5, nRep), "</ TD> </ TR>"
Next

WriteHTML = oSB.ToString ()
Set oSB = Nothing

End Function

StringBuilder need to use a certain overhead, because every time when the use of such examples have to create it, and in the creation of the first instance of a class that must be included in such loaded DLL. StringBuilder additional examples of method calls when they need to spend. Bracketed use "&" method, StringBuilder how to depend on a number of factors, including the number of connections, it is necessary to build the size of the string and the choice of the initialization string buffer StringBuilder performance parameters. Please note that in most cases, the buffer space required is estimated at a slightly higher number of far better than to continue to grow.

Built-in method
ASP contains a very efficient way to create HTML code, just repeatedly call Response.Write. Write implicit function to optimize the use of the string buffer, the buffer can provide excellent performance characteristics. WriteHTML the revised code is as follows:

Function WriteHTML (Data)

Dim nRep

For nRep = 0 to 99
Response.Write "<TR> <TD>"
Response.Write (nRep + 1)
Response.Write "</ TD> <TD>"
Response.Write Data (0, nRep)
Response.Write "</ TD> <TD>"
Response.Write Data (1, nRep)
Response.Write "</ TD> <TD>"
Response.Write Data (2, nRep)
Response.Write "</ TD> <TD>"
Response.Write Data (3, nRep)
Response.Write "</ TD> <TD>"
Response.Write Data (4, nRep)
Response.Write "</ TD> <TD>"
Response.Write Data (5, nRep)
Response.Write "</ TD> </ TR>"
Next

End Function

Although the code is likely to provide us with the best performance and scalability, but has been undermined to some extent, packaging, because the internal code will function directly into the Response stream, it calls the loss of a certain code degree of control over it. In addition, the movement of this code (for example, into COM components) will become more difficult, because this function and the existence of flow-dependent Response.

Test
The four methods mentioned above through a simple ASP page (including a virtual data string array to provide a single form) was tested. We use the Application Center Test ® (ACT) from a single client (Windows ® XP Professional, PIII-850MHz, 512MB RAM) for a single 100Mb/sec network server (Windows 2000 Advanced Server, Dual PIII-1000MHz, 256MB RAM) to implement the test. ACT configured to use 5 threads, to simulate the five users to connect to the load site. Each test includes 20 seconds of warm-up time and the subsequent 100 seconds load time, load as much as possible during the creation of the request.

Change the main form through the repetition of the cycle frequency, the number of connections for different operation to repeat the test run, such as WriteHTML function as shown in the code snippet. Each test run using the above-mentioned four different methods of implementation.

Results
The following chart shows the number of ways throughout the application of the impact of throughput, as well as the ASP page response time. Through these charts, we can understand the request of the application to support the number of users waiting for pages to download to the time required for your browser.

Table 1: the use of the initials of the connection method that

Abbreviation method that
Response.Write method built-RESP
CAT standard method of connecting ("&")
PCAT bracketed method to connect ("&")
BLDR StringBuilder method

ASP in the simulation of a typical application workload, the test and the actual situation in this very different, from Table 2 we can clearly see that, even if repeated 420 times, this page is still not particularly large. Many complex ASP pages in these figures are relatively high, set up the scope of this test is likely to exceed the limit.

Table 2: Test sample of the page size and the number of connections

Connect with the number of repetitions page size (in bytes)
15,240 2,667
30,480 4,917
45,720 7,167
60,960 9,417
75 1,200 11,667
120 1,920 18,539
180 2,880 27,899
240 3,840 37,259
300 4,800 46,619
360 5,760 55,979
420 6,720 62,219



Figure 2: The throughput results of Figure

Chart from Figure 2 can be seen, as we expected, multiple Response.Write method (RESP) in testing the entire range of repeat test provides us with the best throughput. What surprises us is, the standard string connection method (CAT) so much of the decline, and the method of bracketed (PCAT) in the repeat performance of the implementation of 300 is still better than a lot of time. Repeat about 220 times, the string cache performance brought about by more than StringBuilder method (BLDR) inherent overhead, more than at this point in the use of ASP pages StringBuilder additional overhead required is worth it.



Figure 3: Response time results of Figure



Figure 4: CAT omit map of the response time results

Figure 3 and Figure 4 in accordance with the chart shows "to the first byte of the time" measured response time (in milliseconds). Because the standard string methods (CAT) to increase the response time too, it also provides a method not included in this chart (Figure 4), in order to analyze the differences between other methods. It is worth noting, multiple Response.Write method (RESP) and StringBuilder methods (BLDR) as the increase in the number of repeat show a similar linear growth, while the standard connection method (CAT) and processing methods in brackets (PCAT) in the more than a certain threshold after increasing rapidly.

Summary
This paper describes how the application of ASP environment to build a string of different techniques, these elements apply equally to all users of Visual Basic code to create a large string, for example, to create xml files manually. The following principles can help you determine which method best suits your needs.

Bracketed first try "&", especially when dealing with existing code. This method of minimal impact on the code structure, but you will find that application performance will be significantly enhanced, even beyond the intended target.
Without damaging the package level required to use Response.Write. Use this method to prevent unnecessary memory processing within the string, thereby providing the best performance.
StringBuilder to build truly large-scale use of or connected with a relatively large number of strings.
Although you may not see this kind of performance shown in this paper, the growth, but I have been in a real ASP Web applications to use these techniques, only a small additional investment will be scalable in performance and a great deal of respect improved.


Asp Optimization Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.