【问题标题】:How to profile a PHP shell script app or worker using Blackfire如何使用 Blackfire 分析 PHP shell 脚本应用程序或工作人员
【发布时间】:2015-06-04 13:49:02
【问题描述】:

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

我应该做些什么改变?

【问题讨论】:

    标签: php worker beanstalkd blackfire


    【解决方案1】:

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

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

    您可以使用 Blackfire 探测器附带的 BlackfireProbe 类手动检测一些代码:

    // 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。

    【讨论】:

    • 希望该页面始终存在,否则您可能应该包含该页面上的代码/文本,除非您是该网站的“所有者”;-) 您知道 Stack 对仅链接的感觉。
    【解决方案2】:

    我不知道,也许在 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 到该工作进程的进程,LoopClient 将开始分析。它将监听方法consume 的 10 次迭代并发送最后一个探测。之后它将停止分析。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多