Chapter 21Speeding Up PHP Applications IN THIS CHAPTER ◆ Benchmarking your PHP application ◆ Stress-testing your PHP application ◆ Compressing your PHP application output ◆ Using output
Trang 1Tuning and Securing PHP
Applications
CHAPTER 21
Speeding Up PHP Applications
CHAPTER 22
Securing PHP Applications
Part VI
Trang 3Chapter 21
Speeding Up PHP Applications
IN THIS CHAPTER
◆ Benchmarking your PHP application
◆ Stress-testing your PHP application
◆ Compressing your PHP application output
◆ Using output caching using jpcache
◆ Using output caching using the PEAR cache
◆ Using function caching using the PEAR cache
◆ Using PHP opcode caching techniques
T HIS CHAPTER DESCRIBES HOW YOUcan speed up your PHP applications using vari-ous techniques, including fine-tuning code, output buffering, output compression, output caching, and code caching These techniques will enable you to turbocharge your application for the high-volume access scenarios usually present in heavy-traffic Web sites with PHP applications
Optimization isn’t a task that should be undertaken on every piece of code.
You must ask yourself, before starting to optimize code, “is this code fast enough?” If the answer is “yes,” optimization probably isn’t necessary.
Spending time optimizing existing code could be time wasted if you neglect other tasks for the optimization time The best advice is to use good tech-niques while constructing the code in the first place and only optimize code that actually needs it.
713
Trang 4Benchmarking Your PHP Application
Most Web programming is done quickly, and often carelessly When someone needs
a new Web application, notifying the developers is often the last priority Once the developers are notified, the application gets the “was needed yesterday” status Therefore, developers design quick-and-dirty applications, and lack the necessary time to fine-tune the code
When you plan to develop a new application, try to allocate one-third of your project time to fine-tuning your code The first step in fine-tuning your code is identifying the most commonly used code segments You can easily do this by adding spurious print statements to your code or enabling logging/debugging for critical segments of your applications
Once you have identified the segments of code that are most commonly required
to service a request, you need to identify any elements that are not operating at optimal speed
To identify slow code, you should review your code as frequently as possible, using the benchmarking techniques described in the following section
Note that optimizing code won’t always improve performance It’s impor-tant to consider the whole picture when you are experiencing performance problems — if your database is maxed out, your bandwidth not adequate for your traffic, or hardware not keeping up with the demand, optimizing code won’t improve a thing.
Benchmarking your code
The PEAR package discussed in Chapter 4 includes a set of benchmark classes that you can use to benchmark your code without writing a lot of new code For exam-ple, Listing 21-1 shows a PHP script that benchmarks a function called
myFunction
Listing 21-1: bench1.php
<?php // If you have installed PEAR packages in // a different directory than %DocumentRoot%/pear // change the setting below.
$PEAR_DIR = $_SERVER[‘DOCUMENT_ROOT’] ‘/pear’ ;
$PATH = $PEAR_DIR;
714 Part VI: Tuning and Securing PHP Applications
Trang 5ini_set( ‘include_path’, ‘:’
$PATH ‘:’ ini_get(‘include_path’));
require_once ‘Benchmark/Iterate.php’;
$benchmark = new Benchmark_Iterate;
$benchmark->run(10, ‘myFunction’, $argument);
$result = $benchmark->get();
echo “<pre>”;
print_r($result);
echo “</pre>”;
exit;
function myFunction($var) { // do something
echo ‘x ‘;
}
?>
The $PEAR_DIR variable points to the PEAR directory, which in this case is installed in %DocumentRoot%/pear The $PEAR_DIR variable is included in the include_path using the ini_set()call Then the Benchmark/Iterate.phpclass is loaded into the application
A benchmark Iterate object called $benchmark is created This object is used to run the myFunction function 10 times The $argument variable is passed to
myFunction each time it is called The profiling result of the multiple execution,
$result, is retrieved using the get()method of the benchmark object The result is output to the screen using the print_r() function A sample of typical output looks as follows:
x x x x x x x x x x Array
( [1] => 0.00074100494384766 [2] => 0.00013399124145508 [3] => 0.00013101100921631 [4] => 0.0001380443572998 [5] => 0.00014901161193848 [6] => 0.00013506412506104 [7] => 0.00013101100921631 [8] => 0.00013399124145508
Chapter 21: Speeding Up PHP Applications 715