up
Perl Lecture Notes - June 29
- Questions
- Design Patterns
- Object oriented best practices
- 1995 book by Gamma, Helm, Johnson, Vlissides ("Gang of Four")
includes strengths, weaknesses, motivations, etc. for a few dozen patterns.
- "Design Patterns capture solutions.
that have developed and evolved over time." - from the preface of Gama's book
- Not software, or algorithms exactly, or implementations
- Part of the vocabulary of object programming these days;
some features of object oriented languages are built on these ideas.
- Also see Refactoring : methods of cleaning up object oriented code
to remove the "smell".
- Also see AntiPatterns: programming constructions to avoid
- Seems almost like a religion to some folks. (See "QWAN".)
- Can seem fairly abstract, but can be very helpful.
- Samples
- Iterator
- "Intent: Provide a way to access the elements of an aggregate object
sequentially without exposing its underlying representation."
- For example, in perl we have $a="hello"; $a++;
- Singleton
- "Intent: Ensure a class has only one instance,
and provide a global point of access to it."
- Many of the perl CPAN modules which allow a
function or object oriented interface will create a
private copy of an object if you don't create one
explicitly - and then use that single object for all
subsequent calls. Also used for shared resources.
Multiple requests for, say "printer", return the
same single resource. sample perl singleton
- Strategy
- "Intent: Define a family of algorithms,
encapsulate each one, and make them interchangeable.
Strategy lets the algorithm vary independently from
clients that use it."
- See, for example, the CPAN module Crypt::CBC
that encrypts data with a Cipher Block Chain - you
pass it any one of a number of encryption
algorithms, such as Blowfish, DES, Rijndael, GOST,
etc. Or the DBI modules, with various
implementations underneath.
- Benchmarking Server Performance
- ab - Apache Benchmark utility; comes with apache.
/home/httpd/bin/ab on bob.
Man pages are here.
- crashme
perl script that does roughly what ab does,
using LWP::Parallel and Time::HiRes
- Typical stuff to measure
- requests handled per second
- latency - that is, how long each request takes.
- Parts of request that will effect performance
- (1) network speed (dial-up? packets going to Korea?)
- (2) server framework speed (i.e. apache)
- (3) dynamic HTML generation speed (i.e. mod_perl)
- (4) database speed (i.e. postgres)
- Ways to speed things up
- use mod_perl rather than .cgi, especially
if (3) is limiting factor
- .. or even program within Apache with modules and handlers
- cache your results, particularly if the
content changes slowly or similar content
for many requests.
Some of the content environments,
like HTML::Mason provides some support for this, or
you can incorporate your own caching with
the help of modules like Perl Cache
- Devel::DProf profiler
module is suggested for tuning your code.
( "perl -d:DProf test.pl" will run test.pl
and then dump subroutine statistics to
a file "tmon.out". See "perldoc Devel::DProf".)
- for the big time : multiple servers and load
balancing - beyond the scope of this talk.
(But see this
article which discusses the eToys site, written in
Apache/mod_perl, HTML Template, and so on. 3rd busiest
site for Christmas 2000 season, with 200,000 sessions/hr.)
- Some results,
all with 100 requests and 10 concurrent
/home/httpd/bin/ab -n 100 -c 10 url
- www.braats.org
zonorus, static web pages
356 requests/sec , 28 ms/request
- www.dawndance.org
zonorus, CGI::SpeedyCGI, HTML::Mason
30 reqests/sec , 328 ms/request
- marlboro.vt.us
zonorus, my templates built on Template toolkit,
pure perl cgi (pretty bloated)
2 requests/sec , 4688 ms/request (5 sec/request !)
- hi.cgi
bob, trivial .cgi script
116 requests/sec, 86 ms/request
- hi.cgimp
bob, same running under mod_perl
667 requests/sec, 15 ms/request
- people.cgi
bob, perl cgi, several modules to load
8 requests/sec, 1222 ms/request (1 sec/request)
- people.cgimp
bob, same running under mod_perl
190 requests/sec, 53 ms/request
- people.cgi?action=showall
bob, perl cgi, DBD::CSV reads 800 lines
3.4 requests/sec, 2978 ms/request (3 sec/request)
- people.cgimp?action=showall
bob, mod_perl, DBD::CSV reads 800 lines
5.4 requests/sec, 1862 ms/request (2 sec/request)
- Review : a sample perl/cgi/database script
(i.e. people.cgi, Dawn Dance Mailing List)
(If there's time and interest.)
- a fairly generic example that isn't too simplistic
- about 900 names, addresses, emails, etc.
- I made the mailing list by merging two previous versions:
one from an excel spreadsheet and
the other from a cold fusion database output as a CSV file.
I translated it into a common format with perl using regexps,
saving the whole thing as a CSV (comma seperated values) file.
- html front end - cgi or mod_perl, CGI.pm for parameters.
- database back end - SQL DBI::CSV,
perhaps moving to XML or Postgres later.
- This is still in progress - I had hoped to use it to
show more benchmark comparisons but didn't get that far.
- The database : DDmailingList
- Rut it
- Source code
- To Do list
- Issues
- database piece, and testing it
- cgi parameter logic / user interface, and testing it
- creeping featurism
- Resources
- Design Patterns and Refactoring
- Benchmarking
- Assignmnent
- Work on your capstones.
- Entirely Optional
- Read and think about design patterns.
- Run the ab benchmark utility on your web scripts,
testing and timing the various tasks they do.
- Read the eToys article on large scale
Apache/mod_perl E-commerce
See you at the capstone fair.