【问题标题】:How to get doctrine query stats from Symfony2 during page load如何在页面加载期间从 Symfony2 获取教义查询统计信息
【发布时间】:2015-03-17 10:09:45
【问题描述】:

我想在页面的页脚添加一些统计信息,类似于“184ms/6/10ms”。

其中 184ms 是页面生成时间,6 是查询计数,10 是 DB 查询所用的时间。

我可以计算出页面生成时间,但是如何让数据库统计数据脱离原则?

这当然是在应用程序环境中运行时,我很欣赏在 app_dev 中,可能有一种方法可以在 symfony 分析器运行时得到它。

任何帮助将不胜感激。

【问题讨论】:

    标签: php symfony doctrine-orm


    【解决方案1】:

    您可以通过使用 DebugStack 对象来做到这一点。

    您将拥有一个包含每个查询及其各自执行时间的数组。

    例子:

        $doctrine = $this->get('doctrine');
        $doctrineConnection = $doctrine->getConnection();
        $stack = new \Doctrine\DBAL\Logging\DebugStack();
        $doctrineConnection->getConfiguration()->setSQLLogger($stack);
        $em = $doctrine->getManager();
    
        ... // Perform query
    
        var_dump($stack);
    

    $stack 示例的变量转储:

    Doctrine\DBAL\Logging\DebugStack Object
    (
        [queries] => Array
            (
                [1] => Array
                    (
                        [sql] => SELECT t0.id AS id1 FROM Test t0
                        [params] => Array
                            (
                            )
    
                        [types] => Array
                            (
                            )
    
                        [executionMS] => 0.00018191337585449
                    )
    
                [2] => Array
                    (
                        [sql] => SELECT t0.id AS id1 FROM Test t0
                        [params] => Array
                            (
                            )
    
                        [types] => Array
                            (
                            )
    
                        [executionMS] => 0.00016307830810547
                    )
    
            )
    
        [enabled] => 1
        [start] => 1426590420.2278
        [currentQuery] => 2
    )
    

    【讨论】:

    • 是的,虽然在生产环境中,当您为所有查询记录 SQL 时,这不是一个好方法,但我想要的只是执行时间。我想知道是否有一种方法可以实现您自己的“调试堆栈”,它只是抓住时间而不记录所有 SQL。不过,谢谢,它是正确方向的一个很好的指针。
    • 我已经设法让它工作,但它毫无意义。因为页面使用模板“继承”,所以页脚被渲染的点是页面渲染过程的一半,并且似乎不可能告诉 twig 最后渲染它。所以基本上,似乎不可能在 Symfony2 网站的正确时间显示准确的页面生成统计信息,除非有人可以告诉我。
    【解决方案2】:

    首先在您的 config.yml 中启用学说分析

    doctrine:
        dbal:
            ...
            profiling: true
    

    像这样创建 Twig 扩展类:

    <?php
    // src/AppBundle/Twig/AppExtension.pgp
    
    namespace AppBundle\Twig;
    
    use Doctrine\DBAL\Logging\DebugStack;
    
    class AppExtension extends \Twig_Extension
    {
        /**
         * @var DebugStack
         */
        protected $debugStack;
    
        /**
         * AppExtension constructor.
         * @param DebugStack $debugStack
         */
        function __construct(DebugStack $debugStack)
        {
            $this->debugStack = $debugStack;
        }
    
        public function getFunctions()
        {
            return [
                new \Twig_SimpleFunction('request_time', [$this, 'requestTime'], ['is_safe' => ['html']]),
                new \Twig_SimpleFunction('query_time', [$this, 'queryTime'], ['is_safe' => ['html']]),
                new \Twig_SimpleFunction('query_count', [$this, 'queryCount'], ['is_safe' => ['html']]),
            ];
        }
    
        /**
         * Returns application execution time
         *
         * @param int $decimals
         * @return string
         */
        public function requestTime($decimals = 0)
        {
            return number_format((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'])*1000, $decimals);
        }
    
        /**
         * Returns doctrine query execution time
         *
         * @param int $decimals
         * @return string
         */
        public function queryTime($decimals = 2)
        {
                return number_format(array_sum(array_column($this->debugStack->queries, 'executionMS'))*1000, $decimals);
        }
    
        /**
         * Returns doctrine query count
         *
         * @return string
         */
        public function queryCount()
        {
                return count($this->debugStack->queries);
        }
    }
    

    services.yml注册您的分机

    services:
        app.twig_extension:
            class: AppBundle\Twig\AppExtension
            arguments: ["@doctrine.dbal.logger.profiling.default"]
            public: false
            tags:
                - { name: twig.extension }
    

    在twig主模板中使用如下:

    {{ query_count() }}{{ query_time() }}{{ request_time() }}

    记住:

    如果您将{{ query_count() }}{{ query_time() }} 放在模板的开头,它不会显示所有查询。

    最佳做法是将它们放在主模板的末尾。如果您想在一开始就显示它们,请使用 CSS

    现场演示: https://mysql-todolist.tk

    【讨论】:

      猜你喜欢
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 2016-05-16
      • 1970-01-01
      • 2012-01-06
      • 2021-03-10
      • 2023-01-04
      相关资源
      最近更新 更多