Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Experts ---> Performance Question 1

Status
Not open for further replies.

RushiShroff

Programmer
Jan 23, 2002
216
0
0
IN
Hi all,
well I know one web site- which is the premier online job site of India. It is built with ASP.

Now some time back, I had seen that that site is running very slow and for that I complained also.
Recently I have seen that the site works like anything.
So fast..I noticed that all pages (even dynamic ones !)have
.html extension with it ! I am sure that is the reason why it is so fast.

I want to know which is the technology which works behind
this ? How to convert ASP pages to HTML as there are tens of thousands of users.
So caching on the server could also be done for a single user.But what about multiple users ? What about users visiting site for the first time ? Rushi Shroff Rushi@emqube.com
"Life is beautiful."
 
I'm not so sure it's simply a matter of them using the html extension...more likely a number of other things they did to increase their performance. ie: coding better (to make the server(s) do less work), perhaps purchased more powerful servers, etc.

As far as making server side code work in html, I'm not sure exactly how they did that. I've never seen the need for it, so don't really know how to do it myself...however, that is something I'd be interested to find out if someone else knows.

They can also cache the pages within proxy servers...so, if the web server is too busy, the proxy server's can send the page to a user (at least from my understanding of it) which can spead up performance appearence of the web site, however the user will not necessarily be receiving the latest information.

Hopefully someone else will know a little more on this topic, because I'd like to hear a bit on it myself. :)
-Ovatvvon :-Q
 
Just a guess but they could be using ASP on the back end to write each html page to a directory and then add the link to another page.
 
Courtesy : 4GuysFromRolla.com/Scott Mitchell

Caching is the process of taking information that is time-instensive to collect and storing it in a location that requires less time to access. For example, in your office your desk serves as a cache of sorts - it's much quicker to work with documents on your desk than to have to walk over to the filing cabinet each time you need a new document.

In the ASP-world, some time-intensive tasks include, among other things, accessing a database. It is much, much faster in an ASP page to access an in-memory variable than to retrieve the value from a database. (This is due to the time-expensive steps that must be taken to work with a database - setting up the database connection, issuing the query, retrieving the results, etc.)

We can use application-level variables as a cache to store database results. Since application variables are accessible from any ASP page they are quite a logical choice for a caching system. A real-world example of caching can be seen on the ASPMessageboard. On the home page a list of the various messageboard forums is presented along with how many posts each forum has received in the current day and since the forum's existence. This information is retrieved from a database. Rather than querying the database each time someone visits the main page, I use application variables to cache the results of the database query. I also use an application variable to record at what time I last updated the cache with database results. Once that time is greater than 15 minutes, I invalidate the cache. That is, I rerun the database query and populate the application variables with the result of the latest database query.

With this caching technique in place, database hits on the ASPMessageboard homepage only occur, at most, once every 15 minutes. Previously, a database read occurred each time someone visited the home page. So, if 500 page views occurred on the home page in a 15 minute cycle, 500 database reads would have occurred. With the caching system in place, regardless of how many folks visit the home page in a 15 minute period, there will be only one database read! This illustrates the performance benefit inherent with caching!

To read about the technical specifics of using application variables in caching, be sure to read: A Real-World Example of Caching Data in the Application Object. That article examines the ASPMessageboard home page cachine discussed in this FAQ, a great read.

Also, for a thorough collection of caching-related article be sure to check out the Caching Article Index.

I (Scott Mitchell) recently tweaked the ASPMessageboard homepage so that the database values displayed are cached (as opposed to needing to make a call to the database each time the homepage is visited). The front page on the messageboard shows the number of posts for each forum in a given day... (If you're not familiar with the messageboard's front page, take a moment to check it out!).

Before my recent change, the front page made a database call each time it was viewed. The database call grabbed the number of daily posts and total posts for each forum (as well as for the entire messageboard). The front page is the ASPMessageboard's second most-visited page (after the ASP Q&A Forum), so I reasoned that I could remove a little bit of stress on the database by caching the various forum post counts as opposed to grabbing them every single page request.

For this project I decided to cache my data as application-level variables. There are a number of methods you can choose to use when it comes to caching data... there are third-party components avaiable, for example. In my opinion, using the Application object is the simplest and makes the most sense. (With ASP+ there is a built-in Caching object that you can use!)

As with any caching technique, you need to decide what event invalidates the cache. For this project, I decided to have it be time-based: if it had been more than 15 minutes since I last cached, I invalidated the cache, and hit the database, reloading the cache. To start, I needed an application-level variable to keep track of the last time the cache was created: DateCached. Second, I needed to initialize the variable in the Global.asa file's Application_OnStart event handler. Obviously, I need the cache to be invalid when the messageboard's front page is hit for the first time after a server restart. Therefore, I have the DateCached application variable initialized to one day prior to the current time:

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
'Initialize the DateCached application variable to
'one day prior to the current date/time
Application(&quot;DataCached&quot;) = DateAdd(&quot;d&quot;, -1, Now())
End Sub
</SCRIPT>




For more information on DateAdd check out this popular FAQ: How can I add or subtract time from a date? For more information on Global.asa be sure to read: Everything you Wanted to Know about Global.asa, but Were Afraid to Ask!

