简体   繁体   中英

Are my unaltered CGI scripts actually faster with mod_perl?

My apache server previously used CGI scripts written in perl. I heard that using mod_perl is much faster than using standard cgi scripts, so I went about setting up mod_perl on my server. According to this page , all I needed to do in order for my CGI scripts to run under mod_perl was to edit my httpd.conf file. I added the following lines to the end of my httpd:

LoadModule perl_module libexec/apache2/mod_perl.so
<Location /cgi-bin>
    SetHandler perl-script
    PerlResponseHandler ModPerl::Registry
    Options ExecCGI
    PerlSendHeader On
    Order allow,deny
    Allow from all
</Location>

If I check for

exists $ENV{"MOD_PERL"}
in a CGI file, the MOD_PERL variable appears to be present. So I'm pretty sure I have mod_perl set up and running.

My question is: Does my CGI script experience a speedup? CGI脚本会加速吗? Is there anything I can do to speed it up even more? Or would any further work not really be worth it for the speedup? Does anyone have experience with this type of stuff?

I've been searching documentation and forums for an answer, but I can't really find anything. Thanks in advance for your help!

Yes, a completely unaltered CGI script will experience a speedup with mod_perl, using the ModPerl::Registry handler, as long as everything is configured correctly, and it appears that it is in your case.

The reason it is faster is that now, Apache will execute the script with the built-in Perl interpreter instead of launching a new interpreter for each request. Also, your scripts are cached and kept in memory, so Perl does not have to recompile them, reload all the modules that you use d, etc. You can even load them at startup with ModPerl::RegistryLoader, so that they are already cached and ready to get when the first visitor hits your page.

How can you make them even faster? One could write a book on that subject. You can start by moving code to modules instead of scripts. One could use the native mod_perl API, but I recommend using a framework that supports multiple architectures (most of them support all of: mod_perl, psgi/Plack, and FastCGI).

Caution, a major pitfall with ModPerl::Registry is that you can't close over a file-scoped my variable in a subroutine. It will cause the variable to be cached. (Turn warnings on, and if this happens, you will see a "$x will not stay shared" warning. You can either pass them to the sub as parameters, or change them to our variables.)

You don't have to load perl itself again. Any modules you load won't have to be loaded the second time around. The script itself is only compiled once. There are definitely savings.

Your question is partially answered in a similar question: Is CGI still slow when used with a compiled program that doesn't require a VM?

The linked answer and its reading material discuss the differences between CGI and FastCGI or other technologies (like mod_perl eg).

To paraphrase and expand upon the answer:
You will likely see a speedup, but it may be by a negligible amount. It depends heavily on your program's workload. Light workloads with lots of requests will likely see the greatest performance improvements.

When considering further optimization, first ask yourself if it is required. Plenty more reading on program optimization at http://en.wikipedia.org/wiki/Program_optimization

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM