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?
In order to generate flame graphs, you need a profiler that can sample stack traces. There has historically been two types of profilers: System profilers: like Linux perf, which shows system code paths (eg, JVM GC, syscalls, TCP), but not Java methods. JVM profilers: like hprof, LJP, and commercial profilers.
Flame graphs have been adopted by many languages, products, and companies, including Netflix, and have become a standard tool for performance analysis. They were published in "The Flame Graph" article in the June 2016 issue of Communications of the ACM, by their creator, Brendan Gregg.
A flame graph visualizes a distributed request trace and represents each service call that occurred during the request's execution path with a timed, color-coded, horizontal bar. Flame graphs for distributed traces include error and latency data to help developers identify and fix bottlenecks in their applications.
Flame graphs can be interpreted as follows: The top edge of the flame graph shows the function that was running on the CPU when the stack trace was collected. For CPU profiles, this is the function that is directly consuming CPU cycles.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With