Sunday, 6 January 2013

Drupal and Symfony with XDebug and XHProf


After a long week I have hardly any manna to do magic. So I decided to have another run with code analytics tools on Drupal and Symfony. Whenever you need code performance check or want to see what is happening and what are the numbers, times and memory consumptions - you need XDebug or XHProf.


On OS-X / MAMP I had XDebug by default (with a little help - I'll explain later) and managed to install XHProf as mentioned yesterday. As an extra I added graphviz using Homebrew so I can see the nice graphs produced by XHProf stack trace.

For the XDebug profiles I always use kCacheGrind. I know there is WinCacheGrind and MacCallGrind and WebGrind - but the KDE version is just too good. So I installed Lubuntu just for that - I've found that the most handy and responsive Ubuntu distribution lately. I'm still confused why Unity exists. Anyway.

Setting up XHProf is fairly easy. I've found 2 main use cases so far. You either inspect your code on demand with wrapping code blocks:

xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

// Do what you have to do. make_me_sandwich(); and whatevar.
$xhprof_data = xhprof_disable(); var_dump($xhprof_data);

Then you have a nice output of stack trace. The more structured way is to save it and inspect with the XHProf UI tools. For that you do the same with a little extra:

xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

// Do what you have to do. make_me_sandwich(); and whatevar.
$xhprof_data = xhprof_disable(); $XHPROF_ROOT = PLACE_WHERE_I_PUT_IT;
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php"; include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
$xhprof_default = new \XHProfRuns_Default(); $run_id = $xhprof_default->save_run($xhprof_data, "UNIQUE_SESSION_ID");
// var_dump($run_id); - or something

Then you call your publicly available XHProf folder with the url:

http://PATH/xhprof_html/?run=RUN_ID&source=UNIQUE_SESSION_ID

This post is only about some nice pictures so I don't go into a deep structural analysis. Enjoy the results:

Fig 1.: Drupal call stack from index.php (anonymous) (full image)

Fig 2.: Symfony call stack from app_dev.php (anonymous) (full image)


Fig 3.: Drupal call graph

Fig 4.: Symfony call graph

Fig 5.: Drupal full bootstrap (anonymous)

Fig 6.: Symfony bootstrap (anonymous)

Maybe one thing that was interesting. In Drupal when seeing the heavy stuff I've found DB queries and file load obviously. In Symfony however the most used functions are core data manipulations - string handlers. Maybe because of the DI and PHP Reflection layers?

---

Do you know about any other visualisation tools?

Peter

1 comment: