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
) {
\Magento\Framework\Profiler::applyConfig(
$_SERVER['MAGE_PROFILER'],
BP,
!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?