Profiling PHP from your Mac
Published: 2020-06-04 12:58:47

---
title: Profiling PHP from your Mac
published: true
description: 
tags: 
//cover_image: https://direct_url_to_image.jpg
---

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
```bash
# 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:
```bash
$ 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:
```bash
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:
```bash
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.

```bash
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:

```bash
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:
```bash
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:

![Screenshot of qcachegrind](https://dev-to-uploads.s3.amazonaws.com/i/l93a3zsqi8gqhocz6s2k.png)

Now with it knowingly working, you can edit your `php.ini` to disable profiler always and set it to enable trigger.

```bash
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](https://chrome.google.com/webstore/detail/xdebug-helper/eadndfjplgieldjbigjakmdgkmoaaaoc) to toggle it when needed.