34

I learned about flame graphs and find them fascinating - however, I could find no useful reference on how to generate them for my PHP script(s). How can I gather the data and generate the graphs using PHP?

Levi Morrison
  • 18,096
  • 5
  • 59
  • 79
johndodo
  • 13,994
  • 13
  • 81
  • 105
  • 2
    Brendan Gregg has a [*lot to say*](http://www.brendangregg.com/FlameGraphs/cpuflamegraphs) about flame graphs, if that helps. Personally, in my opinion they are overrated. They tease you with pretty colored pixels, but things you could fix to speed up the code [*can easily hide from them*](http://stackoverflow.com/a/25870103/23771). – Mike Dunlavey Dec 10 '14 at 20:26
  • @MikeDunlavey: I don't care about pretty pixels either, but I also don't agree with your assessment of flame graphs as eye-candy. They make *some* (not all!) profiling tasks much easier, which is fine by me. So, do you know how I can make them for PHP web apps? :) – johndodo Dec 11 '14 at 12:20
  • If I wanted to make them, I would collect a lot of stack samples (on wall-clock time, not CPU). Considering each sample as a long string, I would just sort them in lexical order. Then assign a color to each individual function. Then it's just a matter of scanning through the sorted list and rendering each routine as a colored rectangle. My only point is, if you actually need speed, if even only one out of five speedups escapes your attention, it's going to prevent getting the speed you want by a large factor. Because after you clean out other ones, the ones you don't get become dominant. – Mike Dunlavey Dec 11 '14 at 15:02

2 Answers2

6

You can use Xdebug to create cachegrind profiles of your php code. You can take a look to this project that handles xdebug's cachegrind output using php.

n00dl3
  • 19,122
  • 6
  • 58
  • 74
  • 1
    If I understand correctly, the idea is to build a tool which parses cachegrind output and displays it in flame graphs? That sounds doable... – johndodo Dec 17 '14 at 07:35
  • It does notre seem there's a ready-to-use tool built with PHP, but maybe kcachegrind can do it... – n00dl3 Dec 17 '14 at 08:18
  • and to answer your question, yes, that's it ! – n00dl3 Dec 17 '14 at 08:49
1

I found this article from platform.sh where they use xhprof to output the data set from the execution calls, then a perl script called flamegraph.pl process the data set to be displayed as a SVG. A bit more digging and I was able to find the official flame graph repo on GitHub that contains the flamegraph.pl logic. The three resources combined I was able to generate a flame graph as desired though YMMV.

David J Eddy
  • 1,870
  • 1
  • 19
  • 31