Magento 2: Role of Profiler

Magento 2: Role of Profiler
May 21, 2020 526 view(s)
Magento 2: Role of Profiler

In this post, we will talk about the role of Profiler in Magento 2.
Profiler is used  to perform tasks such as analyzing performance in Magento 2.
It will let us know in intricate detail which blocks of code make page load slow, and how long it takes  to complete the task of each block.

The profiling's nature  depends on the analytical tools we use. Magento 2 supports multiple formats like HTMLCSV or Firebug.

How to enable the Profiler in Magento 2:

We have two ways to enable the Profiler in Magento 2:

1. The first way, we can add the following statement in .htaccess file to enable the profiler :

<type> can be “csvfile” or “firebug” or “html”.
if you use type as “csvfile”, you can find the report in var/log/ dir.

2. The second way, to enable the profiler by using CLI, you can run following command :

<type> can be “html” or “csvfile” or “flagfile”.
if you use type as “flagfile”, a file profiler.flag will be created inside var/ directory.

The “html” type profiler will be enable by default if you do not mention any <type> while executing the command.

The result will be shown on the footer of website when the "html" type of profiler is being enabled. It looks like in the following image:

How to disable the Profiler in Magento 2:

1. If you enabled the profiler using .htaccess file, now you can disable it by  removing the statement that you added:

2. If you enabled the profiler using CLI, now you can disable it by running the following command:


How to add Custom Profiler in specific code:

we can use custom profiler when we need to measure the performance of custom code/script.
There are two methods Magento provided in \Magento\Framework\Profiler class: start() and stop() to add custom profiler in custom code.

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.

We can add a custom profiler as following example:

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 analyse the performance of the code.
Thanks