-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Added cache data collector and profiler page #21065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
23c0039
8f73434
32376dd
d63adcd
321e526
26ea3e9
9e12fac
d09bcf3
9f1aa40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\Cache\Adapter\TraceableAdapter; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Inject a data collector to all the cache services to be able to get detailed statistics. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
class CacheCollectorPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('data_collector.cache')) { | ||
return; | ||
} | ||
|
||
$collectorDefinition = $container->getDefinition('data_collector.cache'); | ||
$serviceIds = $container->findTaggedServiceIds('cache.pool'); | ||
|
||
foreach (array_keys($serviceIds) as $id) { | ||
if ($container->getDefinition($id)->isAbstract()) { | ||
continue; | ||
} | ||
|
||
$container->register($id.'.recorder', TraceableAdapter::class) | ||
->setDecoratedService($id) | ||
->addArgument(new Reference($id.'.recorder.inner')) | ||
->setPublic(false); | ||
|
||
// Tell the collector to add the new instance | ||
$collectorDefinition->addMethodCall('addInstance', array($id, new Reference($id))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about reporting the "clearer", "provider", "namespace" and/or "default_lifetime" attributes when available? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note also that this will prevent private services from being removed at compile time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sure. But can that be left out of this PR as a future improvement?
What do you prefer? Should I change the order of the passes |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<!-- DataCollector --> | ||
<service id="data_collector.cache" class="Symfony\Component\Cache\DataCollector\CacheDataCollector"> | ||
<tag name="data_collector" template="@WebProfiler/Collector/cache.html.twig" id="cache" priority="275" /> | ||
</service> | ||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
{% extends 'WebProfilerBundle:Profiler:layout.html.twig' %} | ||
|
||
{% block toolbar %} | ||
{% if collector.totals.calls > 0 %} | ||
{% set icon %} | ||
{{ include('@WebProfiler/Icon/cache.svg') }} | ||
<span class="sf-toolbar-value">{{ collector.totals.calls }}</span> | ||
<span class="sf-toolbar-info-piece-additional-detail"> | ||
<span class="sf-toolbar-label">in</span> | ||
<span class="sf-toolbar-value">{{ '%0.2f'|format(collector.totals.time * 1000) }}</span> | ||
<span class="sf-toolbar-label">ms</span> | ||
</span> | ||
{% endset %} | ||
{% set text %} | ||
<div class="sf-toolbar-info-piece"> | ||
<b>Cache Calls</b> | ||
<span>{{ collector.totals.calls }}</span> | ||
</div> | ||
<div class="sf-toolbar-info-piece"> | ||
<b>Total time</b> | ||
<span>{{ '%0.2f'|format(collector.totals.time * 1000) }} ms</span> | ||
</div> | ||
<div class="sf-toolbar-info-piece"> | ||
<b>Cache hits</b> | ||
<span>{{ collector.totals.hits }}/{{ collector.totals.reads }} ({{ collector.totals['hits/reads'] }})</span> | ||
</div> | ||
<div class="sf-toolbar-info-piece"> | ||
<b>Cache writes</b> | ||
<span>{{ collector.totals.writes }}</span> | ||
</div> | ||
{% endset %} | ||
{% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %} | ||
{% endif %} | ||
{% endblock %} | ||
|
||
{% block menu %} | ||
<span class="label {{ collector.totals.calls == 0 ? 'disabled' }}"> | ||
<span class="icon"> | ||
{{ include('@WebProfiler/Icon/cache.svg') }} | ||
</span> | ||
<strong>Cache</strong> | ||
<span class="count"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should have the time here anymore, for consistency with other panels There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want me to remove it? |
||
<span>{{ collector.totals.calls }}</span> | ||
<span>{{ '%0.2f'|format(collector.totals.time * 1000) }} ms</span> | ||
</span> | ||
</span> | ||
{% endblock %} | ||
|
||
{% block panel %} | ||
<h2>Cache</h2> | ||
{% for name, calls in collector.calls %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be great to have the aggregated stats first as big numbers (as done in other panels), before having the detail for each pool There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no it is not done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
<h3>Statistics for '{{ name }}'</h3> | ||
<div class="metrics"> | ||
{% for key, value in collector.statistics[name] %} | ||
<div class="metric"> | ||
<span class="value"> | ||
{% if key == 'time' %} | ||
<td>{{ '%0.2f'|format(1000*value) }} ms</td> | ||
{% else %} | ||
<td>{{ value }}</td> | ||
{% endif %} | ||
</span> | ||
<span class="label">{{ key|capitalize }}</span> | ||
</div> | ||
{% endfor %} | ||
</div> | ||
<h4>Calls for '{{ name }}'</h4> | ||
|
||
{% if not collector.totals.calls %} | ||
<p> | ||
<em>No calls.</em> | ||
</p> | ||
{% else %} | ||
<table> | ||
<thead> | ||
<tr> | ||
<th style="width: 5rem;">Key</th> | ||
<th>Value</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{% for i, call in calls %} | ||
<tr> | ||
<th style="padding-top:2rem">#{{ i }}</th> | ||
<th style="padding-top:2rem">Pool::{{ call.name }}</th> | ||
</tr> | ||
<tr> | ||
<th>Argument</th> | ||
<td>{{ profiler_dump(call.argument, maxDepth=2) }}</td> | ||
</tr> | ||
<tr> | ||
<th>Results</th> | ||
<td> | ||
{% if call.result != false %} | ||
{{ profiler_dump(call.result, maxDepth=1) }} | ||
{% endif %} | ||
</td> | ||
</tr> | ||
<tr> | ||
<th>Time</th> | ||
<td>{{ '%0.2f'|format((call.end - call.start) * 1000) }} ms</td> | ||
</tr> | ||
|
||
{% endfor %} | ||
</tbody> | ||
|
||
</table> | ||
{% endif %} | ||
{% endfor %} | ||
|
||
{% endblock %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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; | ||
|
||
/** | ||
* @author Aaron Scherer <aequasi@gmail.com> | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
class CacheDataCollector extends DataCollector | ||
{ | ||
/** | ||
* @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) { | ||
$calls = $instance->getCalls(); | ||
foreach ($calls as $call) { | ||
if (isset($call->result)) { | ||
$call->result = $this->cloneVar($call->result); | ||
} | ||
if (isset($call->argument)) { | ||
$call->argument = $this->cloneVar($call->argument); | ||
} | ||
} | ||
$this->data['instances']['calls'][$name] = $calls; | ||
} | ||
|
||
$this->data['instances']['statistics'] = $this->calculateStatistics(); | ||
$this->data['total']['statistics'] = $this->calculateTotalStatistics(); | ||
} | ||
|
||
/** | ||
* {@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, | ||
'hits' => 0, | ||
'misses' => 0, | ||
'writes' => 0, | ||
'deletes' => 0, | ||
); | ||
/** @var TraceableAdapterEvent $call */ | ||
foreach ($calls as $call) { | ||
$statistics[$name]['calls'] += 1; | ||
$statistics[$name]['time'] += $call->end - $call->start; | ||
if ($call->name === 'getItem') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about getItems? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. If one do getItems(['a', 'b', 'c']). I will count that as 1 call and 3 reads. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yoda style everywhere please |
||
$statistics[$name]['reads'] += 1; | ||
if ($call->hits) { | ||
$statistics[$name]['hits'] += 1; | ||
} else { | ||
$statistics[$name]['misses'] += 1; | ||
} | ||
} elseif ($call->name === 'getItems') { | ||
$count = $call->hits + $call->misses; | ||
$statistics[$name]['reads'] += $count; | ||
$statistics[$name]['hits'] += $call->hits; | ||
$statistics[$name]['misses'] += $count - $call->misses; | ||
} elseif ($call->name === 'hasItem') { | ||
$statistics[$name]['reads'] += 1; | ||
if ($call->result === false) { | ||
$statistics[$name]['misses'] += 1; | ||
} | ||
} elseif ($call->name === 'save') { | ||
$statistics[$name]['writes'] += 1; | ||
} elseif ($call->name === 'deleteItem') { | ||
$statistics[$name]['deletes'] += 1; | ||
} | ||
} | ||
if ($statistics[$name]['reads']) { | ||
$statistics[$name]['hits/reads'] = round(100 * $statistics[$name]['hits'] / $statistics[$name]['reads'], 2).'%'; | ||
} else { | ||
$statistics[$name]['hits/reads'] = 'N/A'; | ||
} | ||
} | ||
|
||
return $statistics; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
private function calculateTotalStatistics() | ||
{ | ||
$statistics = $this->getStatistics(); | ||
$totals = array('calls' => 0, 'time' => 0, 'reads' => 0, 'hits' => 0, 'misses' => 0, 'writes' => 0); | ||
foreach ($statistics as $name => $values) { | ||
foreach ($totals as $key => $value) { | ||
$totals[$key] += $statistics[$name][$key]; | ||
} | ||
} | ||
if ($totals['reads']) { | ||
$totals['hits/reads'] = round(100 * $totals['hits'] / $totals['reads'], 2).'%'; | ||
} else { | ||
$totals['hits/reads'] = 'N/A'; | ||
} | ||
|
||
return $totals; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $service) {
, this will allow you to remove the call to getDefinition just belowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relic from old code i believe. Good to change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.