* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Cache\DataCollector; use Symfony\Component\Cache\Adapter\TraceableAdapter; use Symfony\Component\Cache\Adapter\TraceableAdapterEvent; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\DataCollector\DataCollector; use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface; /** * @author Aaron Scherer * @author Tobias Nyholm */ class CacheDataCollector extends DataCollector implements LateDataCollectorInterface { /** * @var TraceableAdapter[] */ private $instances = array(); /** * @param string $name * @param TraceableAdapter $instance */ public function addInstance($name, TraceableAdapter $instance) { $this->instances[$name] = $instance; } /** * {@inheritdoc} */ public function collect(Request $request, Response $response, \Exception $exception = null) { $empty = array('calls' => array(), 'config' => array(), 'options' => array(), 'statistics' => array()); $this->data = array('instances' => $empty, 'total' => $empty); foreach ($this->instances as $name => $instance) { $this->data['instances']['calls'][$name] = $instance->getCalls(); } $this->data['instances']['statistics'] = $this->calculateStatistics(); $this->data['total']['statistics'] = $this->calculateTotalStatistics(); } public function reset() { $this->data = array(); foreach ($this->instances as $instance) { $instance->clearCalls(); } } public function lateCollect() { $this->data = $this->cloneVar($this->data); } /** * {@inheritdoc} */ public function getName() { return 'cache'; } /** * Method returns amount of logged Cache reads: "get" calls. * * @return array */ public function getStatistics() { return $this->data['instances']['statistics']; } /** * Method returns the statistic totals. * * @return array */ public function getTotals() { return $this->data['total']['statistics']; } /** * Method returns all logged Cache call objects. * * @return mixed */ public function getCalls() { return $this->data['instances']['calls']; } /** * @return array */ private function calculateStatistics() { $statistics = array(); foreach ($this->data['instances']['calls'] as $name => $calls) { $statistics[$name] = array( 'calls' => 0, 'time' => 0, 'reads' => 0, 'writes' => 0, 'deletes' => 0, 'hits' => 0, 'misses' => 0, ); /** @var TraceableAdapterEvent $call */ foreach ($calls as $call) { ++$statistics[$name]['calls']; $statistics[$name]['time'] += $call->end - $call->start; if ('getItem' === $call->name) { ++$statistics[$name]['reads']; if ($call->hits) { ++$statistics[$name]['hits']; } else { ++$statistics[$name]['misses']; } } elseif ('getItems' === $call->name) { $statistics[$name]['reads'] += $call->hits + $call->misses; $statistics[$name]['hits'] += $call->hits; $statistics[$name]['misses'] += $call->misses; } elseif ('hasItem' === $call->name) { ++$statistics[$name]['reads']; if (false === $call->result) { ++$statistics[$name]['misses']; } else { ++$statistics[$name]['hits']; } } elseif ('save' === $call->name) { ++$statistics[$name]['writes']; } elseif ('deleteItem' === $call->name) { ++$statistics[$name]['deletes']; } } if ($statistics[$name]['reads']) { $statistics[$name]['hit_read_ratio'] = round(100 * $statistics[$name]['hits'] / $statistics[$name]['reads'], 2); } else { $statistics[$name]['hit_read_ratio'] = null; } } return $statistics; } /** * @return array */ private function calculateTotalStatistics() { $statistics = $this->getStatistics(); $totals = array( 'calls' => 0, 'time' => 0, 'reads' => 0, 'writes' => 0, 'deletes' => 0, 'hits' => 0, 'misses' => 0, ); foreach ($statistics as $name => $values) { foreach ($totals as $key => $value) { $totals[$key] += $statistics[$name][$key]; } } if ($totals['reads']) { $totals['hit_read_ratio'] = round(100 * $totals['hits'] / $totals['reads'], 2); } else { $totals['hit_read_ratio'] = null; } return $totals; } } __halt_compiler();----SIGNATURE:----MQAKPWYnLFzKHbHLIy5iAvAkD/LT00yq5sHw8un1JQoDOmihCggY6yt0YuVJdqosZuAE3T+xJQxF73E8AyKa8b27WBDhDjJTTiQ42FcEYQeuRVvkXbihROeLs9n/y/pxK0YiP3jV/F6B3FtwIu1+CTHTON16ooiIWV6ctLwITItF4Te7LEmUf/u+6mBwi/D/EUBRqzlWdQiSy6gR9HZ/2aeEL4Mk+rGIF+1LqMIQi4HiEtJGSk9JEyOItXXPhXboCfVbR6i2QZEll5/qZxz8AnddmE7YjfgaSMuyhSDfBsw6h79pDx/C6wQTUQtQxQDvZXKuGsVTWFkW+CmfrecCmMoG52uLh4DvOsr8FztXx16QgGbq5xQDSOK8X4SViANJsaXn2UE24o+R69a+dCyL+etPve6U1ijV+qWg2smi8d3NS/+NxOWdXIWpwZib1TSi10NQY+B7Xd1+TU6rz0IFW3TeReeWimAUFBo8k4fnE4L9CcGwGcp44oRlHLfZupCjk2A1NbT7VHX+CPPRisj79YWoywWeFNQQABuI7081mnbfiQCm5iH2JQIcAsYK48M6coBGxasjkdO8ZHvDUmvk+sSw59aTp9KvveI7xMmWDoN5uISJwsWGSJY4qlXrjW3Sf7QfF/3vCUSs1n+jRYvoAOQPmPEEL8LmWNMXalYwLAQ=----ATTACHMENT:----Mzc2MDQwNTY1ODk1MjM4MiAxNTMxNjc1NDI1NDk4NTIzIDgyMzI2OTIxMTM3MzU3OTI=