CacheDataCollector.php
5.6 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?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\Component\Cache\DataCollector;
use Symfony\Component\Cache\Adapter\TraceableAdapter;
use Symfony\Component\Cache\Adapter\TraceableAdapterEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
/**
* @author Aaron Scherer <aequasi@gmail.com>
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*
* @final
*/
class CacheDataCollector extends DataCollector implements LateDataCollectorInterface
{
/**
* @var TraceableAdapter[]
*/
private $instances = [];
public function addInstance(string $name, TraceableAdapter $instance)
{
$this->instances[$name] = $instance;
}
/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
$empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []];
$this->data = ['instances' => $empty, 'total' => $empty];
foreach ($this->instances as $name => $instance) {
$this->data['instances']['calls'][$name] = $instance->getCalls();
}
$this->data['instances']['statistics'] = $this->calculateStatistics();
$this->data['total']['statistics'] = $this->calculateTotalStatistics();
}
public function reset()
{
$this->data = [];
foreach ($this->instances as $instance) {
$instance->clearCalls();
}
}
public function lateCollect()
{
$this->data['instances']['calls'] = $this->cloneVar($this->data['instances']['calls']);
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'cache';
}
/**
* Method returns amount of logged Cache reads: "get" calls.
*/
public function getStatistics(): array
{
return $this->data['instances']['statistics'];
}
/**
* Method returns the statistic totals.
*/
public function getTotals(): array
{
return $this->data['total']['statistics'];
}
/**
* Method returns all logged Cache call objects.
*
* @return mixed
*/
public function getCalls()
{
return $this->data['instances']['calls'];
}
private function calculateStatistics(): array
{
$statistics = [];
foreach ($this->data['instances']['calls'] as $name => $calls) {
$statistics[$name] = [
'calls' => 0,
'time' => 0,
'reads' => 0,
'writes' => 0,
'deletes' => 0,
'hits' => 0,
'misses' => 0,
];
/** @var TraceableAdapterEvent $call */
foreach ($calls as $call) {
++$statistics[$name]['calls'];
$statistics[$name]['time'] += ($call->end ?? microtime(true)) - $call->start;
if ('get' === $call->name) {
++$statistics[$name]['reads'];
if ($call->hits) {
++$statistics[$name]['hits'];
} else {
++$statistics[$name]['misses'];
++$statistics[$name]['writes'];
}
} elseif ('getItem' === $call->name) {
++$statistics[$name]['reads'];
if ($call->hits) {
++$statistics[$name]['hits'];
} else {
++$statistics[$name]['misses'];
}
} elseif ('getItems' === $call->name) {
$statistics[$name]['reads'] += $call->hits + $call->misses;
$statistics[$name]['hits'] += $call->hits;
$statistics[$name]['misses'] += $call->misses;
} elseif ('hasItem' === $call->name) {
++$statistics[$name]['reads'];
foreach ($call->result ?? [] as $result) {
++$statistics[$name][$result ? 'hits' : 'misses'];
}
} elseif ('save' === $call->name) {
++$statistics[$name]['writes'];
} elseif ('deleteItem' === $call->name) {
++$statistics[$name]['deletes'];
}
}
if ($statistics[$name]['reads']) {
$statistics[$name]['hit_read_ratio'] = round(100 * $statistics[$name]['hits'] / $statistics[$name]['reads'], 2);
} else {
$statistics[$name]['hit_read_ratio'] = null;
}
}
return $statistics;
}
private function calculateTotalStatistics(): array
{
$statistics = $this->getStatistics();
$totals = [
'calls' => 0,
'time' => 0,
'reads' => 0,
'writes' => 0,
'deletes' => 0,
'hits' => 0,
'misses' => 0,
];
foreach ($statistics as $name => $values) {
foreach ($totals as $key => $value) {
$totals[$key] += $statistics[$name][$key];
}
}
if ($totals['reads']) {
$totals['hit_read_ratio'] = round(100 * $totals['hits'] / $totals['reads'], 2);
} else {
$totals['hit_read_ratio'] = null;
}
return $totals;
}
}