This document discusses profiling PHP applications to improve performance. It recommends profiling during development to identify inefficiencies. The document introduces Xdebug for profiling PHP code and Webgrind, a PHP frontend for visualizing Xdebug profiles. It provides an example of profiling a sample PHP application, identifying issues, making code changes, and verifying performance improvements through re-profiling.
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Profiling PHP with Xdebug / Webgrind
1. Profiling PHP
Applications
Sam Keen
@samkeen
pdxphp.org: May 2009 Meeting
2. Summary
• Huge topic, so we will settle on one aspect
of it
• Concentrate on the Code aspect of
profiling
• We’ll concern ourselves more with ‘tools
to get started’ rather than ‘Preferment
code best practices’ (see last slide for that)
• Will introduce a ‘secret ingredient’ that
makes profiling super delicious!
3. The Scenario
You build a killer PHP site
Works great out of the gate
but then slowly degrades as
more people use it until...
4.
5. How do we avoid this
Or at least lesson the chance of it happening or at a
minimum reduce the amount of “material” hitting the fan
when it inevitably occurs
6. Profiling
- should be done during development
- allows you to spot inefficiencies and
bottlenecks in code rather than your
clients
Photo: http://www.flickr.com/photos/chermida/2913511936/
7. Facilitating Profiling
During Development
• Make it as easy as possible to set up and
use
• This way it can become part of your daily
(OK, maybe weekly) routine.
9. First steps for Db and
System/Network
Query logs: slow and index-less
top, vmstat, dstat
$ dstat
@see http://dag.wieers.com/home-made/dstat
10. Profiling Code
-Baseline-
Before you make changes, you need to get some sort of
baseline of the performance of the application
Otherwise, you cannot measure improvement
So start with profiling the site as a “whole”
11. Web Server
Profiling Tools
Apache Bench
HTTP_load
Siege
Web Server
13. HTTP_Load
After creating for URL file (simple txt file with list o URLs
(one per line) that will be randomly chosen from by
http_load)
The run something like: (runs for ten seconds, with five
parallel requests)
$ http_load -parallel 5 -seconds 10 urls.txt
14. HTTP_Load
Output
$ http_load -parallel 5 -seconds 10 urls.txt
90 fetches, 5 max parallel, 805770 bytes, in 10 seconds
8953 mean bytes/connection
8.99999 fetches/sec, 80576.9 bytes/sec
msecs/connect: 241.704 mean, 958.418 max, 73.01 min
msecs/first-response: 252.075 mean, 1067.61 max, 83.833 min
HTTP response codes:
code 200 -- 90
15. Xdebug Profiling
Far more than just a profiler:
* stack traces and function traces in error messages with:
o full parameter display for user defined functions
o function name, file name and line indications
o support for member functions
* memory allocation
* protection for infinite recursions
* profiling information for PHP scripts
* code coverage analysis
* debug your scripts interactively with a debug client
@see xdebug.org
18. Xdebug Local Setup
php.ini@ -> /usr/local/php5/lib/php.ini.zenddebug
php.ini.xdebug
php.ini.xdebug.profile
php.ini.zenddebug
~/bin (in my PATH)
-rwxr-xr-x@ php-xdebug*
-rwxr-xr-x@ php-xdebug-profile*
-rwxr-xr-x@ php-zenddebug*
Contents of php-xdebug-profile
#!/bin/sh
rm /usr/local/php5/lib/php.ini
ln -s /usr/local/php5/lib/php.ini.xdebug.profile /usr/local/php5/lib/php.ini
sudo apachectl restart
$ php-xdebug-profile
Now running php with Xdebug PROFILE: hurray for open source
19. Profiling a specific page
With Xdebug profiling enabled
Simply request the web page in question using browser
Look in your xdebug.profiler_output_dir for output
cachegrind.out.1242152836-_Library_WebServer_Documents_persist_better_see_signups_php
20. Examine the output
• Traditionally: Kcachegrind
• Install on Linux, or Windows: easy
• Install on OSX: #&^!*&^!!
• The Kcachegrind UI...
23. Webgrind
Webgrind is an Xdebug profiling web frontend in PHP5
@see http://code.google.com/p/webgrind/
Simple Installation on any platform that can run
WebServer/PHP5 stack
and the UI...
24.
25. Webgrind Install
Install (Typical WebApp install: put the folder in your webroot
and edit a config file)
1. Download to your web root
* edit config.php
* be sure $storageDir is writable by web server
26. Extend UI
UI is HTML and js (jquery) so trivial to make changes
28. BaseLine
$ http_load -parallel 5 -seconds 10 urls_could_improve.txt
1928 fetches, 5 max parallel, 1.92362e+07 bytes, in 10.0002 seconds
9977.27 mean bytes/connection
192.796 fetches/sec, 1.92358e+06 bytes/sec
msecs/connect: 0.456549 mean, 9.326 max, 0.055 min
msecs/first-response: 21.7894 mean, 599.711 max, 0.862 min
HTTP response codes:
code 200 -- 1928
*you would also be watching CPU and RAM with something like
dstat during this test to determine if we are CPU and/or memory
bound (see resources on last slide)
32. Push Work to Client
Push all this work to the client
$('dd.note').each(function(i){
$(this).html($(this).text().replace(/(eb)/ig,
'<span style=quot;color:red;font-weight:800;quot;>$1</span>')
);
});
33. Make Adjustments
• Switch to PDO
• typically lean towards php built-ins that abstract a
great deal of functionality (rather than libs built in
php).
• Use static (.htm) pages if we don’t need DB
• output buffer caching another alternative
• Use js to ‘markup’ content
• fastest way a web server can do work is not to
do it
changes took about 40 min of work