如何在页面加载期间获得Symfony2的原理查询统计信息 [英] How to get doctrine query stats from Symfony2 during page load

查看:109
本文介绍了如何在页面加载期间获得Symfony2的原理查询统计信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望在页面的页脚中添加一些统计信息,其中184ms是184ms / 6 / 10ms页面生成时间,6是查询计数,10是DB查询所花费的时间。



我可以解决页面生成时间,但如何获取数据库统计学士学位?



这当然是在应用程序环境中运行的时候,我很感激在app_dev中,可能有一种方法可以得到它作为symfony分析器正在运行。



任何帮助将不胜感激。

解决方案

首先在你的 config.yml

  doctrine:$ b中启用原则剖析$ b dbal:
...
profiling:true

创建Twig扩展类如下:

 <?php 
// src / AppBundle / Twig / AppExtension.pgp

命名空间AppBundle\Twig;

使用Doctrine\DBAL\Logging\DebugStack;

class AppExtension extends \Twig_Extension
{
/ **
* @var DebugStack
* /
protected $ debugStack;

/ **
* AppExtension构造函数。
* @param DebugStack $ debugStack
* /
函数__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']]),
];
}

/ **
*返回应用程序执行时间
*
* @param int $ decimals
* @return string
* /
public function requestTime($ decimals = 0)
{
return number_format((microtime(true) - $ _SERVER ['REQUEST_TIME_FLOAT'])* 1000,$ decimals);
}

/ **
*返回原则查询执行时间
*
* @param int $ decimals
* @return string
* /
public function queryTime($ decimals = 2)
{
return number_format(array_sum(array_column($ this-> debugStack-> queries,'executionMS')) * 1000,$小数);
}

/ **
*返回原则查询计数
*
* @return string
* /
public function queryCount()
{
return count($ this-> debugStack-> queries);
}
}

服务中注册您的分机.yml

 服务:
app.twig_extension:
class:AppBundle \Twig\AppExtension
参数:[@ doctrine.dbal.logger.profiling.default]
public:false
标签:
- {name:twig.extension

在twig主模板中使用它,如下所示:



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



记住:



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



最佳做法是将它们放在主模板的末尾。如果你想在开始时使用 CSS 显示它们。



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


I'd like to add a little stat to the footer of the page, along the lines of '184ms/6/10ms'.

Where 184ms is the page generation time, 6 is the query count and 10 is the time taken for DB queries.

I can work out the page generation time, but how do I get the database stats out of doctrine?

This would be when running in the app environment of course, I appreciate when in app_dev, there probably is a way to get at it as the symfony profiler is running.

Any help would be appreciated.

解决方案

First enable doctrine profiling in your config.yml

doctrine:
    dbal:
        ...
        profiling: true

Create Twig Extension class like this:

<?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);
    }
}

Register your extension in services.yml

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

Use it in twig main template as follow:

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

Remember:

If you put {{ query_count() }} or {{ query_time() }} in the beginning of your template, it will not show you all queries.

Best practice will be to put them in the end of your main template. If you want to show them in the beginning use CSS.

Live Demo: https://mysql-todolist.tk

这篇关于如何在页面加载期间获得Symfony2的原理查询统计信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