05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Before you begin optimization, ask yourself whether you need to optimize at all. Too<br />

many programmers have wasted hours wondering whether a complex series of string<br />

function calls are faster or slower than a single Perl regular expression, when the<br />

page that this code is in is viewed once every five minutes. Optimization is necessary<br />

only when a page takes so long to load that the user perceives it as slow. Often this is<br />

a symptom of a very popular site—if requests for a page come in fast enough, the<br />

time it takes to generate that page can mean the difference between prompt delivery<br />

and server overload.<br />

Once you’ve decided that your page needs optimization, you can move on to working<br />

out exactly what is slow. You can use the techniques in the upcoming “Profiling”<br />

section to time the various subroutines or logical units of your page. This will give<br />

you an idea of which parts of your page are taking the longest time to produce—<br />

these parts are where you should focus your optimization efforts. If a page is taking 5<br />

seconds to produce, you’ll never get it down to 2 seconds by optimizing a function<br />

that accounts for only 0.25 seconds of the total time. Identify the biggest time-wasting<br />

blocks of code and focus on them. Time the page and the pieces you’re optimizing,<br />

to make sure your changes are having a positive and not negative effect.<br />

Finally, know when to quit. Sometimes there is an absolute limit for the speed at<br />

which you can get something to run. In these circumstances, the only way to get better<br />

performance is to throw new hardware at the problem. The solution might turn out<br />

to be faster machines, or more web servers with a reverse-proxy cache in front of them.<br />

Benchmarking<br />

If you’re using Apache, you can use the Apache benchmarking utility, ab, to do highlevel<br />

performance testing. To use it, run:<br />

$ /usr/local/apache/bin/ab -c 10 -n 1000 http://localhost/info.php<br />

This command tests the speed of the <strong>PHP</strong> script info.php 1,000 times, with 10 concurrent<br />

requests running at any given time. The benchmarking tool returns various<br />

information about the test, including the slowest, fastest, and average load times.<br />

You can compare those values to a static HTML page to see how quickly your script<br />

performs.<br />

For example, here’s the output from 1,000 fetches of a page that simply calls<br />

phpinfo( ):<br />

This is ApacheBench, Version 1.3d apache-1.3<br />

Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd,<br />

http://www.zeustech.net/<br />

Copyright (c) 1998-2001 The Apache Group, http://www.apache.org/<br />

Benchmarking localhost (be patient)<br />

Completed 100 requests<br />

Completed 200 requests<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Performance Tuning | 309

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!