Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Profiler on Console Command

Tags:

symfony

When running a Symfony app in the dev environment, the web debug toolbar allows me to see how many queries that Doctrine generated. Is there a similar profiler option for Console commands?

like image 672
Steven Musumeche Avatar asked Apr 18 '14 16:04

Steven Musumeche


People also ask

What is profiler in Symfony?

The profiler is a powerful development tool that gives detailed information about the execution of any request. Caution. Never enable the profiler in production environments as it will lead to major security vulnerabilities in your project.

How do I run console commands?

Easily open Command Prompt by running Windows Run by holding the Windows button and hitting the R button on your keyboard. You can then type "cmd" and press enter, opening Command Prompt. If you're unsure of what commands to use, you can type "Help" into Command Prompt.

What is bin console in Symfony?

The Symfony framework provides lots of commands through the bin/console script (e.g. the well-known bin/console cache:clear command). These commands are created with the Console component. You can also use it to create your own commands.

What is Symfony Console component?

The Console component eases the creation of beautiful and testable command line interfaces. The Console component allows you to create command-line commands. Your console commands can be used for any recurring task, such as cronjobs, imports, or other batch jobs.


1 Answers

First of all, symfony profiler depends on Request. Thats why its cant be used in console commands out of the box and, probably, it will not be fixed. Related symfony issue

But you still can access default DBAL profiling logger. It should be instance of Doctrine\DBAL\Logging\DebugStack It have public queries property, which hold all executed queries, parameters, execution time etc. Whenever you will need to debug actual queries - you can do in such a way

    /** @var $em Registry */
    $em = $this->getContainer()->get('doctrine')->getManager();
    $profiler = $this->getContainer()->get('doctrine.dbal.logger.profiling.default');

    $shop = $em->getRepository(Shop::class)->find(7);
    $sku = $em->getRepository(Sku::class)->find(45);

    // clear profiles data, we want to profile Criteria below
    $profiler->queries = [];
    $shopWares = $shop->getShopWarePagesForSku($sku);

    $output->writeln(var_export($profiler->queries));

It would generate output like

array (
      3 => 
      array (
        'sql' => 'SELECT ...... FROM ShopWarePage t0 WHERE (t0.sku_id = ? AND t0.sku_id = ?)',
        'params' => 
        array (
          0 => 45,
          1 => 7,
        ),
        'types' => 
        array (
          0 => 'integer',
          1 => 'integer',
        ),
        'executionMS' => 0.00075292587280273438,
      ),
    )
like image 105
Andrew Zhilin Avatar answered Oct 16 '22 06:10

Andrew Zhilin