Skip to main content

Debug Magento 2 using PHP-SPX profiler timeline view

php-spx profiler, timeline profile for php
Using modern PHP frameworks or building online stores based on popular cms can be easy and fast. Developers can install any production-ready platform, add some extensions, import data, configure payment methods and a new online store is ready.

However, installing many 3rd-party modules on top of the original system can affect the performance significantly.

Magento 2 is such a system that can easily lose its fragile performance after installation of a few 3rd-party modules or writing not efficient code.

What Magento 2 offers to a developer out-of-box:

- MAGE_PROFILER

- DB queries logger

Those tools can point to some problem places, but you will never find the exact problem with them.

What PHP world offers:

- xDebug // tracing or profiling and building CallGraphs or flamegraph can show you some problem methods and their call-stacks

- Xhprof // has less overhead, but it’s hard to visualize data xhgui or webgrind may require some time and experience to set up

What paid services offer:

- tideways.com // powerful paid toll

- blackfire.io // good set of tools to visualize PHP profiling data, but only Call Graph is available in free “Hack” plan

The PHP-SPX profiler with a built-in web interface.

Surfing on GitHub I found interesting php extension, a few minutes later I decide to try it for Magento 2. The installation is simple and fast.

I added it to my docker container like this:

RUN cd /usr/lib && git clone https://github.com/NoiseByNorthwest/php-spx.git

RUN cd /usr/lib/php-spx && phpize && ./configure && make && make install

Then added some configuration in php.ini

#SPX profiler

extension = /usr/lib/php-spx/modules/spx.so

spx.http_enabled = 1

spx.http_key = “dev”

spx.http_ip_whitelist = “*”

spx.data_dir = /tmp/spx

*I use it for local profiling. The author does not recommend use it on live server due to security reasons.

Then by opening an app locally with certain parameters in URL you can see the web-interface.

http://magento2.local/?SPX_UI_URI=/&SPX_KEY=dev

Let’s enable profiler and see how Magento 2 resolved the favicon.ico file.

The profiler opens three windows with different views: timeline, flatprofile, flamegraph. The most interesting is the Timeline. We can switch to the category view color scheme and try to find slow methods.

Now we see that execution of 

Magento\Framework\App\FrontController\Interceptor::dispatch

and

Magento\Framework\View\Result\Page\Interceptor::renderResult

methods takes the most of the rendering time. From them we can start checking the child methods and know more about problem places in the codebase. Obviously given images show us system classes and methods that we will not optimize, likewise they give us better understanding about magento 2 internals and how the request is being processed by the application.

Usage of PHP-SPX is simple and the profiler works with low overhead.

Visualizing requests in PHP-SPX timeline can speed up discovering of problem places or just give us a better view on how the php application works. It also supports debugging of the console php commands.

Run commands with the parameters SPX_ENABLED and SPX_REPORT

SPX_REPORT=full SPX_ENABLED=1 php bin/magento


See profile log in the panel

Now, we can look through the timeline view and notice that during console initialization Magento 2 loads all modules twice, that can be a potential place for improvement.

We use php-spx profiler in our local development and test environment.
Check out our article about docker here: https://webmageic.blogspot.com/2020/10/php-magento-docker-development.html

You can learn more about the profiler and its features on GitHub page https://github.com/NoiseByNorthwest/php-spx

I also recommend you to check this short course to learn more about different metrics and views https://symfonycasts.com/screencast/blackfire


Comments

  1. I found your post while searching for some related information on blog search...It's a good post..keep posting and update the information. Learn more about Cloud Based Magento Store Development services.

    ReplyDelete
  2. Good informative article. We offer various services like custom software, magento development, digital marketing, enterprise solution, etc.

    ReplyDelete

Post a Comment

Popular posts from this blog

PHP Magento Docker development environment

     Working with PHP projects usually requires to have a local development environment. In this article we will show you how a typical docker-based environment for Magento 2.     Some containers will be based on the official Magento Cloud Docker  images.