CacheDataCollector.php 5.7 KB

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