
Philippe Chiasson
We've seen that mod_perl can make your CGI code run a lot faster, without making any changes to the source. Let's look at how we can make things even faster by making some code changes to optimise things.
First, use mod_status and ab for benchmarking. Comparing apples with apples is important. When you have mod_perl installed, you can use Apache2::Status, which is the mod_perl equivalent. No overhead and comes with the mod_perl package, so you already have it and there's no disadvantage to having it configured on a production server (assuming you don't allow the world to access it.)
A useful method of looking at memory is GTop for perl, which only works on Linux systems, but usually comes with Gnome.
use GTop;
my $gtop = GTop->new;
my $proc_mem= $gtop->proc_mem($$);
for( qw(size vsize share rss )) {
printf "%s => %d\n", $_, $proc_mem->$_();
}
PerlModule CGI PerlModule DBI ...
DBI->install_driver('mysql');
CGI->compile(':all');
use ModPerl::RegistryLoader() and call the handler($url, $filename) to pre-populate the Registry cache. Be very careful that the URL does match the file pointed at, otherwise you're putting dirty data in the cache and all bets are off. This means your script is in shared memory - so be careful, because if you change the code, every thread will have to reload the script and you'll have n copies of script in memory (where n is the number of threads running.) Of course, the obvious way of getting around this is to restart the server so that the cache is pre-populated with the updated code.handler() with the multiple URLs.use Apache2::Const qw(OK DECLINED); return OK;
use Apache2::Const -compile => qw(OK DECLINED) return Apache2::Const::OK
SetHandler perl-script means that lots of things are done for you, such as STDIN/STDOUT are tied, %ENV, @INC are saved/restored and %ENV changes are propogated. All of these things are done automatically, but if we don't need them (or are willing to work around them for performance gains) we can use SetHandler modperl which does none of these things. This means that it is NOT thread safe (since the environment is a per process value) and may leave HTTP environment variables on the environment. We have to be careful of security implications of this.PerlOptions AutoLoad # Default. Disabling means that you MUST preload everything. PerlOptions -GlobalRequest PerlOptions ParseHeader PerlOptions SetupEnv # Disable to give an almost empty environment.
Apache2::SizeLimit#startup.pl use Apache2::SizeLimit; $Apache2::SizeLimit::MAX_PROCESS_SIZE = 12000; $Apache2::SizeLimit::MIN_SHARE_SIZE = 6000; $Apache2::SizeLimit::MAX_UNSHARED_SIZE = 5000; $Apache2::SizeLimit::CHECK_EVERY_N_REQUESTS = 4; # httpd.conf PerlCleanupHandler Apache2::SizeLimitMore information on the sites mentioned in my earlier post on mod_perl.
