To improve Magento 2 performance, at first, you have to find the performance issues. To do that, you need to have a good profiler.
Profiling is the collection of data on the execution of various programs (files and functions). This is not a process of optimizing or changing programs. The result of this process is usually an extended report on the components of the program, statistics on the execution of functions, analyzing which, it is possible to implement optimization, both on the speed of execution of these or those programs, and on the quality of the executed code.
Every system performance slows down after it is loaded with more content and extensions. Magento 2 performance is no exception. The use of profiling becomes useful when the question of program optimization arises and you need to find out and understand which segments of code are running slowly, which need to be optimized, find broken code, etc. The code profiling procedure should go first in the application optimization process. Any other action will be conjecture and most likely wrong. You need to know what exactly causes problems and “brakes”. The result of profiling is usually a list of performed functions together with the time of their execution.
There are many PHP registration tools (Zend Xdebug, BlackFire, etc.), but not all tools are suitable for analysis directly on the working site. Most of them have big overheads and cannot be used in the production environment.
On the other hand, the problems on a “live” site may not be exactly the same as in the development environment. Profiling only on the developer’s computer will show only part of the problem.
Magento 2 provides a Profiler “out of the box”. However, in my practice, it has never been used. If you are interested, more information about native Magento 2 profiling you can find on the official doc.
Tideways XHProf Extension is a hierarchical Profiler for PHP, forked from the XHProf extension originally developed by Facebook. The Tideways is a convenient and simple profiler that can effectively collect application statistics almost without slowing down your application (or your site) and can be used for production Magento 2 website profiling with a little overhead.
To start working with Tideways, you need to create an account on Tideways:
Then, depends on your server environments, you need to install and configure this extension according to the official documentation:
The last step is to simply configure Magento 2 adding settings to a file app/bootstrap.php:
The API key can be found in General Settings in your Tideways account:
This completes the settings and we can start “researching the code”.
The Tideways provides several methods to trigger profiling:
(To download: Tideways Chrome extension)
Simply add the generated query string to profile the desired page:
The simplest way and to get around a Magento 2 FPC (Full Page Cache) is to trigger it by adding a query string.
We will not describe optimization methods for Magento 2 based on profiling, because this is a completely different topic, we will only consider the possibilities of the Tideways in terms of profiling. Also, we will omit the description of all settings and will focus only on the core profile capabilities.
Assume we had a Magento 2 project which needs to profile. For example, let’s trigger profiling for the home page. Add a query string, provided via Tideways, into the store home URL and refresh the page. Into the Tideways we will see the available Callgraph Traces:
The Timeline profiler collects information about a range of operations performed by the Magento 2 application. In this particular case, we can see that the request of our page returned an HTTP 200 OK response code, see that the request has been handled via Magento\Cms\Controller\Index\Index controller, Magento 2 generates 167 MySQL queries, the response of the page took 941 ms, consumed 50.2 MB of memory, etc. This main information you can see at the top of the page.
Also, for example, we can choose the SQL query which collects EAV (Entity–attribute–value) attribute options data from the block Magento\Swatches\Block\Product\Renderer\Listing\Configurable, and in the right sidebar, we will see all the necessary information about this query: time of execution, memory usage, the query itself, etc.
Another example shows the query which collects some products information to display on the page:
The Summary tab shows the functions with the time duration spent directly in that function, and with the total time duration spent in that function and in the functions which it called, and how many times it was called. The SQL functions are clickable to display the query itself. For example, we can see that query below executes 2 times with self duration 22.5ms and with a total duration of 22.5ms:
Another example shows that the Magento 2 event core_layout_render_elemenеt triggered 109 times with 22.3ms and 4.55ms respectively:
The Callgraph Profiler shows how much time was spent in each function as well as any functions which it calls, as a call graph. It can provide detailed insights into every function call of a PHP request. Let’s trigger one of the pages of the category and look at the results.
Resource-intensive areas of the code are highlighted in yellow (middle) and red (strongest). These are pieces of code that use a lot of resources relative to the rest of the program. This can be one slow function or a large number of fast function calls.
For example, we can see that for the call to function Magento\Catalog\Model\Layer\Filter\AbstractFilter::getItemsCount:
Also, we can see the parent calls, child calls, and slowest code path. The example above shows that the selected function has:
Each time a different function is selected we can see how each component of the view is interlinked and changes.
The Tideways automatically detects a number of bottlenecks in our requests and gives us helpful clarifications about them. The Bottlenecks tab may display slow or repeated HTTP calls, SQL calls, etc. In the example, we can see that the select, which is related to retrieving product information was executed 1 time and took 8.02 ms. All the detected bottlenecks will be highlighted in red:
Magento 2 is a complex system, but using such tools as Tideways give a clear idea of code execution in the context of its optimization. By creating a minimum load on the research environment to collect all the necessary data on the speed of work, the Tideways is a powerful tool for profile Magento 2 application and improve Magento 2 performance.
If you need a hand in improving your Magento 2 performance, please contact us to start discussing your requirements.