Now, in the front page for the messageboard (Default.asp), I needed to first determine if the cache was invalid (i.e., the difference between the current time and the Application(&quot;DateCached&quot;) variable was greater than 15 minutes). If it was, then I needed to hit the database and populate the various application-level variables with the results. If the cache was not invalid, then I simply needed to display the cached data.

To determine if my cache is invalid, I use this simple conditional:

If DateDiff(&quot;n&quot;, Application(&quot;DateCached&quot;), Now()) > 15 Then


This statement uses the VBScript DateDiff function to determine the time between the DateCached application variable and the current time. If this conditional is true (that is, the time between our last cache operation and the current time exceeds 15 minutes), then we need to hit the database and repopulate the cache like so:

Application.Lock 'Lock the application object

If DateDiff(&quot;n&quot;, Application(&quot;DateCached&quot;), Now()) > 15 then

'******* Cache is invalid!! *********
'We need to repopulate the cache...

'Make connection to database
Dim objConn
Set objConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
objConn.Open &quot;DSN=Foo&quot;

'Get the post Count
Dim objRS
Set objRS = objConn.Execute(&quot;sp_GetPostCount&quot;)

'Now, set the application level cache to the
'database results
Application(&quot;iTotal&quot;) = objRS(&quot;TotalPosts&quot;).Value
Application(&quot;iASP&quot;) = objRS(&quot;ASPPosts&quot;).Value
Application(&quot;iDatabase&quot;) = objRS(&quot;DatabasePosts&quot;).Value

' ... some code snipped for brevity ...

' **** VERY VERY IMPORTANT!! ****
'We must now update the time with which we last
'cached our data!
Application(&quot;DateCached&quot;) = Now()

End If

Application.UnLock 'Unlock the application object




Note that we are setting a slew of application-level variables to database results. These are displayed further down in our ASP page, where the HTML is outputted displaying the post counts for each forum for the day. Note that we don't need to do anything if the cache is valid... we already have the cached database results!

Note that we use the Lock method of the Application object before we check to see if the cache valid or not... furthermore, we UnLock the cache once we've completed this step. When the Application object is locked, no other concurrent users can access application-level variables until it is unlocked. Therefore, we prudently Lock the Application object before altering its values.

The Default.asp page concludes with the HTML output that integrates the various application-level variables:

... some HTML removed for brevity ...

