From CGI to mod_perl 2.0, Fast!




                                                                Philippe M. Chiasson
 ...
Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
Fast!
$> wget http://perl.apache.org/download/mod_perl-2.0-current.tar.gz
$> tar zxvf mod_perl-2.0-current.tar.gz
[...]
$>...
Faster!


 $> wget -O-       http://gozer.ectoplasm.org/talks/ApacheCon/2005/US/mod_perl-2.0-cgi-fast/fast.sh   | sh -x


...
Better!

 $> yum install mod_perl

 $> apt-get mod_perl

 $> fink install mod_perl

 $> pkg_add -r mod_perl

 $> emerge mo...
Thank You!




Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
Do we still have time?


                                    Let’s get serious!




Philippe M. Chiasson - http://gozer.ec...
You have

     • Apache 2.x
     • mod_cgi
     • A bunch of Perl CGI scripts
     • A working web site
     • A good job
...
Common Gateway
                 Interface

     • CGI is simple
     • CGI is easy
     • CGI just works


Philippe M. Chi...
How about mod_perl?


     • mod_perl is simple
     • mod_perl is easy
     • mod_perl just works


Philippe M. Chiasson ...
How about mod_perl?


     • mod_perl is simple
     • mod_perl is easy
     • mod_perl just works BETTER


Philippe M. Ch...
How about mod_perl?


     • mod_perl is simple
     • mod_perl is easy
     • mod_perl just works FASTER


Philippe M. Ch...
How about mod_perl?


     • mod_perl is simple
     • mod_perl is easy
     • mod_perl just works EASIERer


Philippe M. ...
But why switch?

     • CPU cycles
     • Memory
     • Servers
     • $$$
     • Apache::* module on CPAN

Philippe M. Ch...
The CGI model


     • Forking
     • Startup
     • Teardown


Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
The CGI model
     •    httpd fork()s

     •    httpd exec(/var/www/cgi-bin/myscript.pl)

     •    OS find #!/usr/bin/per...
The CGI model

                                            2x fork()
                                            2x exec()...
The mod_perl model


     • Embedded Perl
     • One time startup
     • One time teardown


Philippe M. Chiasson - http:/...
The mod_perl model


     •    Perl locates the correct subroutine to invoke

     •    Perl calls it




                ...
Getting there from here




Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
mod_cgi setup

 LoadModule cgi_module libexec/mod_cgi.so

 ScriptAlias /cgi-bin/ /var/www/cgi-bin




Philippe M. Chiasson...
mod_cgi setup
                        ScriptAlias is just shorthand
  LoadModule cgi_module libexec/mod_cgi.so

  Alias /c...
