如何使用 Blackfire 分析 PHP shell 脚本应用程序或工作程序

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

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

我应该做什么改变?

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

那是因为只有在close()方法被调用时才会将数据发送给agent(除非你杀死它,否则它会在程序结束时自动调用)。

您可以使用与 Blackfire 探测器捆绑在一起的 BlackfireProbe class 手动检测一些代码:

// 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 实用程序激活。如果不是,所有调用都将转换为 noops。

我不知道,也许在 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 次迭代并发送最后一个探测。之后它将停止分析。