如何配置一个PHP shell脚本应用程序或工作器使用Blackfire


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

我注意到,当我有一个无尽的工人我不能配置PHP shell脚本。因为当它被杀死时,它不会发送探测器。

我该做什么改变?

当你试图分析一个正在运行无限循环的worker时。在这种情况下,您必须手动编辑代码以删除无限循环,或者手动调用探测器的close()方法(https://blackfire.io/doc/manual-instrumentation)。

这是因为只有在调用close()方法时才会将数据发送给代理(除非您终止了该方法,否则它会在程序结束时自动调用)。

你可以使用BlackfireProbe类手动检测一些代码,BlackfireProbe类与Blackfire的探针捆绑在一起:

// 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();

与自动检测一样,只有当代码通过Companion或blackfire CLI实用程序运行时,分析才会被激活。

我不知道,也许在2015年下面的页面不存在,但现在你可以通过以下方式做分析:https://blackfire.io/docs/24-days/17-php-sdk

$blackfire = new LoopClient(new Client(), 10);
$blackfire->setSignal(SIGUSR1);
$blackfire->attachReference(7);
$blackfire->promoteReferenceSignal(SIGUSR2);
for (;;) {
    $blackfire->startLoop($profileConfig);
    consume();
    $blackfire->endLoop();
    usleep(400000);
}

现在你可以发送信号SIGUSR1到这个worker的进程,LoopClient将开始分析。它将监听方法consume的10次迭代,并发送最后一次探测。