Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Slow SSI

by Baffled (Acolyte)
on Feb 13, 2008 at 19:29 UTC ( #667803=perlquestion: print w/ replies, xml ) Need Help??
Baffled has asked for the wisdom of the Perl Monks concerning the following question:

I use server side include cgi files to deliver dynamic content to my shtml pages, however I beleive they may slow the load time of my pages. I believe there are much
faster ways to do what these ssi files do using more upto date scripting, something to do with the way it collects information to be displayed. Im hoping to get
some feedback on this, and to overall speed up my ssi. Im posting the code for one of several of these types of files, with the thinking that if I can see one I will
be able to figure out the changes for other similar files. The main differences of these files is the area of information that is collected and the size and way its
displayed, I can figure out the display parts with some trial and error, however some notes on the areas of how its collecting the information would be helpful to
help me understand how & where its looking for the information.
#!/usr/bin/perl use Carp; use vars qw(%config %category %form %supercat); require "/var/www/vhosts/mysite.com/cgi-bin/auction/categories.cgi"; require "/var/www/vhosts/mysite.com/cgi-bin/auction/timeleft.cgi"; local %config; $config{'basepath'} = '/var/www/vhosts/mysite.com/cgi-bin/auction/'; $config{'htmlpages'} = 'http://www.mysite.com'; $config{'colortablehead'} = '#E6E6E6'; $config{'colortablehead0'} = '#E6E6E6'; $config{'colortablehead1'} = '#E6E6E6'; $config{'colortablehead2'} = '#E6E6E6'; $config{'colortablehead3'} = '#E6E6E6'; $config{'colortablehead4'} = '#E6E6E6'; $config{'colortablehead5'} = '#E6E6E6'; $config{'colortablehead6'} = '#E6E6E6'; $config{'colortablehead7'} = '#E6E6E6'; $config{'colortablehead8'} = '#E6E6E6'; $config{'colortablehead9'} = '#E6E6E6'; $config{'colortablehead10'} = '#EEEEEE'; $config{'colortablehead10T'} = '#E6E6E6'; $config{'colortablehead10B'} = '#EEEEEE'; $config{'colortablehead11'} = '#E6E6E6'; $config{'colortablehead12'} = '#E6E6E6'; $config{'colortablehead13A'} = '#FFFFBF'; $config{'colortablehead13B'} = '#FFFFBF'; $config{'colortablehead13C'} = '#FFFF8C'; $config{'colortablehead13D'} = '#FFFF8C'; $config{'colortablehead13E'} = '#FFFF00'; $config{'colortablehead13F'} = '#FFFF00'; $config{'specialtext0'} = '#FF0000'; $config{'specialtext1'} = '#FF0000'; $config{'specialtext2'} = '#FF0000'; $config{'text'} = '#000000'; $config{'text0'} = '#000000'; $config{'text1'} = '#000000'; $config{'text2'} = '#000000'; $config{'text3'} = '#000000'; $config{'text4'} = '#000000'; $config{'text5'} = '#000000'; $config{'text6'} = '#000000'; $config{'text7'} = '#000000'; $config{'text8'} = '#000000'; $config{'text9'} = '#000000'; $config{'text10'} = '#FF0000'; $config{'text10T'} = '#000000'; $config{'text10B'} = '#000000'; $config{'text12'} = '#000000'; $config{'text13A'} = '#000000'; $config{'text13C'} = '#000000'; $config{'text13E'} = '#000000'; $config{'colortablebody'} = '#E6E6E6'; $config{'colortablebody1'} = '#EFEFEF'; $config{'bordercolor'} = '#0000FF'; $config{'bordercolor1'} = '#000000'; $config{'odd_row_color'} = '#FFFFFF'; $config{'even_row_color'} = '#EEEEEE'; $config{'currency'} = '$'; $config{'currency2'} = 'U.S.'; $config{'currency3'} = 'dollar'; $config{'disp_max'} = 20; ################################## # SSI Gallery on front page # Randomly gathers current items posted to gallery print "Content-type: text/html\n\n"; print "<div align=center><center><table border=0 cellpadding=0 + cellspacing=0 width=100%>\n"; my $key; my $s = 1; my $pagebreak = int $form{pb} || 17; my ($icount, $pcount) = (0,0); foreach $key (sort keys %category) { opendir THEDIR, "$config{'basepath'}$key" || die "Unable to op +en directory: $!"; my @allfiles = readdir THEDIR; closedir THEDIR; my $file; @allfiles = get_random( \@allfiles, $config{'disp_max' +}) if $#allfiles >= $config{'disp_max'}; foreach $file (sort { int($a) <=> int($b) } @allfiles) { if (-T "$config{'basepath'}$key/$file") { open THEFILE, "$config{'basepath'}$key/$file"; my ($title, $reserve, $inc, $desc, $image1, $image2, $image3, +$image4, $thumb1, $thumb2, $thumb3, $thumb4, $dutch, $qty, $bold, $hi +ghlight, $feat, $catfeat, $grabber, $relist, $buyit, $gallery, $count +er, $ship1, $ship2, $ship3, $ship4, $ship5, $shipcost, $location, $pa +y1, $pay2, $pay3, $pay4, $pay5, $pay6, $pay7, $pay8, $pay9, $paypal, +$idata1, $idata2, $idata3, $idata4, $idata5, $idata6, $idata7, $idata +8, $idata9, @bids) = <THEFILE>; my ($alias, $email, $bid, $time, $add1, $add2, $add3, $oqty, $ +qtysold) = &read_bid($bids[$#bids]); my ($selleralias, $selleremail, $sellerbid, $sellertime, $sell +eradd1, $selleradd2, $selleradd3) = &read_bid($bids[0]); close THEFILE; chomp($title, $reserve, $inc, $desc, $image1, $image2, $image3 +, $image4, $thumb1, $thumb2, $thumb3, $thumb4, $dutch, $qty, $bold, $ +highlight, $feat, $catfeat, $grabber, $relist, $buyit, $gallery, $cou +nter, $ship1, $ship2, $ship3, $ship4, $ship5, $shipcost, $location, $ +pay1, $pay2, $pay3, $pay4, $pay5, $pay6, $pay7, $pay8, $pay9, $paypal +, $idata1, $idata2, $idata3, $idata4, $idata5, $idata6, $idata7, $ida +ta8, $idata9, @bids); my @lastbid = split(/\[\]/,$bids[$#bids]); $file =~ s/\.dat//; my @closetime = localtime int($file); my $timeleft = &timeleft($file); $closetime[4]++; my $imagedisp = "<IMG SRC=$thumb1 BORDER=0 + width=95>"; if ($gallery eq "Yes") { if(++$icount > $pagebreak){$icount=1; $pco +unt++} next if $pcount != $form{page}; print "\n<td width=20\% height=150 align=center valign +=bottom><font face=arial size=2><table width=80 height=80 bgcolor=000 +000><tr><br><br><td align=center><A HREF=$config{'htmlpages'}/cgi-bin +/auction/auction.cgi\?category=$key\&item=$file>$imagedisp</a></td></ +tr></table><FONT SIZE=1 FACE=ARIAL>Current Bid: $config{'currency'}$b +id</font><br><FONT SIZE=1 FACE=ARIAL>Closes: $timeleft</font></td>\n" + if ($thumb1); if ($s==1) { print "\n </tr>"; $s = 1; } else { $s++; } + } } } } if ($s > 1 && $s < 1) { print "\n </TR>\n"; } if ($icount <= 0){ print "<tr><td align=center valign=top hei +ght=225><font face=arial size=2><B><br>No items in category.</B></cen +ter></font></td></tr>"; } print "</table>"; print "<div align=center><center><table border=0 cellpadding=0 + cellspacing=0 width=100%></td></tr></table></center></div>\n"; print "<br><font face=arial size=2><div align=center><center>< +table border=0 cellpadding=0 cellspacing=0 width=100%><tr><td align=r +ight width=90%><font size=2><a href=$config{'htmlpages'}/cgi-bin/auct +ion/auction.cgi\?action=gallery>More Gallery!</a>&nbsp;&nbsp;&nbsp;&n +bsp;</font></td></tr></table></center></div>\n"; # print "<font face=arial size=2><div align=center><center><tab +le border=0 cellpadding=0 cellspacing=0 width=100%><tr><td width=100% + bgcolor=E8E8E8 height=3></td></tr></table></center></div><br>\n"; #-############################################# # Sub: Read Bid Information (bid_string) # Reads an item file sub read_bid { my $bid_string = shift; my ($alias, $email, $bid, $time, $add1, $add2, $add3, $oqty, $qtys +old) = split(/\[\]/,$bid_string); return ($alias, $email, $bid, $time, $add1, $add2, $add3, $oqty, $ +qtysold); } #-############################################# # Sub: Get Random Item Data # Written by: Dieter Werner # Copyright by: Dieter Werner #-############################################# sub get_random { my ($array, $cnt) = @_; my ($val, $i, $j); my @random; my $noe = @$array; $cnt = $noe if $cnt > $noe; for ($i = @$array; --$i;) { $j = int rand ($i + 1); next if $i == $j; @$array[$i, $j] = @$array[$j, $i]; } while ($val = pop @$array) { if ($cnt > 0) { push @random, $val; --$cnt; } } return @random; } 1;

Comment on Slow SSI
Download Code
Re: Slow SSI
by pc88mxer (Vicar) on Feb 13, 2008 at 19:50 UTC
    I'll suggest an architectural change... your script does a lot of directory readings, reading of files, randomizing and sorting, and it's doing all of these things on every page hit. How often do you really need to change the image gallery? If you can live with more infrequent updates, have a cron job which does this work and stores the results in a file. Then your SSI just opens this file, reads the image data and generates the HTML.

    Even if you have the cron job run once a minute, you are only doing this work once a minute instead of once for every page hit.

      Thats actually a really good idea and something totally different than I was expecting. It wouldnt even have to be every minute, more like every 30 would work. I've never made a crone job before, but have heard of it. So this would be a independent script that ran in the background? Would it still be a cgi type of file? Im thinking your talking about splitting this file into two parts and making the search for item part run by crone in the background while the print stuff is called from the html? Or would it still be a ssi?
        For minimum modification you would change your SSI from
        <!--#exec cgi="/cgi-bin/foo.cgi"--> to <!--#include virtual="/cache/latest.html" -->

        You would add a command to your crontab like:

        30 * * * * /www/cgi-bin/foo.cgi > /www/cache/latest.html

        All this entry does is run the foo.cgi script once every 30 minutes and pipe the output to the file you are including via your SSI. Naturally you will need to adjust the paths and names to suit. Execute the command once to generate the latest.html include and also ensure that the command works as expected.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-12-19 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (84 votes), past polls