phpworkerbeanstalkdblackfire

How to profile a PHP shell script app or worker using Blackfire


I noticed that when I have an endless worker I cannot profile PHP shell scripts. Because when it's killed it doesn't send the probe.

What changes shall I do?


Solution

  • When you are trying to profile a worker which is running an endless loop. In this case you have to manually edit your code to either remove the endless loop or instrument your code to manually call the close() method of the probe (https://blackfire.io/doc/manual-instrumentation).

    That's because the data is sent to the agent only when the close() method is called (it is called automatically at the end of the program unless you killed it).

    You can manually instrument some code by using the BlackfireProbe class that comes bundled with the Blackfire's probe:

    // Get the probe main instance
    $probe = BlackfireProbe::getMainInstance();
    // start profiling the code
    $probe->enable();
    
    // Calling close() instead of disable() stops the profiling and forces the  collected data to be sent to Blackfire:
    
    // stop the profiling
    // send the result to Blackfire
    $probe->close();
    

    As with auto-instrumentation, profiling is only active when the code is run through the Companion or the blackfire CLI utility. If not, all calls are converted to noops.