I'm going to use a few open source tools to accomplish this, but alternatively, blackfire.io is an awesome service that handles all this setup for you if you'd rather pay to skip the elaborate setup.
The tools we're going to use are xdebug which will log/profile each part of your application to a file and then qcachegrind will let you visually inspect that log to find where the slow downs are happening.
So on a mac, to get started run
# To install xdebug
pecl install xdebug
At the end of the xdebug install you'll see something like:
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so
Where it's telling you to add that to your php.ini file, to find where your php.ini file is, run php -i | grep "Configuration File"
and you should see something like:
$ php -i | grep "Configuration File"
Configuration File (php.ini) Path => /usr/local/etc/php
Loaded Configuration File => (none)
Now just append that zend_extension line from earlier:
echo 'zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so' >> /usr/local/etc/php/php.ini
Now run php -v
and you should see:
root@api-local:/srv/app# php -v
PHP 7.4.5 (cli) (built: Apr 23 2020 16:44:34) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans
with Zend OPcache v7.4.5, Copyright (c), by Zend Technologies
root@api-local:/srv/app#
So now with xdebug installed, let's set it up to start profiling requests.
vi /usr/local/etc/php/php.ini
Make yours look something like this now:
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so
xdebug.profiler_enable=1
xdebug.profiler_enable_trigger=0
xdebug.profiler_output_dir=/tmp
Notice how xdebug.profiler_enable=1
is on, and xdebug.profiler_enable_trigger=0
is off, that's cause we want to make sure this works and later we can add a chrome extension so we'll only profile when we need.
So with xdebug.profiler_enable=1
enabled, hit a page on your application and make sure you see something like this in your tmp directory:
root@api-local:/srv/app# ls /tmp/cachegrind*
/tmp/cachegrind.out.10812
root@api-local:/srv/app#
Now install qcachegrind
so you can inspect the dumped profile:
brew install qcachegrind
With that installed, use your spotlight shortcut (cmd+spacebar) and type qcachegrind to find and open the visualizer tool, and use the tool to open the /tmp/cachegrind.out.10812
file that gets made:
Now with it knowingly working, you can edit your php.ini
to disable profiler always and set it to enable trigger.
vi /usr/local/etc/php/php.ini
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so
xdebug.profiler_enable=0
xdebug.profiler_enable_trigger=1
xdebug.profiler_output_dir=/tmp
Then use a extension like xdebug helper for chrome to toggle it when needed.
Top comments (1)
For PHP users, there is a tool called ServBay.dev that provides a much easier way, especially for beginners. It handles all versions of PHP, MariaDB, PostgreSQL, as well as Redis and Memcached. You can run multiple PHP instances simultaneously and switch between them effortlessly, without the need for any environment setup. This tool has simplified my PHP development and is definitely worth a try!