I have a cron task in Magento that I need to profile to determine memory usage problems. The script runs during the night, and crashes after half an hour or so due to consuming all available memory.
I need to profile the script to determine which functions are consuming too much memory.
I have tried using the Aoe_Profiler, but when trying to profile a cron task, there is nowhere to view the output, as the cron task does not invoke the MVC, thus the profiler’s view block can’t be rendered.
How can a cron task be profiled in Magento?
A ‘trick’ we have used from time to time in running and debugging/testing cron scripts from the command line is to copy
cron.phpthat lives in the root folder of the Magento application to a new script that represents the cron task that you wish to execute, egxyz_cron_task.php.Down the bottom of that script is a
try/catchblock:This can be changed as follows, to run just the cron task you’re interested in:
Now the cron task can be executed from the command line, or the browser. You can visit the page
http://yourdomain/xyz_cron_task.php, and the cron task will be executed (although you won’t see anything interesting yet).To get the profiling working (assuming you have already enabled profiling in the Admin section), add the following code after
require 'app/Mage.php';:This will enable profiling, but you still won’t see the profiler output, as there is no view model to render it.
You can output the profiler block by adding one line to the
try/catchblock as follows:You should now see the profiler output as follows (assuming you’re using the Aoe_Profiler extension):
Now you can the appropriate
Varien_Profiler::start()/stop()code to your cron task as required.Happy profiling!