summaryrefslogtreecommitdiff
path: root/vendor/symfony/cache/DataCollector
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/cache/DataCollector')
-rw-r--r--vendor/symfony/cache/DataCollector/CacheDataCollector.php184
1 files changed, 184 insertions, 0 deletions
diff --git a/vendor/symfony/cache/DataCollector/CacheDataCollector.php b/vendor/symfony/cache/DataCollector/CacheDataCollector.php
new file mode 100644
index 0000000..b9bcdaf
--- /dev/null
+++ b/vendor/symfony/cache/DataCollector/CacheDataCollector.php
@@ -0,0 +1,184 @@
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Cache\DataCollector;
13
14use Symfony\Component\Cache\Adapter\TraceableAdapter;
15use Symfony\Component\Cache\Adapter\TraceableAdapterEvent;
16use Symfony\Component\HttpFoundation\Request;
17use Symfony\Component\HttpFoundation\Response;
18use Symfony\Component\HttpKernel\DataCollector\DataCollector;
19use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
20
21/**
22 * @author Aaron Scherer <aequasi@gmail.com>
23 * @author Tobias Nyholm <tobias.nyholm@gmail.com>
24 *
25 * @final
26 */
27class CacheDataCollector extends DataCollector implements LateDataCollectorInterface
28{
29 /**
30 * @var TraceableAdapter[]
31 */
32 private array $instances = [];
33
34 public function addInstance(string $name, TraceableAdapter $instance): void
35 {
36 $this->instances[$name] = $instance;
37 }
38
39 public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
40 {
41 $empty = ['calls' => [], 'adapters' => [], 'config' => [], 'options' => [], 'statistics' => []];
42 $this->data = ['instances' => $empty, 'total' => $empty];
43 foreach ($this->instances as $name => $instance) {
44 $this->data['instances']['calls'][$name] = $instance->getCalls();
45 $this->data['instances']['adapters'][$name] = get_debug_type($instance->getPool());
46 }
47
48 $this->data['instances']['statistics'] = $this->calculateStatistics();
49 $this->data['total']['statistics'] = $this->calculateTotalStatistics();
50 }
51
52 public function reset(): void
53 {
54 $this->data = [];
55 foreach ($this->instances as $instance) {
56 $instance->clearCalls();
57 }
58 }
59
60 public function lateCollect(): void
61 {
62 $this->data['instances']['calls'] = $this->cloneVar($this->data['instances']['calls']);
63 }
64
65 public function getName(): string
66 {
67 return 'cache';
68 }
69
70 /**
71 * Method returns amount of logged Cache reads: "get" calls.
72 */
73 public function getStatistics(): array
74 {
75 return $this->data['instances']['statistics'];
76 }
77
78 /**
79 * Method returns the statistic totals.
80 */
81 public function getTotals(): array
82 {
83 return $this->data['total']['statistics'];
84 }
85
86 /**
87 * Method returns all logged Cache call objects.
88 */
89 public function getCalls(): mixed
90 {
91 return $this->data['instances']['calls'];
92 }
93
94 /**
95 * Method returns all logged Cache adapter classes.
96 */
97 public function getAdapters(): array
98 {
99 return $this->data['instances']['adapters'];
100 }
101
102 private function calculateStatistics(): array
103 {
104 $statistics = [];
105 foreach ($this->data['instances']['calls'] as $name => $calls) {
106 $statistics[$name] = [
107 'calls' => 0,
108 'time' => 0,
109 'reads' => 0,
110 'writes' => 0,
111 'deletes' => 0,
112 'hits' => 0,
113 'misses' => 0,
114 ];
115 /** @var TraceableAdapterEvent $call */
116 foreach ($calls as $call) {
117 ++$statistics[$name]['calls'];
118 $statistics[$name]['time'] += ($call->end ?? microtime(true)) - $call->start;
119 if ('get' === $call->name) {
120 ++$statistics[$name]['reads'];
121 if ($call->hits) {
122 ++$statistics[$name]['hits'];
123 } else {
124 ++$statistics[$name]['misses'];
125 ++$statistics[$name]['writes'];
126 }
127 } elseif ('getItem' === $call->name) {
128 ++$statistics[$name]['reads'];
129 if ($call->hits) {
130 ++$statistics[$name]['hits'];
131 } else {
132 ++$statistics[$name]['misses'];
133 }
134 } elseif ('getItems' === $call->name) {
135 $statistics[$name]['reads'] += $call->hits + $call->misses;
136 $statistics[$name]['hits'] += $call->hits;
137 $statistics[$name]['misses'] += $call->misses;
138 } elseif ('hasItem' === $call->name) {
139 ++$statistics[$name]['reads'];
140 foreach ($call->result ?? [] as $result) {
141 ++$statistics[$name][$result ? 'hits' : 'misses'];
142 }
143 } elseif ('save' === $call->name) {
144 ++$statistics[$name]['writes'];
145 } elseif ('deleteItem' === $call->name) {
146 ++$statistics[$name]['deletes'];
147 }
148 }
149 if ($statistics[$name]['reads']) {
150 $statistics[$name]['hit_read_ratio'] = round(100 * $statistics[$name]['hits'] / $statistics[$name]['reads'], 2);
151 } else {
152 $statistics[$name]['hit_read_ratio'] = null;
153 }
154 }
155
156 return $statistics;
157 }
158
159 private function calculateTotalStatistics(): array
160 {
161 $statistics = $this->getStatistics();
162 $totals = [
163 'calls' => 0,
164 'time' => 0,
165 'reads' => 0,
166 'writes' => 0,
167 'deletes' => 0,
168 'hits' => 0,
169 'misses' => 0,
170 ];
171 foreach ($statistics as $name => $values) {
172 foreach ($totals as $key => $value) {
173 $totals[$key] += $statistics[$name][$key];
174 }
175 }
176 if ($totals['reads']) {
177 $totals['hit_read_ratio'] = round(100 * $totals['hits'] / $totals['reads'], 2);
178 } else {
179 $totals['hit_read_ratio'] = null;
180 }
181
182 return $totals;
183 }
184}