【问题标题】:Symfony StopWatch events not appearing in profiler timelineSymfony 秒表事件未出现在分析器时间线中
【发布时间】:2014-10-18 06:32:09
【问题描述】:

我正在尝试将其他时间信息添加到 Symfony Profiler Timeline 中,但我无法显示任何内容。根据我阅读的文档,它应该像下面的示例一样简单,但这不会导致任何额外的信息出现在时间轴上。

我是否需要以某种方式让探查器知道我正在开始和停止的事件?

use Symfony\Component\Stopwatch\Stopwatch;

class DefaultController extends Controller
{
    public function testAction()
    {
        $stopwatch = new Stopwatch();
        $stopwatch->start('testController');

        usleep(1000000);

        $response = new Response(
            '<body>Hi</body>',
            Response::HTTP_OK,
            array('content-type' => 'text/html')
        );

        $event = $stopwatch->stop('testController');

        return $response;
    }
}

【问题讨论】:

    标签: symfony symfony-stopwatch


    【解决方案1】:

    Symfony 的分析器无法扫描所有秒表实例的代码并将其放入时间线。您必须改用分析器提供的预配置秒表:

    public function testAction()
    {
        $stopwatch = $this->get('debug.stopwatch');
        $stopwatch->start('testController');
    
        // ...
    
        $event = $stopwatch->stop('testController');
    
        return $response;
    }
    

    但是,您的控制器已经在时间线上...

    【讨论】:

    • 他们应该在官方文档中添加这个,秒表页面上没有提到它。
    【解决方案2】:

    您应该在构造函数或特定函数中将 stopwacth 作为服务注入,因为在 Symfony 3.4 之后:服务默认为私有。

    testAction(\Symfony\Component\Stopwatch\Stopwatch $stopwatch) {
        $stopwatch->start('testController');
    
        // ...
    
        $event = $stopwatch->stop('testController');
    }
    

    【讨论】:

      猜你喜欢
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多