In this example we are going to create cachegrind profile files in Ubuntu server where our application is running. Afterwards we will use Mac OS to visualise cachegrind profile files with the help from qcachegrind and graphviz packages. You can do whole process in Ubuntu but I am using vagrant box so I cannot open up a visualiser window.


Prepare Mac OS


This is where we visualise cachegrind profile files generated in Ubuntu environment.


# Install qcachegrind
$ brew install qcachegrind

# Install graphviz
$ brew install graphviz

If you run $ qcachegrind command, it will open qcachegrind visualiser window.


Ubuntu


This is where the PHP application runs and creates cachegrind profile files.


# Install xdebug
$ sudo apt-get install -y php-xdebug

# Enable xdebug profiler
$ sudo nano /etc/php/7.1/fpm/php.ini
xdebug.profiler_output_dir = /var/www/html/profiler-app/cachegrind
xdebug.profiler_enable = 1
xdebug.profiler_enable_trigger = 1

# Restart services
$ sudo service nginx restart
$ sudo service php7.1-fpm restart

# Create a dummy application folder
$ mkdir /var/www/html/profiler-app

# Create a dummy application file
$ nano /var/www/html/profiler-app/index.php
abstract class ParentClass
{
protected function parentMethod()
{
return rand(10, 100);
}
}

class ChildClass extends ParentClass
{
private $number;

public function __construct($number)
{
$this->number = $number;
}

public function getArg()
{
return $this->number;
}

public function getMultiplification()
{
return $this->number * parent::parentMethod();
}
}

$childClass = new ChildClass(3);
echo $childClass->getArg();

for ($i = 0; $i < 3; $i++) {
echo $childClass->getMultiplification();
}

# Create folder to keep cachegrind profiler files
$ mkdir /var/www/html/profiler-app/cachegrind

# Call application to generate cachegrind profiler files
$ curl localhost/profiler-app/?XDEBUG_PROFILE=1

# Check cachegrind profiler files
$ ls -l /var/www/html/profiler-app/cachegrind
-rw-r--r-- 1 501 dialout 486 Apr 10 14:08 cachegrind.out.13192

# See the content of the file
$ cat cachegrind/cachegrind.out.13192
version: 1
creator: xdebug 2.6.0 (PHP 7.1.13-1+ubuntu16.04.1+deb.sury.org+1)
cmd: /var/www/html/profiler-app/index.php
...
...

Test


If you run $ qcachegrind command on Mac OS terminal it will open up the visualiser window. Click folder icon, navigate to the path where cachegrind files are stored and select the file. Done!