In Magento 2 the profiler can easily be turned on with an environment variable

SetEnv MAGE_PROFILER [output]

with [output] one of "html", "firebug", or "csvfile"

Now if I want to profile a CLI script, i.e. a bin/magento command, this does not work. Of course, the "firebug" and "html" output formats do not make much sense, but "csvfile" does, which writes the profile result in var/log/profiler.csv.

I tried

MAGE_PROFILER=csvfile bin/magento

to no avail.

I found this code in app/bootstrap.php that checks for the MAGE_PROFILER environment variable:

if (!empty($_SERVER['MAGE_PROFILER'])
    && isset($_SERVER['HTTP_ACCEPT'])
    && strpos($_SERVER['HTTP_ACCEPT'], 'text/html') !== false
) {
        !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'

And apparently it only activates the profiler for HTTP requests (excluding AJAX request)

This works:

HTTP_ACCEPT="text/html" MAGE_PROFILER=csvfile bin/magento

But it's a mere workaround. Is there another, more standard way to profile CLI calls?

1 Answer 1


Is there another, more standard way to profile CLI calls?

Apparently not, so I'll add my workaround as an answer:

HTTP_ACCEPT="text/html" MAGE_PROFILER=csvfile bin/magento
  • This is of course correct, unless you would like to consider using Performance Monitoring Tools like: Blackfire.io and/or NewRelic (This would required extra changes: docs.newrelic.com/docs/agents/php-agent/troubleshooting/…) in terms of CRON Jobs and CLI Commands Aug 27, 2018 at 13:51
  • Can you provide some more information on where/which file we need to apply this workaround Apr 23, 2020 at 4:10
  • @AbhimanyuSingh No file at all, this is a shell command. It sets the environment variables HTTP_ACCEPT and MAGE_PROFILER for the execution of bin/magento Apr 23, 2020 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.