After a looong time I decided to start “documenting” my php ideas and techniques in this personal blog.
Just to start with something, I wanted to share with you a simple profiling technique which includes xdebug and webgrind.
I’m using a mac and I will not get into details on how to setup php and xdebug. You can use the default php version that MacOS comes with, or handle different versions with homebrew, there are plenty of tutorials out there.
To start, clone webgrind from it’s github repo (Webgrind) in a folder accessible from your web server. On my machine this will be /Users/argi/Sites/php/webgrind.
Accessing this directory from your web browser the default interface with load.
Producing a cachegrind file using xdebug it’s really really simple.
You can use the command php -d xdebug.profiler_enable=On php_script.php . To make this a bit more convinient add the alias command: {% highlight bash %}alias phpp=“php -d xdebug.profiler_enable=On “{% endhighlight %} in your .bash_profile file under the home directory. Opening a new console will have the phpp command available. Just execute any script you like and it will have a file generated for this. (the debug output folder is configurable but default settings should work just fine)
![cmd]({{ site.url }}/assets/media/phpp_command.png)
Going back to the browser and webgrind the generated file will be available to select it.
![webgrind selection]({{ site.url }}/assets/media/webgrind_file_selection.png)
Playing a bit with the settings you can get really useful information on function calls, execution time, and many many more…
![webgrind results]({{ site.url }}/assets/media/webgrind_results.png)
Play with it and enjoy it :)
Argi