Quick profiling with Blackfire.io and DDEV

Tarek Djebali
3 min readJul 21, 2020

--

Imagine, one day, after few hours of implementing a new feature, you need one last test before pushing your code… The output sounds correct but… The page takes more than a minute to load!

No big deal! This is probably an internal network issue as usual. Let’s push to staging… Loading … Still loading… Gateway timeout! 😱

Choosing a profiler

Working with PHP, I had to pick one of:

I went with blackfire.io because A) it has better integration with DDEV and B) a better Timeline & Graph views UI.

Setup the profiler

Create a profile on Blackfire.io

First, you have to log in to Blackfire.io and create an organization and an environment.

Fig 01: Create an environment — The environment endpoint is the DDEV project URL
Fig 02: Server ID and Token — Will be used to setup the Agent.

DDEV integration

1- Add Blackfire.io service as DDEV additional service

$ cd .ddev$ curl https://raw.githubusercontent.com/drud/ddev-contrib/master/docker-compose-services/blackfire/docker-compose.blackfire.yaml > docker-compose.blackfire.yaml

2- Edit docker-compose.blackfire.yaml and replace YOUR_SERVER_ID and YOUR_SERVER_TOKEN with values from Fig 02

3- Add custom PHP configuration. This will ensure that the probe and agent can communicate.

$ mkdir php && curl https://raw.githubusercontent.com/drud/ddev-contrib/master/docker-compose-services/blackfire/php/blackfire.ini > php/blackfire.ini

4- Restart DDEV

$ ddev restart

5- Install Blackfire.io browser extension

And we are done!

Demo

For the sake of the demo, I wrote a simple script that simulates slow execution time and high memory/CPU usage. You can find the code used for the demo on Github.

Profiling results

Fig 03: Call graph from Blackfire.io Dashboard.

We can clearly see that the total execution time is 14.5s spent as follow:

  • slowFunction: 68.86% of the execution time.
  • highCpu: 26,87% of the execution tile.

If we go deeper inside function calls, we can see that it reflects exactly what happened inside the script.

Fig 04: SlowFunction metrics show a 10s execution time
Fig 05: highCpu metrics shows CPU Time cost
Fig 06: highMemory metrics show memory usage

Conclusion

Thanks to DDEV and Blackfire.io you can easily set up a profiler, analyze your code execution, and fix performance issues.

For further reading, you can visit the links below.

Resources:

--

--