CacheDataCollector.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 since Symfony 4.4
  22. */
  23. class CacheDataCollector extends DataCollector implements LateDataCollectorInterface
  24. {
  25. /**
  26. * @var TraceableAdapter[]
  27. */
  28. private $instances = [];
  29. /**
  30. * @param string $name
  31. */
  32. public function addInstance($name, TraceableAdapter $instance)
  33. {
  34. $this->instances[$name] = $instance;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. *
  39. * @param \Throwable|null $exception
  40. */
  41. public function collect(Request $request, Response $response/*, \Throwable $exception = null*/)
  42. {
  43. $empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []];
  44. $this->data = ['instances' => $empty, 'total' => $empty];
  45. foreach ($this->instances as $name => $instance) {
  46. $this->data['instances']['calls'][$name] = $instance->getCalls();
  47. }
  48. $this->data['instances']['statistics'] = $this->calculateStatistics();
  49. $this->data['total']['statistics'] = $this->calculateTotalStatistics();
  50. }
  51. public function reset()
  52. {
  53. $this->data = [];
  54. foreach ($this->instances as $instance) {
  55. $instance->clearCalls();
  56. }
  57. }
  58. public function lateCollect()
  59. {
  60. $this->data['instances']['calls'] = $this->cloneVar($this->data['instances']['calls']);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getName()
  66. {
  67. return 'cache';
  68. }
  69. /**
  70. * Method returns amount of logged Cache reads: "get" calls.
  71. *
  72. * @return array
  73. */
  74. public function getStatistics()
  75. {
  76. return $this->data['instances']['statistics'];
  77. }
  78. /**
  79. * Method returns the statistic totals.
  80. *
  81. * @return array
  82. */
  83. public function getTotals()
  84. {
  85. return $this->data['total']['statistics'];
  86. }
  87. /**
  88. * Method returns all logged Cache call objects.
  89. *
  90. * @return mixed
  91. */
  92. public function getCalls()
  93. {
  94. return $this->data['instances']['calls'];
  95. }
  96. private function calculateStatistics(): array
  97. {
  98. $statistics = [];
  99. foreach ($this->data['instances']['calls'] as $name => $calls) {
  100. $statistics[$name] = [
  101. 'calls' => 0,
  102. 'time' => 0,
  103. 'reads' => 0,
  104. 'writes' => 0,
  105. 'deletes' => 0,
  106. 'hits' => 0,
  107. 'misses' => 0,
  108. ];
  109. /** @var TraceableAdapterEvent $call */
  110. foreach ($calls as $call) {
  111. ++$statistics[$name]['calls'];
  112. $statistics[$name]['time'] += $call->end - $call->start;
  113. if ('get' === $call->name) {
  114. ++$statistics[$name]['reads'];
  115. if ($call->hits) {
  116. ++$statistics[$name]['hits'];
  117. } else {
  118. ++$statistics[$name]['misses'];
  119. ++$statistics[$name]['writes'];
  120. }
  121. } elseif ('getItem' === $call->name) {
  122. ++$statistics[$name]['reads'];
  123. if ($call->hits) {
  124. ++$statistics[$name]['hits'];
  125. } else {
  126. ++$statistics[$name]['misses'];
  127. }
  128. } elseif ('getItems' === $call->name) {
  129. $statistics[$name]['reads'] += $call->hits + $call->misses;
  130. $statistics[$name]['hits'] += $call->hits;
  131. $statistics[$name]['misses'] += $call->misses;
  132. } elseif ('hasItem' === $call->name) {
  133. ++$statistics[$name]['reads'];
  134. if (false === $call->result) {
  135. ++$statistics[$name]['misses'];
  136. } else {
  137. ++$statistics[$name]['hits'];
  138. }
  139. } elseif ('save' === $call->name) {
  140. ++$statistics[$name]['writes'];
  141. } elseif ('deleteItem' === $call->name) {
  142. ++$statistics[$name]['deletes'];
  143. }
  144. }
  145. if ($statistics[$name]['reads']) {
  146. $statistics[$name]['hit_read_ratio'] = round(100 * $statistics[$name]['hits'] / $statistics[$name]['reads'], 2);
  147. } else {
  148. $statistics[$name]['hit_read_ratio'] = null;
  149. }
  150. }
  151. return $statistics;
  152. }
  153. private function calculateTotalStatistics(): array
  154. {
  155. $statistics = $this->getStatistics();
  156. $totals = [
  157. 'calls' => 0,
  158. 'time' => 0,
  159. 'reads' => 0,
  160. 'writes' => 0,
  161. 'deletes' => 0,
  162. 'hits' => 0,
  163. 'misses' => 0,
  164. ];
  165. foreach ($statistics as $name => $values) {
  166. foreach ($totals as $key => $value) {
  167. $totals[$key] += $statistics[$name][$key];
  168. }
  169. }
  170. if ($totals['reads']) {
  171. $totals['hit_read_ratio'] = round(100 * $totals['hits'] / $totals['reads'], 2);
  172. } else {
  173. $totals['hit_read_ratio'] = null;
  174. }
  175. return $totals;
  176. }
  177. }