Reading list Switch to dark mode

    Profiler in Magento 2

    Updated 6 April 2023

    How to Debug in Magento 2 using Profiler – In this article, we will learn about the role of Profiler in Magento 2 & debugging.
    In Magento 2, We can use a built-in profiler Magento to perform tasks such as debugging 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.

    Searching for an experienced
    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.

    * write your code here for which custom profiler will calculate the execution time or measure the performance of code

    We can add a custom profiler as following example:

    /**Custom Profiler starts here**/
    /**custom script statements**/
    $resultPage = $this->resultPageFactory->create();
    /**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.
    Thanks 🙂

    Previous Blog: How To Implement Custom Attribute Filter In Custom Product Collection Page In Magento 2

    Next Blog: Creating POST Method Controller in Magento 2.3

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home