Skip to content

Extract the profiler to a new component #14809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"symfony/monolog-bridge": "self.version",
"symfony/options-resolver": "self.version",
"symfony/process": "self.version",
"symfony/profiler": "self.version",
"symfony/property-access": "self.version",
"symfony/property-info": "self.version",
"symfony/proxy-manager-bridge": "self.version",
Expand Down Expand Up @@ -84,6 +85,7 @@
"doctrine/doctrine-bundle": "~1.2",
"monolog/monolog": "~1.11",
"ocramius/proxy-manager": "~0.4|~1.0",
"zendframework/zend-stdlib": "~2.4.8",
"egulias/email-validator": "~1.2",
"phpdocumentor/reflection": "^1.0.7"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* DoctrineDataCollector.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @deprecated since 2.8, to be removed in 3.0. Use Symfony\Bridge\Doctrine\Profiler\DoctrineDataCollector instead.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in 4.0 ?

*/
class DoctrineDataCollector extends DataCollector
{
Expand Down
130 changes: 130 additions & 0 deletions src/Symfony/Bridge/Doctrine/Profiler/DoctrineDataCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Profiler;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\DBAL\Types\Type;
use Symfony\Component\Profiler\DataCollector\DataCollectorInterface;

/**
* DoctrineDataCollector.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class DoctrineDataCollector implements DataCollectorInterface
{
private $registry;
private $loggers = array();

public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
}

/**
* Adds the stack logger for a connection.
*
* @param string $name
* @param DebugStack $logger
*/
public function addLogger($name, DebugStack $logger)
{
$this->loggers[$name] = $logger;
}

/**
* {@inheritdoc}
*/
public function getCollectedData()
{
$queries = array();
foreach ($this->loggers as $name => $logger) {
$queries[$name] = $this->sanitizeQueries($name, $logger->queries);
}

return new DoctrineProfileData($queries, $this->registry);
}

private function sanitizeQueries($connectionName, array $queries)
{
foreach ($queries as $i => $query) {
$queries[$i] = $this->sanitizeQuery($connectionName, $query);
}

return $queries;
}

private function sanitizeQuery($connectionName, $query)
{
$query['explainable'] = true;
if (!is_array($query['params'])) {
$query['params'] = array($query['params']);
}
foreach ($query['params'] as $j => $param) {
if (isset($query['types'][$j])) {
// Transform the param according to the type
$type = $query['types'][$j];
if (is_string($type)) {
$type = Type::getType($type);
}
if ($type instanceof Type) {
$query['types'][$j] = $type->getBindingType();
$param = $type->convertToDatabaseValue($param, $this->registry->getConnection($connectionName)->getDatabasePlatform());
}
}

list($query['params'][$j], $explainable) = $this->sanitizeParam($param);
if (!$explainable) {
$query['explainable'] = false;
}
}

return $query;
}

/**
* Sanitizes a param.
*
* The return value is an array with the sanitized value and a boolean
* indicating if the original value was kept (allowing to use the sanitized
* value to explain the query).
*
* @param mixed $var
*
* @return array
*/
private function sanitizeParam($var)
{
if (is_object($var)) {
return array(sprintf('Object(%s)', get_class($var)), false);
}

if (is_array($var)) {
$a = array();
$original = true;
foreach ($var as $k => $v) {
list($value, $orig) = $this->sanitizeParam($v);
$original = $original && $orig;
$a[$k] = $value;
}

return array($a, $original);
}

if (is_resource($var)) {
return array(sprintf('Resource(%s)', get_resource_type($var)), false);
}

return array($var, true);
}
}
233 changes: 233 additions & 0 deletions src/Symfony/Bridge/Doctrine/Profiler/DoctrineProfileData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
<?php

namespace Symfony\Bridge\Doctrine\Profiler;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Tools\SchemaValidator;
use Doctrine\ORM\Version;
use Symfony\Component\Profiler\ProfileData\ProfileDataInterface;

