Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate flame graphs with PHP?

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?

like image 687
johndodo Avatar asked Nov 20 '14 11:11

johndodo


People also ask

How do you run a flame graph?

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.

Who created Flame graphs?

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.

What are flame graphs?

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.

How do you read a Flamegraph?

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.


Video Answer


2 Answers

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.

like image 91
n00dl3 Avatar answered Oct 06 '22 05:10

n00dl3


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.

like image 39
David J Eddy Avatar answered Oct 06 '22 06:10

David J Eddy