CacheDataCollector.php 5.7 KB

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