CacheDataCollector.php 6.0 KB

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