PHP Optimize
PHP Optimize
PHP Applications
PHP International Conference 2002
o PHP optimization
o Server side caching
o Database optimization
o Bandwidth optimization
o External utilities
-2-
Slide 2/40 November 4, 2002
Reducing Output
Less output is good because...
o Saves server bandwidth
o Decreases server CPU and Memory<br>usage (well, kinda)
o Has the same effect on the client side
o On large sites, bandwidth is most<br>often the most costly bottleneck
-3-
Slide 3/40 November 4, 2002
Optimize
-4-
Slide 4/40 November 4, 2002
HTML Optimization
o Remove All Comments!
o Meta tags don't work!
o Use parent HTML tags to specify child values, when all children have the same value
o Fully qualify links (ie, personal/images/<br>not personal/images)
-5-
Slide 5/40 November 4, 2002
CSS Optimization
<STYLE TYPE="text/css">
<--
TABLE {table-layout:fixed; width: 100%}
#fixed COL {width: 50%}
-->
</STYLE>
<TABLE>
<COL><COL>
<TR><TD>Cell 1</TD><TD>Cell 2</TD></TR>
<TR><TD>Cell 3</TD><TD>Cell 3</TD></TR>
</TABLE>
-6-
Slide 6/40 November 4, 2002
HTTP Optimization
URL Shortening
By using Apache's <i>mod_rewrite</i> module, you can greatly shorten your HTML files by
making common locations into one/two letter aliases.
/etc/apache/abbr
b bytes/
d dogs/
g graphics/
gt graphics/template/
HTTP Caching
Use HTTP headers to have finer grained control over browser caching, these headers include:
Headers
Last-Modified: Mon, 22 Jul 2002 15:50:00 GMT
Expires: Mon, 22 Jul 2002 15:52:25 GMT
Cache-Control: must-revalidate, max-age=15, s-maxage=0, private
-7-
Slide 7/40 November 4, 2002
Compress
HTTP supports native compression by using Lempel-Ziv encoding (LZ77). Its the same
encoding mechanism used by the popular unix tool <code>gzip</code>.
Compression Advantages
o Greatly reduces bandwidth
o Cross browser compliant
o Compression can cache as well
Compression Disadvantages
o CPU intensive on the Server
o CPU intensive on the Client
-8-
Slide 8/40 November 4, 2002
PHP Compression
PHP supports output compression via PHP's ouput buffering features and the zlib extension.
The simplest way is to use the <code>ob_start ('ob_gzhandler')</code> or use the configuration
options <code>zlib.output_compression</code> and <code>zlib.compression_level</code>
HTTP_Compress
Alternatively, you can use PEAR's excellent HTTP_Compress class that will handle everything
related to HTTP_Compression for you...
<?php
require_once 'HTTP/Compress.php';
HTTP_Compress::start ();
print "Some data\n";
print "Some more data\n";
print "Even more data\n";
HTTP_Compress::output ();
? >
-9-
Slide 9/40 November 4, 2002
mod_gzip
Mod_gzip is a superb apache module which handles gzip compression and data caching. It
offers signifigant increases in compression performance to the homebrewed PHP solutions.
mod_gzip features
o Compression statistics
o The ability to chose compression via MIME type<br> and Filename
o You can exclude compression for certain browsers
o Can cache compression results
- 10 -
Slide 10/40 November 4, 2002
Server side caching
- 11 -
Slide 11/40 November 4, 2002
Do not use PHP!
PHP...
...leads to slower pages.
Slower pages...
...lead to fewer users.
Fewer users...
...lead to less income.
- 12 -
Slide 12/40 November 4, 2002
Static rulez!
- 13 -
Slide 13/40 November 4, 2002
Do use PHP!
but
o Use as much static content as possible
o Cache dynamic generated content
o Use as few database queries as possible
- 14 -
Slide 14/40 November 4, 2002
PEAR::Cache_Lite
o Simple caching
o Caches pages, blocks, functions
- 15 -
Slide 15/40 November 4, 2002
PEAR::Cache_Lite - Example
<?php
/ Include the class /
require_once('Cache/Lite.php');
- 16 -
Slide 16/40 November 4, 2002
Caching: Pros and Cons
Pros:
o Less use of external resources
Cons:
o PHP is still parsing the page
o Cache expiry time
o Stale content
- 17 -
Slide 17/40 November 4, 2002
Pregenerated content
- 18 -
Slide 18/40 November 4, 2002
Pregenerating: Pros and Cons
Pros:
o No stale content
o PHP is only used for regeneration
Cons:
o Costs a lot of resources
o Hard to figure out what to regenerate
- 19 -
Slide 19/40 November 4, 2002
Creating pages on demand
- 20 -
Slide 20/40 November 4, 2002
On demand - example
Facts:
Url: http://yoursite.com/news/000001.html
Path: /home/httpd/news/
generate-news.php:
<?php
$id = basename($_SERVER['REDIRECT_URL'], '.html');
- 21 -
Slide 21/40 November 4, 2002
On demand: Pros and Cons
Pros:
o No wasted resources on generating
o No stale content
Cons:
o None :-)
- 22 -
Slide 22/40 November 4, 2002
External Tools
o Bytecode caches
o Content caches
o SRM: Script Running Machine
o lingerd
- 23 -
Slide 23/40 November 4, 2002
Bytecode caches
- 24 -
Slide 24/40 November 4, 2002
Cache Benchmark
- 25 -
Slide 25/40 November 4, 2002
Squid
o transparent caching
o caches HTTP and FTP
if ($_SERVER["HTTP_IF_MODIFIED_SINCE"] == $tsstring) {
/ The UA has the exact same page we have. /
header("HTTP/1.1 304 Not Modified");
exit();
} else {
header("Last-Modified: ".$tsstring);
}
? >
- 26 -
Slide 26/40 November 4, 2002
SRM: Script Running Machine
- 27 -
Slide 27/40 November 4, 2002
lingerd
o Lingering conections
o Apache hands over the job
o Apache can do useful things
o Less server load
- 28 -
Slide 28/40 November 4, 2002
Performance
So You Want to get performance from your php applications?
- 29 -
Slide 29/40 November 4, 2002
Sockets
Performance Problems
o Makes you network dependent (TCP/IP)
o Communication protocol overheads (TCP/IP, especially)
Performance Tips
o When dealing with local sockets consider other<br> options like pipes or shared memory.
o Establishing a connection is the most costly <br> part of the TCP/IP layer. Think carefully about
re-using pre-existing connections.
o When transfering large chunks of data, consider using a compression/encryption method to lower
bandwidth
- 30 -
Slide 30/40 November 4, 2002
File I/O
File I/O Problems
o Disk access is slower than memory access (duh!)
o Simultaeneous write/write operations cause data corruption. Therefore locking must occur.
o Locking is slow
File I/O Tips
o Use the disk in concentrated bits, meaning do all<br>your writing together.
o Do Not Read Large Files Into Memory!
o Do Not Read Small Files From Disk!
o Employ caching to access frequently used data<br> stored in memory
o Try and put write operations where they will<br> cause the least loss in user experience
(remember,<br> web pages are chunked!)
- 31 -
Slide 31/40 November 4, 2002
CPU Usage
o Avoid Using frequent array accesses, remember, each array access is a hash lookup.
The Wrong Way
<?php
$ar['name'] = 'Sterling';
- 32 -
Slide 32/40 November 4, 2002
Regular Expressions
Slow Regexps
Its often beneficial to replace regular expressions with simpler functions that have the same
effect.
Fast Regexps
In other cases it may help to use regular expressions instead of custom parse routines in your PHP
code.
- 33 -
Slide 33/40 November 4, 2002
SQL Queries
SQL Problems
o Unused data is being pulled over a socket
o FULLTEXT indexes
o the LIKE clause
SQL Tips
o Study and Learn Database Optimization
o Failing that use the following tips...
o MySQL supports the 'LIMIT' clause which can be used<br> to lower the returned result set to a
specified number
o Never use LIKE or a FULLTEXT index to do complex<br> searches. Use keywords and an
alternative ranking system
o The only thing worse than using a LIKE clause, is building<br> your own LIKE clause in PHP.
- 34 -
Slide 34/40 November 4, 2002
Database optimization
- 35 -
Slide 35/40 November 4, 2002
Visitation model 1
Directory structure:
gimmeParent(7);
? >
- 36 -
Slide 36/40 November 4, 2002
Visitation model 2
Tree structure:
Visitation:
- 37 -
Stored as: <table class="db" border="1" align="center" cellspacing="0"> <tr>
<th>Id</th><th>Parent</th><th>Name</th><th>Left</th><th>Right</th> </tr> <tr>
<td>3</td><td>1</td><td class="left">Countries</td><td>1</td><td>12</td> </tr> <tr>
<td>4</td><td>3</td><td class="left">Belgium</td><td>2</td><td>3</td> </tr> <tr>
<td>5</td><td>3</td><td class="left">Netherlands</td><td>4</td><td>9</td> </tr> <tr>
<td>6</td><td>3</td><td class="left">Germany</td><td>10</td><td>11</td> </tr> <tr>
<td>7</td><td>5</td><td class="left">Business</td><td>5</td><td>6</td> </tr> <tr>
<td>8</td><td>5</td><td class="left">Economy</td><td>7</td><td>8</td> </tr> </table>
- 38 -
Slide 37/40 November 4, 2002
Visitation model 3
Query:
SELECT * FROM directory
WHERE
left < 5 AND right > 6
- 39 -
Slide 38/40 November 4, 2002
Visitation model 4
Query:
SELECT * FROM directory
WHERE
right - left = 1
- 40 -
Slide 39/40 November 4, 2002
Visitation model 5
Select a subtree:
Query:
SELECT * FROM directory
WHERE
left > 4 AND right < 9
- 41 -
Slide 40/40 November 4, 2002
Thank You!
- 42 -
Index
Introduction ............................................................................................... 2
Reducing Output ....................................................................................... 3
Optimize .................................................................................................... 4
HTML Optimization ................................................................................. 5
CSS Optimization ...................................................................................... 6
HTTP Optimization ................................................................................... 7
Compress ................................................................................................... 8
PHP Compression ..................................................................................... 9
mod_gzip ................................................................................................... 10
Server side caching .................................................................................... 11
Do not use PHP! ........................................................................................ 12
Static rulez! ............................................................................................... 13
Do use PHP! .............................................................................................. 14
PEAR::Cache_Lite .................................................................................... 15
PEAR::Cache_Lite - Example .................................................................. 16
Caching: Pros and Cons ............................................................................ 17
Pregenerated content ................................................................................. 18
Pregenerating: Pros and Cons ................................................................... 19
Creating pages on demand ........................................................................ 20
On demand - example ............................................................................... 21
On demand: Pros and Cons ....................................................................... 22
External Tools ........................................................................................... 23
Bytecode caches ........................................................................................ 24
Cache Benchmark ..................................................................................... 25
Squid ......................................................................................................... 26
SRM: Script Running Machine ................................................................. 27
lingerd ........................................................................................................ 28
Performance .............................................................................................. 29
Sockets ...................................................................................................... 30
File I/O ...................................................................................................... 31
CPU Usage ................................................................................................ 32
Regular Expressions .................................................................................. 33
SQL Queries .............................................................................................. 34
Database optimization ............................................................................... 35
Visitation model 1 ..................................................................................... 36
Visitation model 2 ..................................................................................... 37
Visitation model 3 ..................................................................................... 39
Visitation model 4 ..................................................................................... 40
Visitation model 5 ..................................................................................... 41
Thank You! ............................................................................................... 42