Profiling PHP application with Docker and Xhprof

Volodymyr Ustinov
2 min readJun 22, 2021

Today i am going to share some info that can be useful for debugging and profiling yours PHP applications. In modern web development with PHP we can use different profiling systems like Blackfire, Xdebug and others. I am going highlight Xhprof. Its kind of standard profiler that gives you info about usage memory, cpu, count of calls and so on, and this can help you define some bottlenecks of your application. And also there is GUI that show Callgraph.

I am running my application inside Docker, so i will cover usage Xhprof with Docker too.

My Dockerfile looks like these:

Starting from line 18 we did installation and config our profiler. And we should provide a file xhprof.ini with content:

date.timezone = "Europe/London"
xhprof.output_dir = "/profiles"

this set storage folder for profiling data.

Xhprof provided with its own system for view profiling data, so wee need setup web server to be able run it. I am using Nginx, and config for it looks like that:

Next step will be for docker-compose for combining all together:

As you can see we use latest Nginx image and pass inside 2 config files for php application and for Xhprof. And we need make volume for xhprof files and use this volume with Nginx container.
Now when all is setup, for make profiling we need turn it on like this.
put this line before code what you are going to profile:

xhprof_enable(XHPROF_FLAGS_MEMORY + XHPROF_FLAGS_CPU);

and for gather info we need store it like this:

file_put_contents('/profiles/'.time().'.application.xhprof', serialize(xhprof_disable()));

Thant all. Now we can run our application and go to uri regarding config file of Nginx: http://localhost:8081/index.php

And i got a list with profile data for every run:

and on click we can see our profiling data:

and finally Callgraph:

For using this Graph we need install extension that calls: graphviz.
That’s it, hope someone will find it useful.

--

--

Volodymyr Ustinov

Hi, I’m a software engineer. My career started at 2012. My common ecosystem is backend with PHP