In this article, we will learn about role of Profiler in Magento 2.
In Magento 2, We can use a built-in profiler with Magento to perform tasks such as analyzing performance.
It can tell us in intricate detail which blocks of code are used in the loading of a slow page, and how long it takes each block to complete its task.
The nature of profiling depends on the analytical tools we use. Currently, Magento supports multiple formats like HTML, CSV or Firebug.
Enable the Profiler in Magento 2:
There are two ways to enable the Profiler in Magento 2:
1. By Using .htaccess file: We can enable the profiler by adding following statement in .htaccess file:
SetEnv MAGE_PROFILER <type>
where, <type> can be “csvfile” or “firebug” or “html”.
In case of “csvfile”, you can find the report in var/log/ dir.
2. By using CLI: You can run following command to enable the profiler:
php bin/magento dev:profiler:enable <type>
where, <type> can be “html” or “csvfile” or “flagfile”.
In case of “flagfile”, a flagfile named as profiler.flag will be created inside var/ directory.
Magento Company ? Read More
Note: If you will not mention any <type> while executing the command, then it will enable the “html” type profiler by default.
If you have enabled the “html” type profiler. Then it always display the result on footer section of web page, you can see in the following image:
Disable the Profiler in Magento 2:
1. By Using .htaccess file: Remove the following statement from .htaccess file:
SetEnv MAGE_PROFILER <type>
2. By Using CLI: You can run following command to disable the profiler and remove the flagfile:
php bin/magento dev:profiler:disable
Adding Custom Profiler in specific code:
Sometimes, we need to measure the performance of custom code/script, then in that case we can use custom profiler.
To add custom profiler in custom code, Magento provides \Magento\Framework\Profiler class which have two methods: start() and stop().
start(): In this method, we pass the custom profiler name as parameter, which will measure the performance of the code for example execution time etc.
stop(): In this method, we pass the custom profiler name as parameter which have to be stopped.
\Magento\Framework\Profiler::start('custom-profiler-name'); /*** * write your code here for which custom profiler will calculate the execution time or measure the performance of code **/ \Magento\Framework\Profiler::stop('custom-profiler-name');
We can add a custom profiler as following example:
/**Custom Profiler starts here**/ \Magento\Framework\Profiler::start('custom-profiler-name'); /**custom script statements**/ $resultPage = $this->resultPageFactory->create(); $resultPage->getConfig()->getTitle()->set(__("Example")); \Magento\Framework\Profiler::stop('custom-profiler-name'); /**Custom Profiler ends here**/
If you have enabled the “html” type profiler then you can see your custom profiler as following image:
In html output profiler, the columns are as follows:
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.
Cnt: Short for 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.
Hope this will help you to analyze the performance of the code.