Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

PHP Optimize

The document discusses various techniques for boosting the performance of PHP applications, including reducing output size, optimizing HTML, CSS, HTTP requests, using compression, and implementing server-side caching through techniques like caching pages, pregenerating content, and creating pages on demand. The bulk of the document is slides from a presentation covering these topics in further detail with examples and pros and cons of different approaches.

Uploaded by

wincarpstudio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

PHP Optimize

The document discusses various techniques for boosting the performance of PHP applications, including reducing output size, optimizing HTML, CSS, HTTP requests, using compression, and implementing server-side caching through techniques like caching pages, pregenerating content, and creating pages on demand. The bulk of the document is slides from a presentation covering these topics in further detail with examples and pros and cons of different approaches.

Uploaded by

wincarpstudio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Boosting The Performance of your

PHP Applications
PHP International Conference 2002

November 4, 2002. Frankfurt, Germany

Sterling Hughes & Derick Rethans <br />


<sterling@bumblebury.com /
d.rethans@jdimedia.nl>
Slide 1/40 November 4, 2002
Introduction

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

o Use preprocessors to eliminate<br>whitespace


o If you save 20 bytes per file, and have 1,000,000 hits a day, you've just saved 20,000,000 extra
bytes
o Send content "when ready", and use progressive images

-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

o Use CSS instead of incessant <font> tag usage


o All CSS should be contained in an external file that can be cached.
o Use short names for CSS attributes
o Override global HTML tag attributes using CSS properties
o Use CSS to control table definitions with CSS v2...

<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.

Apache Mod_Rewrite Entry

RewriteMap abbr dbm:/etc/apache/abbr


RewriteRule ^/r/([^/])/?(.) ${abbr:$1}$2
[redirect=permanent,last]

/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

o Caching dynamic content


o Pregenerating content
o Creating pages on demand

- 11 -
Slide 11/40 November 4, 2002
Do not use PHP!

PHP...
&nbsp;&nbsp;&nbsp;...leads to slower pages.
Slower pages...
&nbsp;&nbsp;&nbsp;...lead to fewer users.
Fewer users...
&nbsp;&nbsp;&nbsp;...lead to less income.

- 12 -
Slide 12/40 November 4, 2002
Static rulez!

How dynamic is your content?

- 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');

/ Set a key for this cache item /


$id = 'newsitem1';

/ Set a few options /


$options = array(
'cacheDir' => '/var/cache/news/',
'lifeTime' => 3600
);

/ Create a Cache_Lite object /


$Cache_Lite = new Cache_Lite($options);

/ Test if there is a valid cache-entry for this key /


if ($data = $Cache_Lite->get($id)) {
/ Cache hit! /
} else {
/ Cache miss! /
ob_start();
/ Generate content /
$data = ob_get_contents();
$Cache_Lite->save($data);
}
? >

- 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

o Only regenerate page when needed


o Different elements

- 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

o Only regenerate page when requested


o Most pages are never requested
o Use the 404-trick

- 20 -
Slide 20/40 November 4, 2002
On demand - example
Facts:

Url: http://yoursite.com/news/000001.html
Path: /home/httpd/news/

Set up a 404 error handler:


<Directory /home/httpd/news>
ErrorDocument 404 /generate-news.php
</Directory>

generate-news.php:
<?php
$id = basename($_SERVER['REDIRECT_URL'], '.html');

/ Generate the HTML /


include "/home/httpd/news/generator.php";
$html = generate_html($data);

/ Show the page /


echo $html;

/ Store the page for later use /


$fp = fopen(sprintf("/home/httpd/news/%06d.html", $id), "w");
fputs($fp, $html);
fclose($fp);
? >

- 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

o Cache compiled PHP scripts


o Serializing caches:
o APC (Alternative PHP Cache)
o 'SHM' caches:
o ionCube Accelerator
o Zend Cache

- 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

Some effort to use with PHP:


<?php
$tsstring = gmdate("D, d M Y H:i:s ", $timestamp)."GMT";

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

o Functions to work with persistent Objects


o Bridge between PHP Client and Objects
o Objects run in threads
o Manage persistent resources and data

- 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';

for ($i = 0; $i < 20; ++$i) {


print $ar['name'];
}
? >

o Shorten "required" executions, such as loops.


o Avoid rebuilding large data structures.
o Look for alternatives to regular expressions

- 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.

With Regular Expression


<?php
if (preg_match ('/(.)@(.)/', $data, $matches)) {
print $matches[2];
}
? >

Without Regular Expressions


<?php
$pos = strrpos ('@', $data);
if ($pos !== false) {
print substr ($data, $pos);
}
? >

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

o Queries are slow


o Recursive queries are slower
o Recursive queries on every page are slowest

- 35 -
Slide 35/40 November 4, 2002
Visitation model 1
Directory structure:

Stored as: <table class="db" border="1" align="center" cellspacing="0"> <tr>


<th>Id</th><th>Parent</th><th>Name</th> </tr> <tr> <td>3</td><td>1</td><td
class="left">Countries</td> </tr> <tr> <td>4</td><td>3</td><td class="left">Belgium</td>
</tr> <tr> <td>5</td><td>3</td><td class="left">Netherlands</td> </tr> <tr>
<td>6</td><td>3</td><td class="left">Germany</td> </tr> <tr> <td>7</td><td>5</td><td
class="left">Business and Economy</td> </tr> </table>

Retrieve list with:


<?php
function gimmeParent($id) {
global $dir;

$res = mysql_query("SELECT parent, name FROM directory WHERE id = $id");


if (mysql_num_rows($res) > 0) {
$dir[] = ($row = mysql_fetch_row($res));
gimmeParent($row['parent']);
}
}

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

Select the path:

Query:
SELECT * FROM directory
WHERE
left < 5 AND right > 6

- 39 -
Slide 38/40 November 4, 2002
Visitation model 4

Select all end points:

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!

These Slides: http://pres.derickrethans.nl

HTTP Cache Explanation: http://www.mnot.net/cache_docs/


HTML Tidy: http://www.w3.org/People/Raggett/tidy/
ionCube: http://www.ioncube.com
lingerd: http://www.iagora.com/about/software/lingerd
mod_gzip: http://www.remotecommunications.com/apache/mod_gzip/
PHP: http://www.php.net
squid: http://www.squid-cache.org
Webreference HTML Optimization: http://www.webreference.com/authoring/languages/html/optimize/
Zend: http://www.zend.com

- 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

You might also like