-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
src/Symfony/Bridge/Doctrine/Profiler/DoctrineDataCollector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
233
src/Symfony/Bridge/Doctrine/Profiler/DoctrineProfileData.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in 4.0 ?