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?


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
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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