Profiler in simple language can be considered as a program, which helps to check and report the performance of another program. Magento 2 is also using a profiler for the same purpose. If we need to check the performance of any specific script or need to check the performance of a complete page, we can simply enable the profiler and look at the output. Magento 2 provides 2 types of profiles i.e. HTML, CSV.
Enable/Disable profiler
There are two ways to enable profiler in Magento 2. The first way is to enable the profiler using the SetEnv variable, we can set this variable in .htaccess like
SetEnv MAGE_PROFILER <type>
Where can be one of the following HTML or CSV based on your need, or we can set the value at the bootstrapping of Magento application through apache or Nginx servers. If you guys need more information about setting up variables at bootstrapping, please check out this link for more information.
To disable the profile, just comment a SetEnv MAGE_PROFILER from your .htaccess file or from your web server config and restart your web server.
There is another way to enable profiler in Magento 2 i.e. using a CLI.
To enable profiler, navigate to the Magento installation directory as the Magento file system owner, enter the following command to configure the profiler.
php bin\magento dev:profiler:enable <type>
Where can be one of the following HTML or CSV based on your need. To disable the profiler, execute the following command from your Magento root directory.
php bin/magento dev:profiler:disable
Once we enable the profiler in Magento, the application creates a flag file inside <magento_root>/var
directory as profiler.flag, this file contains the name of profiler type that we have set through CLI or MAGE_PROFILER variable.
HTML Profiler
We can enable the HTML profiler using the following command
php bin\magento dev:profiler:enable html
This command shows the output in your web browser at the bottom of your page.
Note: Just make sure that you are not using varnish or any other caching mechanism or disable before using the Magento profiler.
If you have enabled the “html” type profiler, then it always display the result on the footer section of a web page. You can see in the following image:
Once we enable the dev:profiler:enable
without specifying any param, Magento set HTML as a default profiler type.
We can find more detail about this command in Console/Command/ProfilerEnableCommand.php a class file where TYPE_DEFAULT
Is set as HTML. The output driver class resides under Magento’s profiler module i.e. Magento\Framework\Profiler\Driver\Standard\Output
CSV Profiler
CSV profiler type, it gives another way to check the performance of our application. When we trigger a CLI command dev:profiler:enable csvfile
the output will be saved to <project_root>/var/log/profiler.csv
. The profiler.csv will be overridden on each page refresh. The output driver class resides under Magento’s profiler module i.e. Profiler/Driver/Standard/Output/Csvfile.php
In output profiler, the columns are as follows: (you can find this variable defined in Profiler/Driver/Standard/Stat.php class as constant variables)
Timer Id: This gives the name of the block of code being executed.
Time: The time it took to complete in seconds.
Avg: The average time it took to complete in seconds.
Count: This represents the number of times this individual block ran to generate the output required.
Emalloc: The amount of memory PHP ( the programming language on which Magento runs) assigned to this single operation. This is again represented in bytes.
RealMem: The actual amount of memory used to perform the operation.
This is all about Magento profiling. Please let me know your thoughts about this blog down in the comment section, or, write me an email on admin@asolutions.co.in . That’s all for now, Happy coding
Top comments (1)
Profiler in Magento 2 is a powerful tool that provides insights into the performance of your Magento store. Profile provides Code Profiling, Database Query Analysis, Template Analysis, Custom Module Analysis, Debugging and Issue Resolution and Performance Testing and Benchmarking