-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathPersistenceCacheCollector.php
More file actions
150 lines (131 loc) · 4.06 KB
/
Copy pathPersistenceCacheCollector.php
File metadata and controls
150 lines (131 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\Bundle\Debug\Collector;
use Ibexa\Core\Persistence\Cache\PersistenceLogger;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
/**
* Data collector listing SPI cache calls.
*/
class PersistenceCacheCollector extends DataCollector
{
/** @var \Ibexa\Core\Persistence\Cache\PersistenceLogger */
private $logger;
public function __construct(PersistenceLogger $logger)
{
$this->logger = $logger;
}
public function collect(Request $request, Response $response, ?\Throwable $exception = null)
{
$this->data = [
'stats' => $this->logger->getStats(),
'calls_logging_enabled' => $this->logger->isCallsLoggingEnabled(),
'calls' => $this->logger->getCalls(),
'handlers' => $this->logger->getLoadedUnCachedHandlers(),
];
}
public function getName()
{
return 'ezpublish.debug.persistence';
}
/**
* Returns call count.
*
* @deprecated since 7.5, use getStats().
*
* @return int
*/
public function getCount()
{
return $this->data['stats']['call'] + $this->data['stats']['miss'];
}
/**
* Returns stats on Persistance cache usage.
*
* @since 7.5
*
* @return int [<string>]
*/
public function getStats()
{
return $this->data['stats'];
}
/**
* Returns flag to indicate if logging of calls is enabled or not.
*
* Typically not enabled in prod.
*
* @return bool
*/
public function getCallsLoggingEnabled()
{
return $this->data['calls_logging_enabled'];
}
/**
* Returns all calls.
*
* @return array
*/
public function getCalls()
{
if (empty($this->data['calls'])) {
return [];
}
$calls = $count = [];
foreach ($this->data['calls'] as $hash => $call) {
list($class, $method) = \explode('::', $call['method']);
$namespace = \explode('\\', $class);
$class = \array_pop($namespace);
$calls[$hash] = [
'namespace' => $namespace,
'class' => $class,
'method' => $method,
'arguments' => $call['arguments'],
'stats' => $call['stats'],
];
// Get traces, and order them to have the most called first
$calls[$hash]['traces'] = $call['traces'];
$traceCount = [];
foreach ($call['traces'] as $traceHash => $traceData) {
$traceCount[$traceHash] = $traceData['count'];
}
\array_multisort($traceCount, SORT_DESC, SORT_NUMERIC, $calls[$hash]['traces']);
// For call sorting count all calls, but weight in-memory lookups lower
$count[$hash] = $call['stats']['uncached'] + $call['stats']['miss'] + $call['stats']['hit'] + ($call['stats']['memory'] * 0.001);
}
// Order calls
\array_multisort($count, SORT_DESC, SORT_NUMERIC, $calls);
return $calls;
}
/**
* Returns un cached handlers being loaded.
*
* @return array
*/
public function getHandlers()
{
$handlers = [];
foreach ($this->data['handlers'] as $handler => $count) {
list($class, $method) = explode('::', $handler);
unset($class);
$handlers[$method] = $method . '(' . $count . ')';
}
return $handlers;
}
/**
* Returns uncached handlers being loaded.
*/
public function getHandlersCount(): int
{
return (int)array_sum($this->data['handlers']);
}
public function reset(): void
{
$this->data = [];
}
}
class_alias(PersistenceCacheCollector::class, 'eZ\Bundle\EzPublishDebugBundle\Collector\PersistenceCacheCollector');