Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Improving Perl Speeds in Apache

by Superlman (Pilgrim)
on Sep 15, 2001 at 00:17 UTC ( [id://112527]=perlquestion: print w/replies, xml ) Need Help??

Superlman has asked for the wisdom of the Perl Monks concerning the following question:

Yet another quickie:

What tweaking would you suggest to make Perl run faster in Apache? (Or, more specifically, dynamic content produced through Perl, on an Apache server)

(It's worth noting that Perlmonks loads faster (including download times) to my box than some of my own site (running off said box) does, even though Perlmonks pages are significantly more complex. Obviously, I'm doing something horribly, dreadfully wrong. )

Replies are listed 'Best First'.
Re: Improving Perl Speeds in Apache
by mitd (Curate) on Sep 15, 2001 at 00:35 UTC
    First I assume that you are using Perl, as is most common, to create CGI scripts.

    This being the case you can start by installing mod_perl and begin by using the bundled Apache::Registry module which allows to run almost all legacy perl CGI scripts unaltered from within the mod_perl environment.

    The speed enhancement is accomplished in two important ways:

    1. mod_perl 'pre-loads' perl.
    2. each apache process or child will only compile the script once

    CGI execution speed increases of 400-1000% have been observed.

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

      Plus, if there's a database involved, use shared database handles (Apache::DBI is useful for this). Oracle suggest this can make an order of magnitude difference in speed.

      Andy.

Re: Improving Perl Speeds in Apache
by blakem (Monsignor) on Sep 15, 2001 at 00:28 UTC
    Assuming you are already using mod_perl (if not, that would probably be my first suggestion unless you are using something like FastCGI or SpeedyCGI....) here is the mod_perl tuning guide.

    I've found that the slowest part of many dynamic web sites is the interaction with the database. Optimizing your SQL queries and database setup tends to be time well spent....

    -Blake

Re: Improving Perl Speeds in Apache
by perrin (Chancellor) on Sep 15, 2001 at 00:23 UTC
    Use a persistent Perl engine like mod_perl, FastCGI, or SpeedyCGI. Read the performance tuning documentation for mod_perl. Profile your code, find the slow parts, and fix them.
Re: Improving Perl Speeds in Apache
by oneiros (Scribe) on Sep 15, 2001 at 06:26 UTC

    It's a combination of several things. Here's the biggies.

    • Using mod_perl
    • Implementing your program(s) as an apache handler or Apache::Registry
    • Pre-loading all modules
    • Caching all needed data and expensive queries (this really helps)
    • Optimal SQL queries if you're using some type of database
    • General coding efficiency (writing tight code, using references, optimising accessor functions, etc.)
    • If you're returning huge pages, mod_gzip really helps cut down on transfer time
    • Tuning apache's MinSpareServers, MaxSpareServers, etc
    • Use tt2 instead of HTML::Template. It's great, and the new XS-based Template::Stash is spiffy

    Hope this helps.

Re: Improving Perl Speeds in Apache
by maverick (Curate) on Sep 15, 2001 at 05:06 UTC
    Along with other's suggestions, take a look at a kernel level httpd for static content (Linux has a nice one), or move it to another server and reference it like <img src="http://images.foo.com/gif1.gif">.

    Just decreasing the number of things that Apache, or the same machine has to serve, will help your performance and scalability.

    Also, always including the width and height attributes in your image tags will help the 'percieved' speed at which the page loads, because if the browser knows the size of the images it can render the page while it is downloading the images.

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

      Along with other's suggestions, take a look at a kernel level httpd for static content (Linux has a nice one)...

      Right on Maverick! I had forgotten about user-space server which is strange since we just started using it recently.

      One can find detail here KHTTPd.

      mitd-Made in the Dark
      'My favourite colour appears to be grey.'

Re: Improving Perl Speeds in Apache
by Ven'Tatsu (Deacon) on Sep 15, 2001 at 10:29 UTC
    While mod_perl and the like are great and all I would really strees looking at the speed the code it self exicutes.
    In most cases the server process will not send any data, not even the HTTP responce headders, until all data from the CGI process has been read.
    That means a pourly writen loop, even after most of the page has been sent to the server process, can chew up valuable seconds, especaly if you have any SQL querys inside a loop.
Re: Improving Perl Speeds in Apache
by toma (Vicar) on Sep 15, 2001 at 23:04 UTC
    Often the problem with CGI program speed has to do with print statement slowness. See Re: Perl CGI and SSI speed. Other articles in that thread suggest many other useful speedups.

    Benchmarking is a great way to find problems in the speed of your code. If you benchmark correctly, it is easy for others to help you speed up critical sections of your code.

    It should work perfectly the first time! - toma

Re: Improving Perl Speeds in Apache
by drifter (Scribe) on Sep 17, 2001 at 09:57 UTC
    You might consider about taking a look at HTML::Mason, from CPAN or http://www.masonhq.com, which is a mod_perl related dynamic content module. It might seem a bit awkward at the beginning but the speed increase is nice, it has it's own caching magic of components.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://112527]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found