CacheDataCollector.php 5.5 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. class CacheDataCollector extends DataCollector implements LateDataCollectorInterface
  22. {
  23. /**
  24. * @var TraceableAdapter[]
  25. */
  26. private $instances = array();
  27. /**
  28. * @param string $name
  29. * @param TraceableAdapter $instance
  30. */
  31. public function addInstance($name, TraceableAdapter $instance)
  32. {
  33. $this->instances[$name] = $instance;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function collect(Request $request, Response $response, \Exception $exception = null)
  39. {
  40. $empty = array('calls' => array(), 'config' => array(), 'options' => array(), 'statistics' => array());
  41. $this->data = array('instances' => $empty, 'total' => $empty);
  42. foreach ($this->instances as $name => $instance) {
  43. $this->data['instances']['calls'][$name] = $instance->getCalls();
  44. }
  45. $this->data['instances']['statistics'] = $this->calculateStatistics();
  46. $this->data['total']['statistics'] = $this->calculateTotalStatistics();
  47. }
  48. public function reset()
  49. {
  50. $this->data = array();
  51. foreach ($this->instances as $instance) {
  52. $instance->clearCalls();
  53. }
  54. }
  55. public function lateCollect()
  56. {
  57. $this->data = $this->cloneVar($this->data);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getName()
  63. {
  64. return 'cache';
  65. }
  66. /**
  67. * Method returns amount of logged Cache reads: "get" calls.
  68. *
  69. * @return array
  70. */
  71. public function getStatistics()
  72. {
  73. return $this->data['instances']['statistics'];
  74. }
  75. /**
  76. * Method returns the statistic totals.
  77. *
  78. * @return array
  79. */
  80. public function getTotals()
  81. {
  82. return $this->data['total']['statistics'];
  83. }
  84. /**
  85. * Method returns all logged Cache call objects.
  86. *
  87. * @return mixed
  88. */
  89. public function getCalls()
  90. {
  91. return $this->data['instances']['calls'];
  92. }
  93. /**
  94. * @return array
  95. */
  96. private function calculateStatistics()
  97. {
  98. $statistics = array();
  99. foreach ($this->data['instances']['calls'] as $name => $calls) {
  100. $statistics[$name] = array(
  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 ('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. if (false === $call->result) {
  127. ++$statistics[$name]['misses'];
  128. } else {
  129. ++$statistics[$name]['hits'];
  130. }
  131. } elseif ('save' === $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. /**
  146. * @return array
  147. */
  148. private function calculateTotalStatistics()
  149. {
  150. $statistics = $this->getStatistics();
  151. $totals = array(
  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. }