class DoctrineProfileData implements ProfileDataInterface
{
private $queries;
private $connections;
private $managers;
private $invalidEntityCount;
private $cacheEnabled = false;
private $cacheLogEnabled = false;
private $cacheCounts = array('puts' => 0, 'hits' => 0, 'misses' => 0);
private $cacheRegions = array('puts' => array(), 'hits' => array(), 'misses' => array());
private $errors = array();
private $entities = array();

public function __construct(array $queries, ManagerRegistry $registry)
{
$this->queries = $queries;
$this->connections = $registry->getConnectionNames();
$this->managers = $registry->getManagerNames();

/*
* @var string
* @var \Doctrine\ORM\EntityManager
*/
foreach ($registry->getManagers() as $name => $em) {
$this->entities[$name] = array();
/** @var $factory \Doctrine\ORM\Mapping\ClassMetadataFactory */
$factory = $em->getMetadataFactory();
$validator = new SchemaValidator($em);

/** @var $class \Doctrine\ORM\Mapping\ClassMetadataInfo */
foreach ($factory->getLoadedMetadata() as $class) {
if (!isset($entities[$name][$class->getName()])) {
$classErrors = $validator->validateClass($class);
$this->entities[$name][$class->getName()] = $class->getName();

if (!empty($classErrors)) {
$this->errors[$name][$class->getName()] = $classErrors;
}
}
}

if (version_compare(Version::VERSION, '2.5.0-DEV') < 0) {
continue;
}

/** @var $emConfig \Doctrine\ORM\Configuration */
$emConfig = $em->getConfiguration();
$slcEnabled = $emConfig->isSecondLevelCacheEnabled();

if (!$slcEnabled) {
continue;
}

$this->cacheEnabled = true;

/** @var $cacheConfiguration \Doctrine\ORM\Cache\CacheConfiguration */
$cacheConfiguration = $emConfig->getSecondLevelCacheConfiguration();
/** @var $cacheLoggerChain \Doctrine\ORM\Cache\Logging\CacheLoggerChain */
$cacheLoggerChain = $cacheConfiguration->getCacheLogger();

if (!$cacheLoggerChain || !$cacheLoggerChain->getLogger('statistics')) {
continue;
}

/** @var $cacheLoggerStats \Doctrine\ORM\Cache\Logging\StatisticsCacheLogger */
$cacheLoggerStats = $cacheLoggerChain->getLogger('statistics');
$this->cacheLogEnabled = true;

$this->cacheCounts['puts'] += $cacheLoggerStats->getPutCount();
$this->cacheCounts['hits'] += $cacheLoggerStats->getHitCount();
$this->cacheCounts['misses'] += $cacheLoggerStats->getMissCount();

foreach ($cacheLoggerStats->getRegionsPut() as $key => $value) {
if (!isset($this->cacheRegions['hits'][$key])) {
$this->cacheRegions['hits'][$key] = 0;
}

$this->cacheRegions['puts'][$key] += $value;
}

foreach ($cacheLoggerStats->getRegionsHit() as $key => $value) {
if (!isset($this->cacheRegions['hits'][$key])) {
$this->cacheRegions['hits'][$key] = 0;
}

$this->cacheRegions['hits'][$key] += $value;
}

foreach ($cacheLoggerStats->getRegionsMiss() as $key => $value) {
if (!isset($this->cacheRegions['misses'][$key])) {
$this->cacheRegions['misses'][$key] = 0;
}

$this->cacheRegions['misses'][$key] += $value;
}
}
}

public function getManagers()
{
return $this->managers;
}

public function getConnections()
{
return $this->connections;
}

public function getQueryCount()
{
return array_sum(array_map('count', $this->queries));
}

public function getQueries()
{
return $this->queries;
}

public function getTime()
{
$time = 0;
foreach ($this->queries as $queries) {
foreach ($queries as $query) {
$time += $query['executionMS'];
}
}

return $time;
}

public function getEntities()
{
return $this->entities;
}

public function getMappingErrors()
{
return $this->errors;
}

public function getCacheHitsCount()
{
return $this->cacheCounts['hits'];
}

public function getCachePutsCount()
{
return $this->cacheCounts['puts'];
}

public function getCacheMissesCount()
{
return $this->cacheCounts['misses'];
}

public function getCacheEnabled()
{
return $this->cacheEnabled;
}

public function getCacheRegions()
{
return $this->cacheRegions;
}

public function getCacheCounts()
{
return $this->cacheCounts;
}

public function getInvalidEntityCount()
{
if (null === $this->invalidEntityCount) {
$this->invalidEntityCount = array_sum(array_map('count', $this->errors));
}

return $this->invalidEntityCount;
}

/**
* {@inheritdoc}
*/
public function serialize()
{
return serialize(array(
'queries' => $this->queries,
'connections' => $this->connections,
'managers' => $this->managers,
'invalidEntityCount' => $this->invalidEntityCount,
'cacheEnabled' => $this->cacheEnabled,
'cacheLogEnabled' => $this->cacheLogEnabled,
'cacheCounts' => $this->cacheCounts,
'cacheRegions' => $this->cacheRegions,
'errors' => $this->errors,
'entities' => $this->entities,
));
}

/**
* {@inheritdoc}
*/
public function unserialize($serialized)
{
$unserialized = unserialize($serialized);
$this->queries = $unserialized['queries'];
$this->connections = $unserialized['connections'];
$this->managers = $unserialized['managers'];
$this->invalidEntityCount = $unserialized['invalidEntityCount'];
$this->cacheEnabled = $unserialized['cacheEnabled'];
$this->cacheLogEnabled = $unserialized['cacheLogEnabled'];
$this->cacheCounts = $unserialized['cacheCounts'];
$this->cacheRegions = $unserialized['cacheRegions'];
$this->errors = $unserialized['errors'];
$this->entities = $unserialized['entities'];
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'db';
}
}
Loading