Friday, February 17, 2006

Waking up AFP in the background

I’m in the final stages of the development of a commercial website that was built using AFP. I’ve been really pleased with how well AFP has worked for this site and its performance. Because AFP is VFP, obviously its data handling capabilities are superb. I’ve found this to fit in really well with AJAX style applications because typically server side scripting is handling data intensive operations.

One caveat to using any VFP technology for server side scripting is its load time. So, of course, this applies to AFP. If AFP hasn’t done anything in a while, it shuts down. That means that when you hit an AFP page, AFP has to be loaded. AFP loads the VFP runtime and this seems to be by far the biggest bottleneck in its startup time. So basically the amount of time it takes to load AFP is roughly equivalent to the amount of time it takes VFP to load, AFP then has to process the page the user navigated to and then lastly, the page is returned to the browser. The end result of all this is that the first time a user hits an AFP page on your server (when AFP isn’t already running) is slow.

I decided to deal with this by stealing a few techniques from my AJAX functions. It’s pretty simple really, in the onload event of non AFP page (which is my home page), make an XMLHTTP call to open a dummy .AFP page in the background. This starts AFP while the user is viewing the page. By the time they click on a link to an AFP page AFP is already up and running and then the performance is of course, snappy.
I always have trouble getting code to format in the blog, but here it goes.
First, in the body section of your page add onload=”LoadAFP()”. This code tells your browser to execute this function after the page has loaded.

Now, if you want to avoid an error, you need a LoadAFP function. You can place it in a .js file and simply reference the file, which I’d recommend if you’re going to do this in more than one page or you can place it in the heading section of your page. Here’s the code for that, sans the script tags which Blogger will eat.

function LoadAFP() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
xmlhttp.open("GET", "http://www.f1tech.com/afploader.afp", true);
xmlhttp.send(null);
}

Basically this function just creates the xmlhttp object – which is complicated due to browser differences. In ie7 (currently in beta), it’ll be native to the browser like it is currently in modern Mozzilla based browsers, but it’ll be a long time before you can just count on it being present. Once you’ve got a reference to an xmlhttp object, the last two lines are one loads AFP in the background. Make sure you change the URL to something on your own server. Starting AFP on mine won’t help your performance. BTW, I didn’t include any code for afploader.afp because it can be any old AFP page. I didn’t test this, but I’d suspect it doesn’t even have to include and AFP script. AFP is invoked on the server whenever a request is made for a file with a .AFP extension.

I also played around with another variation of this that might be more appropriate if you're using AFP on your home page. Basically, the idea is to make a dummy homepage that just says something like "Server idle, loading website..." and then in your onload section just call a oneline javascript function that reads:

function LoadAFP() {
window.location="YourAFPHomePage.AFP"
}

This approach displays the server idle message for as long as necessary, then displays your home page. If AFP is already loaded it jumps to the homepage immediately.

Neither of these techniques take a lot of effort or are difficult to implement, but I think they can really make a difference for visitors to your website.

0 Comments:

Post a Comment

<< Home