mod_cgi setup
 /var/www/cgi-bin/hello.pl
  #!/usr/bin/perl

  use CGI;

  my $q = new CGI;

  print $q->header('text/html'...
mod_cgi setup




Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
How fast is that ?

     • Apache Bench (ab)
     • Nice benchmarking tool
     • Will work for us nicely
     • Comes wit...
How fast is that ?

  $> ab -c1 -n50 http://localhost:8529/cgi-bin/hello.pl
  Benchmarking 127.0.0.1 (be patient).....done...
mod_perl time
$> wget http://perl.apache.org/download/mod_perl-2.0-current.tar.gz
$> tar zxvf mod_perl-2.0-current.tar.gz
...
mod_perl time

                                Download & unpack
$> wget http://perl.apache.org/download/mod_perl-2.0-curr...
mod_perl time

                                           Configure

$> /usr/bin/perl Makefile.PL MP_APXS=/usr/sbin/apxs
Co...
mod_perl time

                                    Build, test, install

$> make
$> make test
$> make install




Philippe...
mod_perl time

                                  Check your distro!
             mod_perl is supported by lots of them



...
mod_perl time
                                           httpd.conf
  LoadModule cgi_module modules/mod_cgi.so
  LoadModul...
mod_perl time


     • SetHandler
     • PerlHandler
     • ModPerl::PerlRun


Philippe M. Chiasson - http://gozer.ectopla...
mod_perl time

  $> apachectl restart
  [notice] Apache/2.0.52 (Unix) mod_perl/2.0.x Perl/v5.8.7 configured




Philippe M...
mod_perl time




Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
it works!


     • $ENV{MOD_PERL} = “mod_perl/2.0.x”;



Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
How fast is THAT ?

  $> ab -c1 -n50 http://localhost:8529/perlrun/hello.pl
  Benchmarking 127.0.0.1 (be patient).....done...
How fast is THAT ?
  mod_cgi
  $> ab -c1 -n50 http://localhost:8529/cgi-bin/hello.pl
  Benchmarking 127.0.0.1 (be patient)...
How fast is THAT ?
  mod_cgi
  $> ab -c1 -n50 http://localhost:8529/cgi-bin/hello.pl
  Benchmarking 127.0.0.1 (be patient)...
ModPerl::PerlRun


     • mod_perl’s bundled module
     • closest CGI emulation available


Philippe M. Chiasson - http:/...
ModPerl::PerlRun
       On every request for the script:
     •    Found

     •    Loaded

     •    Parsed

     •    BE...
ModPerl::Registry
                                           httpd.conf
 Alias /cgi-bin /var/www/cgi-bin
 Alias /perlrun /...
ModPerl::Registry




Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
How fast is THAT ?

  $> ab -c1 -n50 http://localhost:8529/registry/hello.pl
  Benchmarking 127.0.0.1 (be patient).....don...
How fast is THAT ?
 mod_cgi
 $> ab -c1 -n50 http://localhost:8529/cgi-bin/hello.pl
 Benchmarking 127.0.0.1 (be patient)......
How fast is THAT ?
 mod_cgi
 $> ab -c1 -n50 http://localhost:8529/cgi-bin/hello.pl
 Benchmarking 127.0.0.1 (be patient)......
ModPerl::Registry


     • mod_perl’s bundled module
     • Faster, but CGI emulation laking


Philippe M. Chiasson - http...
ModPerl::Registry
       On every request for the script:
     •    Load from file if:

          •   Not found in cache

 ...
ModPerl::(PerlRun|Registry)

     • Try ModPerl::Registry first
      • faster
      • might break some CGIs
     • Try Mod...
ModPerl::(PerlRun|Registry)


     • Simple Perl modules
     • Designed to run perl scripts from mod_perl
           pret...
package ModPerl::PerlRun;
             use base qw(ModPerl::RegistryCooker);

             sub handler : method {
        ...
CGI emulation


     • It’s an emulation
     • It ain’t the real thing
     • things can go wonky


Philippe M. Chiasson ...
I remember
   #!/usr/bin/perl

   use CGI;

   my $q = new CGI;

   print $q->header('text/plain');

   $counter++;

   pr...
#!/usr/bin/perl
                             I remember
  use CGI;

  my $q = new CGI;

  print $q->header('text/plain');
...
#!/usr/bin/perl
                             I remember
  use CGI;

  my $q = new CGI;

  print $q->header('text/plain');
...
That can’t be right?
   #!/usr/bin/perl

   use CGI;

   my $q = new CGI;

   print $q->header('text/plain');

   $counter...
That can’t be right?

     • Code is cached
     • Perl sticks around
     • Great for speed
     • Globals are suddently ...
That can’t be right?
   #!/usr/bin/perl

   use CGI;

   my $q = new CGI;

   print $q->header('text/plain');

   $counter...
That can’t be right?
   #!/usr/bin/perl

   use CGI;

   my $q = new CGI;

   print $q->header('text/plain');

   $main::c...
That can be right!
   #!/usr/bin/perl

   use CGI;

   my $counter;
   my $q = new CGI;

   print $q->header('text/plain')...
Globals will stick


     • Globals will stick around
     • Use my
     •   Try ModPerl::PerlRun




Philippe M. Chiasson...
CGI emulation


     • It’s an emulation
     • It ain’t the real thing
     • things can go wonky


Philippe M. Chiasson ...
I still remember
  #!/usr/bin/perl
                                                           $> GET http://localhost:8529...
But, what the..?

     • I used my so it’s not a global...
     • To Perl, all code lives in subroutines
     • mod_perl t...
subification       *




                                   #!/usr/bin/perl

                                   use CGI;
  ...
subification   *




       • subroutine needs a name
       • filepath is a convenient naming

  * the process of making a ...
subification         *




                                                /var/www/cgi-bin/hello.pl

         ModPerl::ROO...
sub
                              subification                  *




ModPerl::ROOT::ModPerl::Registry::var_www_cgi_2dbin_c...
closures
     • Closures are a Perl feature
     • Come to be when creating subs
     • Normally not an issue
     • mod_p...
I still remember
  #!/usr/bin/perl

  use CGI;                                             2 warning signs to look for:
  ...
I still remember
  #!/usr/bin/perl

  use CGI;
  use strict;
  use warnings;
                                             ...
Good advice

     • use strict;
     • use warnings;
     • avoid globals
     • look for hints in your error_log file

Phi...
CGI emulation


     • It’s an emulation
     • It ain’t the real thing
     • things can go wonky


Philippe M. Chiasson ...
I used to work
  #!/usr/bin/perl                                          #countlib.pl

  use CGI;                        ...
I used to work




 error_log:
  [Sun Oct 23 21:37:44 2005] [error] Can't locate countlib.pl in @INC (@INC
  contains: [.....
I used to work
  #!/usr/bin/perl

  use   CGI;
  use   strict;
  use   warnings;
  use   Cwd;

  my $q = new CGI;

  print...
I used to work


     • mod_cgi
      • fork()s
      • chdir()s to the script’s directory


Philippe M. Chiasson - http:/...
Cwd

     • mod_perl
      • doesn’t fork()
      • doesn’t chdir()s
      • could chdir()s

Philippe M. Chiasson - http:/...
Cwd

     • Cwd() is a process proprety
     • Apache threaded MPMs
     • chdir() isn’t thread-safe
     • mod_perl doesn...
Cwd


     • You can fix your scripts
      • use lib();
      • require “/fully/qualfied/lib/path.pl”;


Philippe M. Chiass...
Cwd


     • If your MPM is Prefork:
      • ModPerl::RegistryPrefork
      • ModPerl::PerlRunPrefork


Philippe M. Chiass...
It ain’t magical!


     • use strict/warnings
     • Think about globals
     • watch the error_log


Philippe M. Chiasso...
More info
     • mod_perl User’s mailing-list
          – http://perl.apache.org/maillist/modperl.html
          – <modper...
Thank You!

                          Slides and bonus material:

                     http://gozer.ectoplasm.org/talk/


...
Upcoming SlideShare
Loading in...5
×

From CGI to mod_perl 2.0, Fast!

243,910

Published on

Published in: Technology
1 Comment
5 Likes
Statistics
Notes
No Downloads
Views
Total Views
243,910
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
65
Comments
1
Likes
5
Embeds 0
No embeds

No notes for slide

From CGI to mod_perl 2.0, Fast!

  1. 1. From CGI to mod_perl 2.0, Fast! Philippe M. Chiasson gozer@ectoplasm.org Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  2. 2. Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  3. 3. Fast! $> wget http://perl.apache.org/download/mod_perl-2.0-current.tar.gz $> tar zxvf mod_perl-2.0-current.tar.gz [...] $> cd mod_perl-2.0.*/ $> perl Makefile.PL MP_APXS=`which apxs` Configuring Apache/2.0.52 mod_perl2/2.0.2 Perl/v5.8.6 [...] $> make cd quot;src/modules/perlquot; && make [...] $> make test $> make install $> perl -pi -e’s/cgi_module/perl_module/g’ `apxs -q SYSCONFDIR`/httpd.conf $> perl -pi -e’s/mod_cgi/mod_perl/g’ `apxs -q SYSCONFDIR`/httpd.conf $> perl -pi -e’s/cgi-script/perl-script/g’ `apxs -q SYSCONFDIR`/httpd.conf $> `apxs -q SBINDIR`/apachectl configtest Syntax OK $> `apxs -q SBINDIR`/apachectl restart Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  4. 4. Faster! $> wget -O- http://gozer.ectoplasm.org/talks/ApacheCon/2005/US/mod_perl-2.0-cgi-fast/fast.sh | sh -x Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  5. 5. Better! $> yum install mod_perl $> apt-get mod_perl $> fink install mod_perl $> pkg_add -r mod_perl $> emerge mod_perl Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  6. 6. Thank You! Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  7. 7. Do we still have time? Let’s get serious! Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  8. 8. You have • Apache 2.x • mod_cgi • A bunch of Perl CGI scripts • A working web site • A good job Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  9. 9. Common Gateway Interface • CGI is simple • CGI is easy • CGI just works Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  10. 10. How about mod_perl? • mod_perl is simple • mod_perl is easy • mod_perl just works Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  11. 11. How about mod_perl? • mod_perl is simple • mod_perl is easy • mod_perl just works BETTER Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  12. 12. How about mod_perl? • mod_perl is simple • mod_perl is easy • mod_perl just works FASTER Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  13. 13. How about mod_perl? • mod_perl is simple • mod_perl is easy • mod_perl just works EASIERer Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  14. 14. But why switch? • CPU cycles • Memory • Servers • $$$ • Apache::* module on CPAN Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  15. 15. The CGI model • Forking • Startup • Teardown Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  16. 16. The CGI model • httpd fork()s • httpd exec(/var/www/cgi-bin/myscript.pl) • OS find #!/usr/bin/perl in /var/www/cgi-bin/myscript.pl • OS fork()s • OS exec()s /usr/bin/perl • Perl opens, initializes and parses myscript.pl • Perl runs the script Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  17. 17. The CGI model 2x fork() 2x exec() Perl startup Perl shutdown EXPENSIVE Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  18. 18. The mod_perl model • Embedded Perl • One time startup • One time teardown Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  19. 19. The mod_perl model • Perl locates the correct subroutine to invoke • Perl calls it CHEAP er Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  20. 20. Getting there from here Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  21. 21. mod_cgi setup LoadModule cgi_module libexec/mod_cgi.so ScriptAlias /cgi-bin/ /var/www/cgi-bin Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  22. 22. mod_cgi setup ScriptAlias is just shorthand LoadModule cgi_module libexec/mod_cgi.so Alias /cgi-bin /var/www/cgi-bin <Location /cgi-bin> SetHandler cgi-script Option +ExecCGI </Location> Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  23. 23. mod_cgi setup /var/www/cgi-bin/hello.pl #!/usr/bin/perl use CGI; my $q = new CGI; print $q->header('text/html'); print <<quot;EOFquot;; <html><body> <h1>Hello world!</h1> <pre> GATEWAY_INTERFACE: $ENV{GATEWAY_INTERFACE} MOD_PERL: $ENV{MOD_PERL} </pre> </body></html> EOF Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  24. 24. mod_cgi setup Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  25. 25. How fast is that ? • Apache Bench (ab) • Nice benchmarking tool • Will work for us nicely • Comes with Apache Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  26. 26. How fast is that ? $> ab -c1 -n50 http://localhost:8529/cgi-bin/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 5.57 [#/sec] (mean) Time per request: 179.448 [ms] (mean) Transfer rate: 1.67 [Kbytes/sec] received Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  27. 27. mod_perl time $> wget http://perl.apache.org/download/mod_perl-2.0-current.tar.gz $> tar zxvf mod_perl-2.0-current.tar.gz [...] $> cd mod_perl-2.0.*/ $> perl Makefile.PL MP_APXS=`which apxs` Configuring Apache/2.0.52 mod_perl2/2.0.x Perl/v5.8.6 [...] $> make cd quot;src/modules/perlquot; && make [...] $> make test $> make install $> perl -pi -e’s/cgi_module/perl_module/g’ `apxs -q SYSCONFDIR`/httpd.conf $> perl -pi -e’s/mod_cgi/mod_perl/g’ `apxs -q SYSCONFDIR`/httpd.conf $> perl -pi -e’s/cgi-script/perl-script/g’ `apxs -q SYSCONFDIR`/httpd.conf $> `apxs -q SBINDIR`/apachectl configtest Syntax OK $> `apxs -q SBINDIR`/apachectl restart Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  28. 28. mod_perl time Download & unpack $> wget http://perl.apache.org/download/mod_perl-2.0-current.tar.gz $> tar zxvf mod_perl-2.0-current.tar.gz [...] $> cd mod_perl-2.0.*/ Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  29. 29. mod_perl time Configure $> /usr/bin/perl Makefile.PL MP_APXS=/usr/sbin/apxs Configuring Apache/2.0.52 mod_perl2/2.0.x Perl/v5.8.6 [...] Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  30. 30. mod_perl time Build, test, install $> make $> make test $> make install Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  31. 31. mod_perl time Check your distro! mod_perl is supported by lots of them Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  32. 32. mod_perl time httpd.conf LoadModule cgi_module modules/mod_cgi.so LoadModule perl_module modules/mod_perl.so Alias /cgi-bin /var/www/cgi-bin Alias /perlrun /var/www/cgi-bin <Location /cgi-bin> SetHandler cgi-script Option +ExecCGI </Location> <Location /perlrun> SetHandler perl-script Option +ExecCGI PerlHandler ModPerl::PerlRun </Location> Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  33. 33. mod_perl time • SetHandler • PerlHandler • ModPerl::PerlRun Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  34. 34. mod_perl time $> apachectl restart [notice] Apache/2.0.52 (Unix) mod_perl/2.0.x Perl/v5.8.7 configured Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  35. 35. mod_perl time Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  36. 36. it works! • $ENV{MOD_PERL} = “mod_perl/2.0.x”; Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  37. 37. How fast is THAT ? $> ab -c1 -n50 http://localhost:8529/perlrun/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 83.15 [#/sec] (mean) Time per request: 12.027 [ms] (mean) Transfer rate: 26.61 [Kbytes/sec] received Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  38. 38. How fast is THAT ? mod_cgi $> ab -c1 -n50 http://localhost:8529/cgi-bin/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 5.57 [#/sec] (mean) Time per request: 179.448 [ms] (mean) Transfer rate: 1.67 [Kbytes/sec] received ModPerl::PerlRun $> ab -c1 -n50 http://localhost:8529/perlrun/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 83.15 [#/sec] (mean) Time per request: 12.027 [ms] (mean) Transfer rate: 26.61 [Kbytes/sec] received Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  39. 39. How fast is THAT ? mod_cgi $> ab -c1 -n50 http://localhost:8529/cgi-bin/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 5.57 [#/sec] (mean) Time per request: 179.448 [ms] (mean) Transfer rate: 1.67 [Kbytes/sec] received ModPerl::PerlRun $> ab -c1 -n50 http://localhost:8529/perlrun/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 15x(mod_cgi) [#/sec] (mean) Time per request: 15x(mod_cgi) [ms] (mean) Transfer rate: 16x(mod_cgi) [Kbytes/sec] received Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  40. 40. ModPerl::PerlRun • mod_perl’s bundled module • closest CGI emulation available Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  41. 41. ModPerl::PerlRun On every request for the script: • Found • Loaded • Parsed • BEGIN {} • Executed • END {} • Destroyed Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  42. 42. ModPerl::Registry httpd.conf Alias /cgi-bin /var/www/cgi-bin Alias /perlrun /var/www/cgi-bin Alias /registry /var/www/cgi-bin <Location /cgi-bin> SetHandler cgi-script Option +ExecCGI </Location> <Location /perlrun> SetHandler perl-script Option +ExecCGI PerlHandler ModPerl::PerlRun </Location> <Location /perlrun> SetHandler perl-script Option +ExecCGI PerlHandler ModPerl::Registry </Location> Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  43. 43. ModPerl::Registry Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  44. 44. How fast is THAT ? $> ab -c1 -n50 http://localhost:8529/registry/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 156.37 [#/sec] (mean) Time per request: 6.395 [ms] (mean) Transfer rate: 50.04 [Kbytes/sec] received Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  45. 45. How fast is THAT ? mod_cgi $> ab -c1 -n50 http://localhost:8529/cgi-bin/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 5.57 [#/sec] (mean) Time per request: 179.448 [ms] (mean) Transfer rate: 1.67 [Kbytes/sec] received ModPerl::PerlRun $> ab -c1 -n50 http://localhost:8529/perlrun/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 83.15 [#/sec] (mean) Time per request: 12.027 [ms] (mean) Transfer rate: 26.61 [Kbytes/sec] received ModPerl::Registry $> ab -c1 -n50 http://localhost:8529/registry/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 156.37 [#/sec] (mean) Time per request: 6.395 [ms] (mean) Transfer rate: 50.04 [Kbytes/sec] received Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  46. 46. How fast is THAT ? mod_cgi $> ab -c1 -n50 http://localhost:8529/cgi-bin/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 5.57 [#/sec] (mean) Time per request: 179.448 [ms] (mean) Transfer rate: 1.67 [Kbytes/sec] received ModPerl::PerlRun $> ab -c1 -n50 http://localhost:8529/perlrun/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 15x(mod_cgi) [#/sec] (mean) Time per request: 15x(mod_cgi) [ms] (mean) Transfer rate: 16x(mod_cgi) [Kbytes/sec] received ModPerl::Registry $> ab -c1 -n50 http://localhost:8529/registry/hello.pl Benchmarking 127.0.0.1 (be patient).....done Requests per second: 28x(mod_cgi) [#/sec] (mean) Time per request: 28x(mod_cgi) [ms] (mean) Transfer rate: 30x(mod_cgi) [Kbytes/sec] received Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  47. 47. ModPerl::Registry • mod_perl’s bundled module • Faster, but CGI emulation laking Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  48. 48. ModPerl::Registry On every request for the script: • Load from file if: • Not found in cache • Cache entry is older than the file • BEGIN {} • Executed • END {} Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  49. 49. ModPerl::(PerlRun|Registry) • Try ModPerl::Registry first • faster • might break some CGIs • Try ModPerl::PerlRun after • slower • might break CGIs some Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  50. 50. ModPerl::(PerlRun|Registry) • Simple Perl modules • Designed to run perl scripts from mod_perl pretending it’s a CGI • Derives from ModPerl::RegistryCooker • New ones can be made in a pinch Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  51. 51. package ModPerl::PerlRun; use base qw(ModPerl::RegistryCooker); sub handler : method { my $class = (@_ >= 2) ? shift : __PACKAGE__; my $r = shift; return $class->new($r)->default_handler(); } my $parent = 'ModPerl::RegistryCooker'; my %aliases = ( new => 'new', init => 'init', default_handler => 'default_handler', run => 'run', can_compile => 'can_compile', make_namespace => 'make_namespace', namespace_root => 'namespace_root', namespace_from => 'namespace_from_filename', is_cached => 'FALSE', should_compile => 'TRUE', flush_namespace => 'flush_namespace_normal', cache_table => 'cache_table_common', cache_it => 'NOP', read_script => 'read_script', shebang_to_perl => 'shebang_to_perl', get_script_name => 'get_script_name', chdir_file => 'NOP', get_mark_line => 'get_mark_line', compile => 'compile', error_check => 'error_check', should_reset_inc_hash => 'TRUE', strip_end_data_segment => 'strip_end_data_segment', convert_script_to_compiled_handler => 'convert_script_to_compiled_handler', ); $aliases{$_} = $parent . quot;::quot; . $aliases{$_} for keys %aliases; __PACKAGE__->install_aliases(%aliases); Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  52. 52. CGI emulation • It’s an emulation • It ain’t the real thing • things can go wonky Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  53. 53. I remember #!/usr/bin/perl use CGI; my $q = new CGI; print $q->header('text/plain'); $counter++; print <<quot;EOFquot;; counted $counter EOF Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  54. 54. #!/usr/bin/perl I remember use CGI; my $q = new CGI; print $q->header('text/plain'); $counter++; print <<quot;EOFquot;; counted $counter EOF $> GET http://localhost:8529/cgi-bin/count.pl counted 1 $> GET http://localhost:8529/cgi-bin/count.pl counted 1 $> GET http://localhost:8529/cgi-bin/count.pl counted 1 Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  55. 55. #!/usr/bin/perl I remember use CGI; my $q = new CGI; print $q->header('text/plain'); $counter++; print <<quot;EOFquot;; counted $counter EOF $> GET http://localhost:8529/registry/count.pl counted 1 $> GET http://localhost:8529/registry/count.pl counted 2 $> GET http://localhost:8529registry/count.pl counted 3 $> GET http://localhost:8529registry/count.pl counted 1 $> GET http://localhost:8529registry/count.pl counted 4 Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  56. 56. That can’t be right? #!/usr/bin/perl use CGI; my $q = new CGI; print $q->header('text/plain'); $counter++; print <<quot;EOFquot;; counted $counter EOF Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  57. 57. That can’t be right? • Code is cached • Perl sticks around • Great for speed • Globals are suddently VERY GLOBAL Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  58. 58. That can’t be right? #!/usr/bin/perl use CGI; my $q = new CGI; print $q->header('text/plain'); $counter++; print <<quot;EOFquot;; counted $counter EOF Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  59. 59. That can’t be right? #!/usr/bin/perl use CGI; my $q = new CGI; print $q->header('text/plain'); $main::counter++; # GLOBAL! print <<quot;EOFquot;; counted $counter EOF Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  60. 60. That can be right! #!/usr/bin/perl use CGI; my $counter; my $q = new CGI; print $q->header('text/plain'); $counter++; print <<quot;EOFquot;; counted $counter EOF Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  61. 61. Globals will stick • Globals will stick around • Use my • Try ModPerl::PerlRun Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  62. 62. CGI emulation • It’s an emulation • It ain’t the real thing • things can go wonky Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  63. 63. I still remember #!/usr/bin/perl $> GET http://localhost:8529/registry/count.pl Counter: 1 use CGI; $> GET http://localhost:8529/registry/count.pl use strict; use warnings; Counter: 2 $> GET http://localhost:8529/registry/count.pl my $q = new CGI; Counter: 3 my $counter; $> GET http://localhost:8529/registry/count.pl Counter: 1 print $q->header('text/plain'); $> GET http://localhost:8529/registry/count.pl Counter: 4 counter_up(); sub counter_up { $counter++; print quot;Counter: $counternquot;; } Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  64. 64. But, what the..? • I used my so it’s not a global... • To Perl, all code lives in subroutines • mod_perl turns scripts into subroutines • subroutines can create closures Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  65. 65. subification * #!/usr/bin/perl use CGI; use strict; use warnings; my $q = new CGI; my $counter; print $q->header('text/plain'); counter_up(); sub counter_up { $counter++; print quot;Counter: $counternquot;; } * the process of making a subroutine, obviously Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  66. 66. subification * • subroutine needs a name • filepath is a convenient naming * the process of making a subroutine, obviously Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  67. 67. subification * /var/www/cgi-bin/hello.pl ModPerl::ROOT::ModPerl::Registry::var_www_cgi_2dbin_hello_2epl::handler Prefix Path Sub * the process of making a subroutine, obviously Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  68. 68. sub subification * ModPerl::ROOT::ModPerl::Registry::var_www_cgi_2dbin_count_2epl::handler { package ModPerl::ROOT::ModPerl::Registry::var_www_cgi_2dbin_count_2epl; no warnings; local $0 = '/var/www/cgi-bin/count.pl’; use CGI; use strict; use warnings; my $q = new CGI; my $counter; print $q->header('text/plain'); counter_up(); sub counter_up { $counter++; print quot;Counter: $counternquot;; } } the process of making a subroutine, obviously * Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  69. 69. closures • Closures are a Perl feature • Come to be when creating subs • Normally not an issue • mod_perl can create some without telling you • man perlref for more Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  70. 70. I still remember #!/usr/bin/perl use CGI; 2 warning signs to look for: use strict; use warnings; globals my $q = new CGI; my $counter; error_log: print $q->header('text/plain'); Variable quot;$counterquot; will not stay shared at /var/www/cgi-bin/ counter_up(); count.pl sub counter_up { $counter++; # GLOBAL print quot;Counter: $counternquot;; } Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  71. 71. I still remember #!/usr/bin/perl use CGI; use strict; use warnings; Just remember that globals in general are to my $q = new CGI; my $counter; print $q->header('text/plain'); watch out for counter_up($counter); sub counter_up { my $counter = shift; $counter++; # GLOBAL print quot;Counter: $counternquot;; } Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  72. 72. Good advice • use strict; • use warnings; • avoid globals • look for hints in your error_log file Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  73. 73. CGI emulation • It’s an emulation • It ain’t the real thing • things can go wonky Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  74. 74. I used to work #!/usr/bin/perl #countlib.pl use CGI; use strict; use strict; my $counter = 0; use warnings; sub counter_up { require quot;countlib.plquot;; $counter++; print “Counter: $countern”; my $q = new CGI; } print $q->header('text/plain'); counter_up(); Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  75. 75. I used to work error_log: [Sun Oct 23 21:37:44 2005] [error] Can't locate countlib.pl in @INC (@INC contains: [...]) at /var/www/cgi-bin/hello.pl line 7.n Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  76. 76. I used to work #!/usr/bin/perl use CGI; use strict; use warnings; use Cwd; my $q = new CGI; print $q->header('text/plain'); print “Cwd: “, cwd(), “n”; $> GET http://localhost:8529/cgi-bin/cwd.pl Cwd: /var/www/cgi-bin $> GET http://localhost:8529/registry/cwd.pl Cwd: / Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  77. 77. I used to work • mod_cgi • fork()s • chdir()s to the script’s directory Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  78. 78. Cwd • mod_perl • doesn’t fork() • doesn’t chdir()s • could chdir()s Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  79. 79. Cwd • Cwd() is a process proprety • Apache threaded MPMs • chdir() isn’t thread-safe • mod_perl doesn’t try to break things Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  80. 80. Cwd • You can fix your scripts • use lib(); • require “/fully/qualfied/lib/path.pl”; Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  81. 81. Cwd • If your MPM is Prefork: • ModPerl::RegistryPrefork • ModPerl::PerlRunPrefork Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  82. 82. It ain’t magical! • use strict/warnings • Think about globals • watch the error_log Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  83. 83. More info • mod_perl User’s mailing-list – http://perl.apache.org/maillist/modperl.html – <modperl@perl.apache.org> • mod_perl Developer's Cookbook – http://www.modperlcookbook.org/ • – http://www.modperlbook.org/ Practical mod_perl • – http://perl.apache.org/ mod_perl at the ASF Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  84. 84. Thank You! Slides and bonus material: http://gozer.ectoplasm.org/talk/ Philippe M. Chiasson - http://gozer.ectoplasm.org/talks/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×