<li><a HREF=&quot;/forum/asp.asp&quot;>ASP Questions and Answers</a>
(<%=FormatNumber(Application(&quot;iASP&quot;),0)%> new posts today<br>
<li><a HREF=&quot;/forum/ASPPlus.asp&quot;><b>Moderated</b> ASP+ Forum</a>
(<%=FormatNumber(Application(&quot;iASPPlus&quot;),0)%> new posts today<br>
<li><a HREF=&quot;/forum/databases.asp&quot;>Database (ADO/SQL/Access) Questions
and Answers</a> (<%=FormatNumber(Application(&quot;iDatabase&quot;),0)%>
new posts today, <%=FormatNumber(iDatabaseTotal,0)%> total!)<br>

... some HTML removed for brevity ...

<font size=2><i>
Last cached on <%=Application(&quot;DateCached&quot;)%>
</I></font>


Well, that about wraps it up! In this article we examined how to use the Application object to cache data. On several sites there's no real need to hit the database everytime, especially if the values change infrequently.... simply cache the data in application-level variables! All you need to do is decide how to invalidate the cache, and write a small bit of code!
Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
Courtesy 4GuysFromRolla.com

Speed up your ASP pages: Turn Them into HTML with ASPTear!

Let's face it: HTML is faster than ASP. Yes, we are hearing promises of compiled ASP+ code that will run a lot faster than today's ASP, but will it be faster than good old HTML? (To learn more about ASP+ be sure to check out our ASP+ Article Index.) As developers we often use ASP to connect to a database and display certain results. Whether we do it all with ASP, or with components it doesn't matter. The question on our minds is &quot;how long does it take to process?&quot;. A good example of this is a messageboard where for every 10 visits you get only one post. It makes more sense to change it from an ASP page which requests a visit to the database every time to a plain HTML page and update the HTML page only when there is a new post. In this article I will show you a way to improve some of your ASP pages by transforming them into pure HTML using the free component ASPTear. (To learn more about ASPTear be sure to read the previous 4Guys article: Grabbing Information from Other Servers.)

I will break down this example into 3 parts. In the first one I will show you how to use ASPTear to get the contents of another page, in the second part how to write the contents to an HTML file using the FileSystemObject, and in the third part I will put everything together using transactions to make sure they all get executed. (To learn more about the FileSystemObject be sure to take a moment and read the FileSystemObject F.A.Q..)

Part 1: Using ASPTear
The free component can be downloaded from where you can also find instructions on how to register it on your server and use it. I will summarize the use of it here though. To instantiate the component you write:

Set xObj = Server.CreateObject(&quot;SOFTWING.ASPTear&quot;)
strRetVal = xObj.Retrieve(StrUrl, nRequestType, _
strQueryString|strPostData, strUsername, strPassword)




This is a very simple component. It supports only one method: Retrieve. The rest here means:

Parameter Meaning
StrRetVal The HTML code returned back to us in the form of a string
StrUrl The complete URL of the page you want to get
NRequestType 2 for GET, 1 for POST
strQueryString|strPostData any information you need to post to a page to aid it in the processing, just like posting data from a form or using the URL to add a querystring.
strUsername/strPassword Log in to secured sites with username/password


The simplest form of this component could be something like this:

strRetVal = xObj.Retrieve(&quot;

or a more complicated one could be something like:

strPostData = &quot;Name=&quot; & Server.URLEncode(&quot;Christoph Wille&quot;) & _
&quot;&goto=&quot; & Server.URLEncode(&quot;strRetVal = xObj.Retrieve(&quot; _
strPostData,&quot;Evagoras&quot;,&quot;Charalambous&quot;)




Ok let's get to work! We'll be creating a procedure that will run on the server and will do all this for us. I call this procedure CreateHTMLPage for obvious reasons and it takes 2 parameters: getURL, the page to get the contents of, and postFile, the HTML file that it will output the results to.

In Part 2 we will look at writing the output of the ASP page to an HTML file. This section concludes with the part of the CreateHTMLPage subroutine that uses ASPTear to obtain the output

In Part 1 we described the goal of this article, to cache the output of an ASP page in an HTML file. We also discussed how to use ASPTear, a free commponent, to grab the output of an ASP page. In this part we'll look at saving this output to a text file.

Part 2: Writing to the HTML file
Keep in mind where we are at this point. We are in the processing page, the page that does all the database updates and so on. So, now we have a string with the HTML page returned to us. We need to write this to a plain HTML file, and if one is already there simply replace it. We use the FileSystemObject to accomplish this (To learn more about the FileSystemObject be sure to take a moment and read the FileSystemObject F.A.Q..):

Sub CreateHTMLPage(getURL,postFile)
'Continued from Part 1 of this article...
...

'write to file...
Dim objFile, objTStream
Set objFile = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objTStream = objFile.OpenTextFile(postPage, 2, True, 0)
objTStream.Write(strRetrieval)

'Clean up...
Set objTStream = Nothing
Set objFile = Nothing

End Sub
...




As you might recall the OpenTextFile method takes 4 parameters:

Parameter Meaning
postPage Relative or absolute path of the file you are writing/reading (absolute is usually faster)
2 Determines the I/O mode: 1 ForReading, 2 ForWriting, and 8 ForAppending
True A new file will be created if none exists (default is False)
0 Determines how to open the file: 0 for ASCII, -1 for Unicode, and -2 for the system default format)


(Interested in learning more about the OpenTextFile method? Be sure to read the technical documentation!)

At this point it might be a good idea to write all this script into a separate asp page and just &quot;include&quot; it in our processing page. Let's call this page by the subroutine name, CreateHTMLPage.asp. The completed code can be seen at the bottom of this article.

We are now ready for the final part of this article, putting it all together!


Read Part 3!


--------------------------------------------------------------------------------

Sub CreateHTMLPage(getURL,postFile)
'###################################################
'# change the variables below to match your site #
'###################################################

'replace &quot;getSite&quot; variable to the address of your site
'to get the complete URL of the active file you want to run
Dim getPage
getPage = &quot; & getURL

'replace &quot;postSite&quot; to the physical address of your site
'to make a composite physical address of the &quot;HTML&quot; file
'you want to write to
Dim postPage
postPage = &quot;C:\Inetpub\ & postFile

'##################################################
'# end changes #
'##################################################

'variables for &quot;ASPTear&quot; component
Const Request_POST = 1
Const Request_GET = 2

'initiate component &quot;ASPTear.dll&quot;
Dim TearObj
Set TearObj = CreateObject(&quot;SOFTWING.ASPTear&quot;)
Response.ContentType = &quot;text/html&quot;

'Note on above line: you can actually skip it if your asp file
'that you are calling has a similar declaration in its HEAD part,
'like:
'<meta http-equiv=&quot;Content-Type&quot;
content=&quot;text/html;
charset=iso-8859-1&quot;>

' to collect the output of the ASP page as a string
Dim strRetrieval
strRetrieval = TearObj.Retrieve(getPage, Request_GET, _
&quot;&quot;, &quot;&quot;, &quot;&quot;)

'in case of error getting the page, output error
'this is a direct copy from the documentation of ASPTear
If Err.Number <> 0 Then
Response.Write &quot;<b>
If Err.Number >= 400 Then
Response.Write &quot;Server returned error: &quot; & Err.Number
Else
Response.Write &quot;Component/WinInet error: &quot; & Err.Description
End If

Response.Write &quot;<b>
Response.End
End If

Set TearObj = Nothing


'write to file...
Dim objFile, objTStream
Set objFile = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objTStream = objFile.OpenTextFile(postPage, 2, True, 0)
objTStream.Write(strRetrieval)
Set objTStream = Nothing
Set objFile = Nothing
End Sub



Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
Very helpful stuff, Rushi.
penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top