1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Doctrine Bundle |
5
|
|
|
* |
6
|
|
|
* The code was originally distributed inside the Symfony framework. |
7
|
|
|
* |
8
|
|
|
* (c) Fabien Potencier <[email protected]> |
9
|
|
|
* (c) Doctrine Project, Benjamin Eberlei <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\DataCollector; |
16
|
|
|
|
17
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
18
|
|
|
use Doctrine\ORM\Tools\SchemaValidator; |
19
|
|
|
use Doctrine\ORM\Version; |
20
|
|
|
use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector as BaseCollector; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* DoctrineDataCollector. |
26
|
|
|
* |
27
|
|
|
* @author Christophe Coevoet <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class DoctrineDataCollector extends BaseCollector |
30
|
|
|
{ |
31
|
|
|
private $registry; |
|
|
|
|
32
|
|
|
private $invalidEntityCount; |
33
|
|
|
|
34
|
|
|
public function __construct(ManagerRegistry $registry) |
35
|
|
|
{ |
36
|
|
|
$this->registry = $registry; |
37
|
|
|
|
38
|
|
|
parent::__construct($registry); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
45
|
|
|
{ |
46
|
|
|
parent::collect($request, $response, $exception); |
47
|
|
|
|
48
|
|
|
$errors = array(); |
49
|
|
|
$entities = array(); |
50
|
|
|
$caches = array( |
51
|
|
|
'enabled' => false, |
52
|
|
|
'log_enabled' => false, |
53
|
|
|
'counts' => array( |
54
|
|
|
'puts' => 0, |
55
|
|
|
'hits' => 0, |
56
|
|
|
'misses' => 0, |
57
|
|
|
), |
58
|
|
|
'regions' => array( |
59
|
|
|
'puts' => array(), |
60
|
|
|
'hits' => array(), |
61
|
|
|
'misses' => array(), |
62
|
|
|
), |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
foreach ($this->registry->getManagers() as $name => $em) { |
66
|
|
|
$entities[$name] = array(); |
67
|
|
|
/** @var $factory \Doctrine\ORM\Mapping\ClassMetadataFactory */ |
68
|
|
|
$factory = $em->getMetadataFactory(); |
69
|
|
|
$validator = new SchemaValidator($em); |
|
|
|
|
70
|
|
|
|
71
|
|
|
/** @var $class \Doctrine\ORM\Mapping\ClassMetadataInfo */ |
72
|
|
|
foreach ($factory->getLoadedMetadata() as $class) { |
73
|
|
|
if (!isset($entities[$name][$class->getName()])) { |
74
|
|
|
$classErrors = $validator->validateClass($class); |
|
|
|
|
75
|
|
|
$entities[$name][$class->getName()] = $class->getName(); |
76
|
|
|
|
77
|
|
|
if (!empty($classErrors)) { |
78
|
|
|
$errors[$name][$class->getName()] = $classErrors; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (version_compare(Version::VERSION, '2.5.0-DEV') < 0) { |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @var $emConfig \Doctrine\ORM\Configuration */ |
88
|
|
|
$emConfig = $em->getConfiguration(); |
|
|
|
|
89
|
|
|
$slcEnabled = $emConfig->isSecondLevelCacheEnabled(); |
90
|
|
|
|
91
|
|
|
if (!$slcEnabled) { |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$caches['enabled'] = true; |
96
|
|
|
|
97
|
|
|
/** @var $cacheConfiguration \Doctrine\ORM\Cache\CacheConfiguration */ |
98
|
|
|
/** @var $cacheLoggerChain \Doctrine\ORM\Cache\Logging\CacheLoggerChain */ |
99
|
|
|
$cacheConfiguration = $emConfig->getSecondLevelCacheConfiguration(); |
100
|
|
|
$cacheLoggerChain = $cacheConfiguration->getCacheLogger(); |
101
|
|
|
|
102
|
|
|
if (!$cacheLoggerChain || !$cacheLoggerChain->getLogger('statistics')) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** @var $cacheLoggerStats \Doctrine\ORM\Cache\Logging\StatisticsCacheLogger */ |
107
|
|
|
$cacheLoggerStats = $cacheLoggerChain->getLogger('statistics'); |
108
|
|
|
$caches['log_enabled'] = true; |
109
|
|
|
|
110
|
|
|
$caches['counts']['puts'] += $cacheLoggerStats->getPutCount(); |
111
|
|
|
$caches['counts']['hits'] += $cacheLoggerStats->getHitCount(); |
112
|
|
|
$caches['counts']['misses'] += $cacheLoggerStats->getMissCount(); |
113
|
|
|
|
114
|
|
View Code Duplication |
foreach ($cacheLoggerStats->getRegionsPut() as $key => $value) { |
|
|
|
|
115
|
|
|
if (!isset($caches['regions']['puts'][$key])) { |
116
|
|
|
$caches['regions']['puts'][$key] = 0; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$caches['regions']['puts'][$key] += $value; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
foreach ($cacheLoggerStats->getRegionsHit() as $key => $value) { |
|
|
|
|
123
|
|
|
if (!isset($caches['regions']['hits'][$key])) { |
124
|
|
|
$caches['regions']['hits'][$key] = 0; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$caches['regions']['hits'][$key] += $value; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
foreach ($cacheLoggerStats->getRegionsMiss() as $key => $value) { |
|
|
|
|
131
|
|
|
if (!isset($caches['regions']['misses'][$key])) { |
132
|
|
|
$caches['regions']['misses'][$key] = 0; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$caches['regions']['misses'][$key] += $value; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->data['entities'] = $entities; |
140
|
|
|
$this->data['errors'] = $errors; |
141
|
|
|
$this->data['caches'] = $caches; |
142
|
|
|
$this->data['groupedQueries'] = $this->getGroupedQueries(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getEntities() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
return $this->data['entities']; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getMappingErrors() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
return $this->data['errors']; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function getCacheHitsCount() |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
return $this->data['caches']['counts']['hits']; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getCachePutsCount() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
return $this->data['caches']['counts']['puts']; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getCacheMissesCount() |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
return $this->data['caches']['counts']['misses']; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getCacheEnabled() |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
return $this->data['caches']['enabled']; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getCacheRegions() |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
return $this->data['caches']['regions']; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getCacheCounts() |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
return $this->data['caches']['counts']; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function getInvalidEntityCount() |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
if (null === $this->invalidEntityCount) { |
188
|
|
|
$this->invalidEntityCount = array_sum(array_map('count', $this->data['errors'])); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $this->invalidEntityCount; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function getGroupedQueries() |
195
|
|
|
{ |
196
|
|
|
$groupedQueries = array(); |
197
|
|
|
foreach ($this->data['queries'] as $connection => $queries) { |
198
|
|
|
$connectionGroupedQueries = array(); |
199
|
|
|
foreach ($queries as $i => $query) { |
200
|
|
|
$key = $query['sql']; |
201
|
|
|
if (!isset($connectionGroupedQueries[$key])) { |
202
|
|
|
$connectionGroupedQueries[$key] = $query; |
203
|
|
|
$connectionGroupedQueries[$key]['executionMS'] = 0; |
204
|
|
|
$connectionGroupedQueries[$key]['count'] = 0; |
205
|
|
|
$connectionGroupedQueries[$key]['index'] = $i; // "Explain query" relies on query index in 'queries'. |
206
|
|
|
} |
207
|
|
|
$connectionGroupedQueries[$key]['executionMS'] += $query['executionMS']; |
208
|
|
|
$connectionGroupedQueries[$key]['count']++; |
209
|
|
|
} |
210
|
|
|
usort($connectionGroupedQueries, function ($a, $b) { |
211
|
|
|
if ($a['executionMS'] === $b['executionMS']) { |
212
|
|
|
return 0; |
213
|
|
|
} |
214
|
|
|
return ($a['executionMS'] < $b['executionMS']) ? 1 : -1; |
215
|
|
|
}); |
216
|
|
|
$groupedQueries[$connection] = $connectionGroupedQueries; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $groupedQueries; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|