CacheDataCollector.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /**
  93. * @return array
  94. */
  95. private function calculateStatistics()
  96. {
  97. $statistics = [];
  98. foreach ($this->data['instances']['calls'] as $name => $calls) {
  99. $statistics[$name] = [
  100. 'calls' => 0,
  101. 'time' => 0,
  102. 'reads' => 0,
  103. 'writes' => 0,
  104. 'deletes' => 0,
  105. 'hits' => 0,
  106. 'misses' => 0,
  107. ];
  108. /** @var TraceableAdapterEvent $call */
  109. foreach ($calls as $call) {
  110. ++$statistics[$name]['calls'];
  111. $statistics[$name]['time'] += $call->end - $call->start;
  112. if ('getItem' === $call->name) {
  113. ++$statistics[$name]['reads'];
  114. if ($call->hits) {
  115. ++$statistics[$name]['hits'];
  116. } else {
  117. ++$statistics[$name]['misses'];
  118. }
  119. } elseif ('getItems' === $call->name) {
  120. $statistics[$name]['reads'] += $call->hits + $call->misses;
  121. $statistics[$name]['hits'] += $call->hits;
  122. $statistics[$name]['misses'] += $call->misses;
  123. } elseif ('hasItem' === $call->name) {
  124. ++$statistics[$name]['reads'];
  125. if (false === $call->result) {
  126. ++$statistics[$name]['misses'];
  127. } else {
  128. ++$statistics[$name]['hits'];
  129. }
  130. } elseif ('save' === $call->name) {
  131. ++$statistics[$name]['writes'];
  132. } elseif ('deleteItem' === $call->name) {
  133. ++$statistics[$name]['deletes'];
  134. }
  135. }
  136. if ($statistics[$name]['reads']) {
  137. $statistics[$name]['hit_read_ratio'] = round(100 * $statistics[$name]['hits'] / $statistics[$name]['reads'], 2);
  138. } else {
  139. $statistics[$name]['hit_read_ratio'] = null;
  140. }
  141. }
  142. return $statistics;
  143. }
  144. /**
  145. * @return array
  146. */
  147. private function calculateTotalStatistics()
  148. {
  149. $statistics = $this->getStatistics();
  150. $totals = [
  151. 'calls' => 0,
  152. 'time' => 0,
  153. 'reads' => 0,
  154. 'writes' => 0,
  155. 'deletes' => 0,
  156. 'hits' => 0,
  157. 'misses' => 0,
  158. ];
  159. foreach ($statistics as $name => $values) {
  160. foreach ($totals as $key => $value) {
  161. $totals[$key] += $statistics[$name][$key];
  162. }
  163. }
  164. if ($totals['reads']) {
  165. $totals['hit_read_ratio'] = round(100 * $totals['hits'] / $totals['reads'], 2);
  166. } else {
  167. $totals['hit_read_ratio'] = null;
  168. }
  169. return $totals;
  170. }
  171. }