Profiling with PHP

5422m4n

Sven Kanoldt

Posted on September 27, 2018

Profiling with PHP

There are basically 3 simple steps how to get to a profiling of your application to get performance insights.

1. Step xdebug setup

Check if your installation has xdebug support enabled:

-bash-4.1$ php --info | grep -i xdebug | grep enabled
xdebug support => enabled
Enter fullscreen mode Exit fullscreen mode

if that does not show anything then checkout the Manual

if it does not show enabled then checkout your php.ini and enable the extension.

2. Step create this bash alias

alias php-profile='php -d xdebug.profiler_enable=on -d xdebug.profiler_output_name=profile.out -d xdebug.profiler_output_dir=$PWD'
Enter fullscreen mode Exit fullscreen mode

3. Step create a PHPUnit test case that exercise your code

this is pretty simple as this snipped will illustrate it on a DNA class with the method hammingDistance that is our bottle neck to be profiled:

// filename: DNATest.php

require_once __DIR__.'/DNA.php';

final class DNATest extends \PHPUnit\Framework\TestCase
{
    public function test()
    {
        $dna = new DNA('GGACT');
        $distance = $dna->hammingDistance(new DNA('CGACT'));
        $this->assertThat($distance, $this->equalTo(1));
    }
}
Enter fullscreen mode Exit fullscreen mode

Finally run the profiling

run the profiling this way (assuming you have the phpunit.phar there):

php-profile phpunit.phar DNATest.php
Enter fullscreen mode Exit fullscreen mode

That will

  • run the test
  • produces a profile.out

That file contains all profiling data. It can be opened by PHPStorm or QCacheGrind.

However now you got a bunch of metrics, you need to first ignore all the profiling of PHPUnit and find the start of the invocation of your code.

What other approaches do you use to get profiling?
Please share your experience.

Thanks for reading!

💖 💪 🙅 🚩
5422m4n
Sven Kanoldt

Posted on September 27, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

🐘 Unit Tests in PHP
php 🐘 Unit Tests in PHP

January 15, 2019

Profiling with PHP
php Profiling with PHP

September 27, 2018