Bu örneğimizde xdebug profiler ve cachegrind ile Ubuntu sunucusununda çalışan uygulamamızın performans profil dosyalarını yaratacağız. Daha sonra Mac OS ortamında qcachegrind ve graphviz paketlerini kullanıp görselleştirme işlemini yapacağız. Aslında bu işlemlerin tamamını Ubuntu ortamında yapabilirsiniz ama ben vagrant kullandığım için görselleştirme işlemini Mac OS ortamında yapıyorum.


Mac OS hazırlığı


Ubuntu ortamında cachegrind ile yaratılan profil dosyalarını burada görselleştireceğiz.


# Install qcachegrind
$ brew install qcachegrind

# Install graphviz
$ brew install graphviz

Eğer terminalde $ qcachegrind komutunu çalıştırırsanız, qcachegrind görselleştirme penceresi açılacaktır.


Ubuntu


Burası PHP uygulamamızın çalıştığı ve cachegrind'in profil dosyalarını yarattığı yer.


# 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


Eğer Mac OS terminalinde $ qcachegrind komutunu çalıştırırsanız, qcachegrind görselleştirme penceresi açılacaktır. Açılan pencerede klasör simgesine tıklayın, cachegrind dosyaları bulunduğu yere gidin ve birini seçin. Bu kadar!