perl-embperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Neil Gunton <n...@nilspace.com>
Subject Re: What's going on with Embperl???
Date Wed, 17 Sep 2003 23:48:26 GMT
Ruben Safir Secretary NYLXS wrote:
> After the first hit, and when EMBPERL has it's logging level lowered,
> is was outstandingly fast, especially with large data grabs, and pages
> which were repeated.  OTOH, my HTTPD processes grow large...and I'm thrilled
> to death about that.  The key is modperl.  Modperl is a C program which
> is exceptionally fast.  Most of my embperl code looks like this

If you're really concerned with performance, then you should preload the modules in your startup.pl
script. I do this with a recursive sub that scans the directories & calls Execute() to
bring each
one into memory and cache it. It makes server startup a little slower, since it needs to compile
all the Embperl modules in the website, but after that it's all there and much faster. Also,
more
memory is shared, because all the Embperl modules are shared with the Apache child processes.
Otherwise, each child process compiles its own copy of the code. You'll notice that if you
don't preload,
then the first request to any mod_perl child is a little slower, because it is compiling the
module. If you preload, then after the initial startup, all children respond with equal speed
immediately.
My startup.pl file is included at the bottom of this email, for anyone who's interested.
 
> [- use MYMODULE -]
> [- $obj = MODULE->new('var') -]
> [- $obj->displaywidget -]
 
This would be more efficiently done in a single Embperl block:

[-
   use MYMODULE;
   $obj = MODULE->new('var');
   $obj->displaywidget;
-]

Not getting nitpicky or anything, but I think it's a little more efficient for Embperl that
way...

'Course, I could have my head up my own ass as usual too.

/Neil


#!/usr/bin/perl

# First modify the include path
BEGIN
{
    use strict;
    use Apache ();
    use lib '/www/lib/perl';
}


# Common modules
use Apache::Constants ();
use Apache::File ();
use Apache::Log ();
use IO::Zlib ();
use Safe ();
use URI::Escape ();
use Log::Logger ();
use File::Copy ();
use File::Path ();
use File::Glob ();
use Time::Zone ();
use CGI qw (-compile :cookie cgi_error header);
use Date::Calc qw(:all);
use Image::Magick ();
use HTML::Embperl ();
use HTML::EmbperlObject ();
#use Embperl ();
#use Embperl::Object ();
use Mail::Sender;
use LWP::UserAgent;
use HTTP::Request;
use DBI ();
DBI->install_driver('mysql');
use Digest::HMAC_MD5 qw(hmac_md5_hex);
use SOAP::Lite ();
use Text::Wrap;

# My modules
use Apache::BlockAgent ();
use Apache::Nilspace::Main::Access ();
use Apache::Nilspace::Subscription::Access ();
use Apache::Nilspace::Subscription::Handler ();
use Nilspace ();
use Nilspace::Agenda ();
use Nilspace::Commerce ();
use Nilspace::Mail ();

# Apache::VMonitor
use Apache::VMonitor();
$Apache::VMonitor::Config{BLINKING} = 1;
$Apache::VMonitor::Config{REFRESH}  = 0;
$Apache::VMonitor::Config{VERBOSE}  = 0;
$Apache::VMonitor::Config{SYSTEM}   = 1;
$Apache::VMonitor::Config{APACHE}   = 1;
$Apache::VMonitor::Config{PROCS}    = 1;
$Apache::VMonitor::Config{MOUNT}    = 1;
$Apache::VMonitor::Config{FS_USAGE} = 1;
$Apache::VMonitor::Config{SORT_BY}  = 'size';
$Apache::VMonitor::PROC_REGEX = join "\|", qw(httpd_proxy httpd_perl mysql );

# For handling the remote ip address through mod_proxy reverse proxy
sub My::ProxyRemoteAddr ($)
{
    my $r = shift;
                                    
    # we'll only look at the X-Forwarded-For header if the requests
    # comes from our proxy at localhost
    return Apache::Constants::OK 
	unless ($r->connection->remote_ip eq "127.0.0.1") 
	and $r->header_in('X-Forwarded-For');
                                   
    # Select last value in the chain -- original client's ip
    if (my ($ip) = $r->headers_in->{'X-Forwarded-For'} =~ /([^,\s]+)$/)
    {
	$r->connection->remote_ip($ip);
    }
    
    return Apache::Constants::OK;
}

# Preload Embperl website code
if (lc($ENV{PRELOAD_WEBSITES}) eq 'on')
{
    preload_dir ('/www/lib/perl/Apache', '*.html *.epl');
    preload_dir ('/www/vhosts/www.neilgunton.com/htdocs', '*.html *.epl');
    preload_dir ('/www/vhosts/www.crazyguyonabike.com/htdocs', '*.html *.epl');
}

# Recursive directory traversal sub which preloads Embperl files
sub preload_dir
{
    my ($dir,            # The current directory which is to be processed
	$pattern,        # Pattern identifying files to be processed, e.g. '*.html *.epl'
	@search_path     # List of paths for giving to Embperl to search for files
	) = @_;

    @search_path = () if !@search_path;

    # Put the current dir on the search path
    push (@search_path, $dir);

    local *DIR;
    opendir (DIR, $dir) or die "Could not open directory: $dir: $!";

    # First, process files in this directory
    # Pattern consists of a potential list of patterns, separated by spaces.
    # First we make a list of patterns, and then glob each of these
    foreach my $glob (split (/\s/, $pattern))
    {
	# Iterate through the resulting list of files
	foreach my $file (File::Glob::glob ("$dir/$glob"))
	{
	    if (!(-d $file) && (-e $file))
	    {
		# Build up the paths, starting with the current dir
		# and working back up to the website root
		my $path = '';
		foreach my $i (1 .. scalar(@search_path))
		{
		    $path .= ':' . $search_path[scalar(@search_path) - $i];
		}
		$path .= ':/www/lib/perl/Apache/';
		$file =~ /\/([^\/]+)$/;
		my $filename = $1;
		print "Embperl::Execute $file\n";
		HTML::Embperl::Execute ({inputfile => $file,
					 path => $path,
					 import => 0,
					 escmode => 0,
					 options => 16}) ;
	    }
	}
    }

    # Now, recursively go down into subdirectories
    while (defined(my $subdir = readdir (DIR)))
    {
	# Only recurse on directories, which do not start with ".",
	# and skip symbolic links
	if (-d "$dir/$subdir" &&
	    !(-l "$dir/$subdir") &&
	    ($subdir !~ /^\.{1,2}$/))
	{
	    
	    preload_dir ("$dir/$subdir", $pattern, @search_path);
	}
    }
}

1;

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Mime
View raw message