1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\DataCollector; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
use Doctrine\ORM\Cache\Logging\CacheLoggerChain; |
7
|
|
|
use Doctrine\ORM\Cache\Logging\StatisticsCacheLogger; |
8
|
|
|
use Doctrine\ORM\Configuration; |
9
|
|
|
use Doctrine\ORM\EntityManager; |
10
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
11
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
12
|
|
|
use Doctrine\ORM\Tools\SchemaValidator; |
13
|
|
|
use Exception; |
14
|
|
|
use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector as BaseCollector; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DoctrineDataCollector. |
20
|
|
|
*/ |
21
|
|
|
class DoctrineDataCollector extends BaseCollector |
22
|
|
|
{ |
23
|
|
|
/** @var ManagerRegistry */ |
24
|
|
|
private $registry; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** @var int|null */ |
27
|
|
|
private $invalidEntityCount; |
28
|
|
|
|
29
|
|
|
/** @var string[] */ |
30
|
|
|
private $groupedQueries; |
31
|
|
|
|
32
|
|
|
public function __construct(ManagerRegistry $registry) |
33
|
|
|
{ |
34
|
|
|
$this->registry = $registry; |
35
|
|
|
|
36
|
|
|
parent::__construct($registry); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function collect(Request $request, Response $response, Exception $exception = null) |
43
|
|
|
{ |
44
|
|
|
parent::collect($request, $response, $exception); |
45
|
|
|
|
46
|
|
|
$errors = []; |
47
|
|
|
$entities = []; |
48
|
|
|
$caches = [ |
49
|
|
|
'enabled' => false, |
50
|
|
|
'log_enabled' => false, |
51
|
|
|
'counts' => [ |
52
|
|
|
'puts' => 0, |
53
|
|
|
'hits' => 0, |
54
|
|
|
'misses' => 0, |
55
|
|
|
], |
56
|
|
|
'regions' => [ |
57
|
|
|
'puts' => [], |
58
|
|
|
'hits' => [], |
59
|
|
|
'misses' => [], |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
/** @var EntityManager $em */ |
64
|
|
|
foreach ($this->registry->getManagers() as $name => $em) { |
65
|
|
|
$entities[$name] = []; |
66
|
|
|
/** @var ClassMetadataFactory $factory */ |
67
|
|
|
$factory = $em->getMetadataFactory(); |
68
|
|
|
$validator = new SchemaValidator($em); |
|
|
|
|
69
|
|
|
|
70
|
|
|
/** @var ClassMetadataInfo $class */ |
71
|
|
|
foreach ($factory->getLoadedMetadata() as $class) { |
72
|
|
|
if (isset($entities[$name][$class->getName()])) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$classErrors = $validator->validateClass($class); |
|
|
|
|
77
|
|
|
$entities[$name][$class->getName()] = $class->getName(); |
78
|
|
|
|
79
|
|
|
if (empty($classErrors)) { |
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$errors[$name][$class->getName()] = $classErrors; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** @var Configuration $emConfig */ |
87
|
|
|
$emConfig = $em->getConfiguration(); |
|
|
|
|
88
|
|
|
$slcEnabled = $emConfig->isSecondLevelCacheEnabled(); |
89
|
|
|
|
90
|
|
|
if (! $slcEnabled) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$caches['enabled'] = true; |
95
|
|
|
|
96
|
|
|
/** @var $cacheConfiguration \Doctrine\ORM\Cache\CacheConfiguration */ |
97
|
|
|
/** @var CacheLoggerChain $cacheLoggerChain */ |
98
|
|
|
$cacheConfiguration = $emConfig->getSecondLevelCacheConfiguration(); |
99
|
|
|
$cacheLoggerChain = $cacheConfiguration->getCacheLogger(); |
100
|
|
|
|
101
|
|
|
if (! $cacheLoggerChain || ! $cacheLoggerChain->getLogger('statistics')) { |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** @var StatisticsCacheLogger $cacheLoggerStats */ |
106
|
|
|
$cacheLoggerStats = $cacheLoggerChain->getLogger('statistics'); |
107
|
|
|
$caches['log_enabled'] = true; |
108
|
|
|
|
109
|
|
|
$caches['counts']['puts'] += $cacheLoggerStats->getPutCount(); |
110
|
|
|
$caches['counts']['hits'] += $cacheLoggerStats->getHitCount(); |
111
|
|
|
$caches['counts']['misses'] += $cacheLoggerStats->getMissCount(); |
112
|
|
|
|
113
|
|
View Code Duplication |
foreach ($cacheLoggerStats->getRegionsPut() as $key => $value) { |
|
|
|
|
114
|
|
|
if (! isset($caches['regions']['puts'][$key])) { |
115
|
|
|
$caches['regions']['puts'][$key] = 0; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$caches['regions']['puts'][$key] += $value; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
View Code Duplication |
foreach ($cacheLoggerStats->getRegionsHit() as $key => $value) { |
|
|
|
|
122
|
|
|
if (! isset($caches['regions']['hits'][$key])) { |
123
|
|
|
$caches['regions']['hits'][$key] = 0; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$caches['regions']['hits'][$key] += $value; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
View Code Duplication |
foreach ($cacheLoggerStats->getRegionsMiss() as $key => $value) { |
|
|
|
|
130
|
|
|
if (! isset($caches['regions']['misses'][$key])) { |
131
|
|
|
$caches['regions']['misses'][$key] = 0; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$caches['regions']['misses'][$key] += $value; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// Might be good idea to replicate this block in doctrine bridge so we can drop this from here after some time. |
139
|
|
|
// This code is compatible with such change, because cloneVar is supposed to check if input is already cloned. |
140
|
|
|
foreach ($this->data['queries'] as &$queries) { |
141
|
|
|
foreach ($queries as &$query) { |
142
|
|
|
$query['params'] = $this->cloneVar($query['params']); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->data['entities'] = $entities; |
147
|
|
|
$this->data['errors'] = $errors; |
148
|
|
|
$this->data['caches'] = $caches; |
149
|
|
|
$this->groupedQueries = null; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getEntities() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
return $this->data['entities']; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getMappingErrors() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
return $this->data['errors']; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getCacheHitsCount() |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
return $this->data['caches']['counts']['hits']; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getCachePutsCount() |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
return $this->data['caches']['counts']['puts']; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getCacheMissesCount() |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
return $this->data['caches']['counts']['misses']; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function getCacheEnabled() |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
return $this->data['caches']['enabled']; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function getCacheRegions() |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
return $this->data['caches']['regions']; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function getCacheCounts() |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
return $this->data['caches']['counts']; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function getInvalidEntityCount() |
193
|
|
|
{ |
194
|
|
|
if ($this->invalidEntityCount === null) { |
195
|
|
|
$this->invalidEntityCount = array_sum(array_map('count', $this->data['errors'])); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this->invalidEntityCount; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function getGroupedQueries() |
202
|
|
|
{ |
203
|
|
|
if ($this->groupedQueries !== null) { |
204
|
|
|
return $this->groupedQueries; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$this->groupedQueries = []; |
208
|
|
|
$totalExecutionMS = 0; |
209
|
|
|
foreach ($this->data['queries'] as $connection => $queries) { |
210
|
|
|
$connectionGroupedQueries = []; |
211
|
|
|
foreach ($queries as $i => $query) { |
212
|
|
|
$key = $query['sql']; |
213
|
|
|
if (! isset($connectionGroupedQueries[$key])) { |
214
|
|
|
$connectionGroupedQueries[$key] = $query; |
215
|
|
|
$connectionGroupedQueries[$key]['executionMS'] = 0; |
216
|
|
|
$connectionGroupedQueries[$key]['count'] = 0; |
217
|
|
|
$connectionGroupedQueries[$key]['index'] = $i; // "Explain query" relies on query index in 'queries'. |
218
|
|
|
} |
219
|
|
|
$connectionGroupedQueries[$key]['executionMS'] += $query['executionMS']; |
220
|
|
|
$connectionGroupedQueries[$key]['count']++; |
221
|
|
|
$totalExecutionMS += $query['executionMS']; |
222
|
|
|
} |
223
|
|
|
usort($connectionGroupedQueries, static function ($a, $b) { |
224
|
|
|
if ($a['executionMS'] === $b['executionMS']) { |
225
|
|
|
return 0; |
226
|
|
|
} |
227
|
|
|
return $a['executionMS'] < $b['executionMS'] ? 1 : -1; |
228
|
|
|
}); |
229
|
|
|
$this->groupedQueries[$connection] = $connectionGroupedQueries; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
foreach ($this->groupedQueries as $connection => $queries) { |
233
|
|
|
foreach ($queries as $i => $query) { |
234
|
|
|
$this->groupedQueries[$connection][$i]['executionPercent'] = |
235
|
|
|
$this->executionTimePercentage($query['executionMS'], $totalExecutionMS); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $this->groupedQueries; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
private function executionTimePercentage($executionTimeMS, $totalExecutionTimeMS) |
243
|
|
|
{ |
244
|
|
|
if ($totalExecutionTimeMS === 0.0 || $totalExecutionTimeMS === 0) { |
245
|
|
|
return 0; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $executionTimeMS / $totalExecutionTimeMS * 100; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function getGroupedQueryCount() |
252
|
|
|
{ |
253
|
|
|
$count = 0; |
254
|
|
|
foreach ($this->getGroupedQueries() as $connectionGroupedQueries) { |
255
|
|
|
$count += count($connectionGroupedQueries); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $count; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|