Before you can find php bottlenecks, you must have the following prerequisites installed:
sudo apt-get install apache2 php5 libapache2-mod-php5 git-core vim
Xdebug’s built-in profiler allows you to find bottlenecks in your script and visualize those with an external tool such as KCacheGrind or WinCacheGrind.
In this case we will be using Webgrind as our “visualizer”.
Open a terminal window and copy `Ctrl+C` & paste `Ctrl+Shift+V` the following into a terminal:
sudo apt-get install php5-xdebug
You can set Xdebug to profile in one of two ways:
1. Profile every script
This method profiles every php script you ever run. Depending on the size of your cachegrind files you may inadvertently fill up your HDD if you go this route. Add the following line to the end of `/etc/php5/apache2/php.ini` to profile every script:
xdebug.profiler_enable = 1
2. Profile with GET/POST (preferred)
This method only profiles pages that have the XDEBUG_PROFILE request variable set. This is my preferred method.Add the following line to the end of `/etc/php5/apache2/php.ini` to profile with GET/POST:
xdebug.profiler_enable_trigger = 1
Don’t forget to restart Apache
sudo service apache2 restart
Webgrind is a Xdebug profiling web frontend in PHP5. It implements a subset of the features of kcachegrind and installs in seconds and works on all platforms.
For the purpose of this tutorial **YOURWEBROOT** refers to your preferred webgrind installation directory (example: `/var/www/webgrind`), and **webgrind.dev** refers to your preferred local domain name. Be sure that Apache has permission to access YOURWEBROOT, whereever that might be.
You can install Webgrind by:
Downloading the latest archive and extracting it to YOURWEBROOT.
Cloning the git repository to YOURWEBROOT (preferred).
git clone https://github.com/jokkedk/webgrind.git YOURWEBROOT
Note: you can easily update webgrind in the future by running the following command in YOURWEBROOT:
git pull origin master
Create a new configuration file in `/etc/apache2/sites-available` with your preferred text editor (vi/vim or nano). You will need `sudo` privelages for this.
cd /etc/apache2/sites-available sudo vim webgrind.conf
Copy `Ctrl+C` & paste `Ctrl+Shift+V` the following into `webgrind.conf`.
<Virtualhost *:80> DocumentRoot YOURWEBROOT ServerName webgrind.dev </VirtualHost>
Update **YOURWEBROOT** and **webgrind.dev** to suit your needs and save the file.
Enable webgrind.dev and restart Apache.
sudo a2ensite webgrind.conf sudo service apache2 restart
Update Your Hosts File
Add the following line to `/etc/hosts`.
Start Finding Bottlenecks
Open your favorite webbrowser, chromium in my case, go to `http://webgrind.dev` and start profiling.
The first drop down specifies the percentage of processing time for which webgrind will return function calls. For example, the default is `90%` which only returns the functions which consume the top `90%` of processing time. To include every function, change this to `100%`. To only include functions which consume the most processing time (aka: the top `10%`), change this to something like `10%`. Make sense? Good.
The second specifies which cachegrind file to process. If you don’t see one you may need to invoke your script by visiting the URL of the project it’s associated with (or maybe you forgot the XDEBUG_PROFILE request variable…?).
The third specifies weather to return cost as percentages, milliseconds, or microseconds.