[How2Tips] How to benchmark your code with xdebug

Published on

Jun 9, 2017

how2tips

Reading time : 1 min

Dr Klein shares with you his thoughts about how to profile your php scripts, in a post extracted from our internal knowledge database. Before we start, let’s just be aware that there are many ways to profile your apps :

  • xdebug
  • xhprof (by Facebook, almost dead)
  • FriendsOfPHP/uprofiler (renamed fork of xhprof, by Sensio, almost dead, prototype for blackfire)
  • xhmem (dead)
  • blackfire (Saas)
  • https://tideways.io/ (the php extension generates the same output as xhprof)

We'll focus on xdebug for now. Always think of what you're trying to achieve : in this case you are trying to isolate a performance bottleneck. It's thus a good idea to isolate the portion of code you're trying to benchmark in a separate script. This way you'll be able to run it via the CLI, which simplifies greatly the setup. Isolate your subject under test (SUT) : imagine you want to profile a Symfony service.

Just create a simple php script :

(bin/test.php):
<?php

require \_\_DIR\_\_.'/../vendor/autoload.php';

use App\\Symfony\\AppKernel;

$kernel = new AppKernel('dev', false);
$kernel->boot();
$container = $kernel->getContainer();

$container->get('expensive\_service')->run(); // whatever

Activate the xdebug profiler

RTFM: https://xdebug.com/docs/all The easiest is to punctually enable it via php -d :

php -dxdebug.profiler\_enable=1 -dxdebug.profiler\_output\_dir=$(pwd) bin/test.php

Note: this will activate the profiler for this process only, and will put the cachegrind output in the current folder.

Cachegri- what??

Cachegrind is a format developed for valgrind (an analysis tool). The cool thing is that KcacheGrind, a KDE app, is the best cachegrind visualizer, and is available on Linux.

Visualize

Once you generated a cachegrind file, just run it with kcachegrind <cachegrind.out.whatever>. It will help you understand where php took most of its time. It also generates nice call graphs.

Running with docker?

First, grasp the basics. Don't forget that containers are just UNIX processes. Nothing magical. If you don't get UNIX processes, switch to Windows. Now that we're on the same page, remember that the container has a different filesystem tree than your host one. In order to access the cachegrind files generated by xdebug, you'll have to mount a volume, like in the following example:

docker exec -v $(pwd):/xdebug my\_image php -dxdebug.profiler\_enable=1 -dxdebug.profiler\_output\_dir=/xdebug /xdebug/bin/test.php

This will ask docker to mount your current folder as /xdebug, and it will dump the cachegrind files in it. But we diverge, because that has nothing to do with xdebug.

Troobleshoot

Always verify if xdebug is correctly configured: On your host:

php -i | grep profiler\_enable

Or with docker:

docker exec my\_image php -i | grep profiler\_enable

It should display :

xdebug.profiler\_enable => On => On

Any questions ? Ping us at @KNPLabs IMG_0626-ANIMATION

Written by

Florian Klein
Florian Klein

Comments