magento 2 profiler

As a Magento 2 developer, you know how tedious performance debugging can be. Finding out why Magento is slow is not as easy as it seems. So any tools that help you on the way come in handy. In most cases, the Magento 2 profiler helps with the performance-related debugging best.

While you may use some third-party extensions for the job it takes time to find an effective solution fr your specific case. Thus, we'll look closer at the good old Magento 2 profiler and ways to improve it.

If you only start working with Magento, this is a great overview of the profiling functionality. Yet, adept developers can refresh their knowledge of the subject.

Let's get right to it.

What is Magento 2 Profiler?

Magento 2 profiler is a default tool for performance debugging. It gives insights into the loading time of various blocks as well as the amount of allocated and used memory to perform the task.

The profiler features a table with page blocks and resources used to load them. These details create a clear picture and allow you to spot the issues faster.

Profiler magento

From the detailed report, you can learn:

  • Timer Id — name of the executed block
  • Time —  time spent on executing the code in seconds
  • Avg —  average time spent on executing the code in seconds
  • Cnt —  number of times the block had to run to create the required result
  • Emalloc —  PHP memory allocated to this process
  • RealMem —  the real memory amount used to execute the process

The profiling described above is in the HTML format. However, you can also choose the CSV or Firebug type to view the code execution details in a file. We'll cover how to do it in the next section.

How to Enable Profiling in Magento 2?

Moving on, let's see how to enable Magento 2 profiling. There are two ways to opt for: editing the .htaccess file or running a CLI command.

Enable profiling in .htaccess file

To enable profiling using the .htaccess file:

1. Navigate to your Magento root directory and open the .htaccess file.

2. Add the below line to the file:

 SetEnv MAGE_PROFILER "type"

Note: Replace "type" with "html", "csv" or "firebug" depending on the desired profiler output.

The HTML profiler will be displayed at the bottom of your frontend pages. If you choose the CSV or Firebug input, you'll find the report in the var/log/profiler.csv file.

3. Switch your Magento to the developer mode:

php bin/magento deploy:mode:set developer

Just like that, the profiling is on.

Enable profiling via CLI

You may also enable profiling by running the below CLI command:

php bin/magento dev:profiler:enable <type>

Here, the output <type> can be either <html> or <csvfile>. So, don't forget to set the desired option.

Note: If you don't specify the output type, HTML will be set by default.

Once you run this command, the var/profiler.flag file is created. It is removed when you disable profiling later.

The profiler tells you what causes performance issues. Yet, it doesn't tell you why this happens. So, to see the root of the problem, you may need to improve the profiling process.

How to Enhance Magento 2 Profiler?

Most Magento shops utilize a great variety of extensions. While useful in one regard, they can modify the standard Magento processes and load productivity. So, one of the solutions is to check how long the plugins work.

You can achieve this by improving the regular Magento profiler.

1. Open Magento\Framework\Profiler () and find the public static function start($timerName, array $tags = null) function. Add the following:

static $wasLayoutRenderer = false;

before the function.

Adding code before Magento function

Then, insert the following code at the beginning of the same function:

if ($timerName == 'magento') {
 self::$wasLayoutRenderer = true;
}


if (!self::$wasLayoutRenderer && strpos($timerName, 'MFMETHOD - ') === 0) {
 return;
}

Magento function modification

Next, find the public static function stop($timerName = null) function and modify the first if:

if (!self::$_enabled || !self::_checkTags(self::_getTags())) {
    return;
}
if (empty(self::$_pathIndex[$timerName])) {
   return;
}

This is a result you should get:

Profiler enhancement

2. Navigate to Magento\PageCache\Model\Layout\LayoutPlugin () and find the public function afterGetOutput(Layout $subject, $result) method. Wrap the following line $tags[] = $block->getIdentities(); as shown below:

\Magento\Framework\Profiler::start('getIdentities ' . get_class($block) . ' ' . $block->getNameInLayout());
$tags[] = $block->getIdentities();
\Magento\Framework\Profiler::stop('getIdentities ' . get_class($block) . ' ' . $block->getNameInLayout());

Wrap Magento method

3. Open Magento\Framework\Interception\Interceptor (trait) () and find the protected function ___callPlugins($method, array $arguments, array $pluginInfo) function.

Then, wrap this string $beforeResult = $pluginInstance->$pluginMethod($this, ...array_values($arguments));

\Magento\Framework\Profiler::start('MFMETHOD - ' . get_class($this) . '::' . $method . ' | ' . get_class($pluginInstance) . '::' . $pluginMethod);
$beforeResult = $pluginInstance->$pluginMethod($this, ...array_values($arguments));
\Magento\Framework\Profiler::stop('MFMETHOD - ' . get_class($this) . '::' . $method . ' | ' . get_class($pluginInstance) . '::' . $pluginMethod);

Wrap the two below strings in this function the way we did above:

$result = $pluginInstance->$pluginMethod($subject, $next, ...array_values($arguments));

$result = $pluginInstance->$pluginMethod($subject, $result, ...array_values($arguments));

Magento 2 profiler enhancement

As a result, the enhanced profiler will look as follows:

Improved profiler in Magento 2

Marked in yellow are the loaded plugins. This way, you don't have to examine the code and spend countless hours looking for the problem. You can now see all the plugins that require attention right there. 

How to Disable Profiling in Magento 2?

Once you do the debugging and know what drags performance down, you can disable profiling. To do it run the CLI command:

bin/magento dev:profiler:disable

It will disable profiling and remove the profiler.flag file. You may switch to the production mode and continue with your day-to-day store operations.

Certainly, the best way to tackle the issue is to prevent it altogether. So, you may consider using the full-page cache or other solutions to make sure your store's performance is always up to mark.

With the tips on profiler enhancement, you will spend less time on debugging and concentrate on improving your store.