Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added cache data collector and profiler page
  • Loading branch information
Nyholm committed Jan 14, 2017
commit 23c0039ed3917cd4e63a84678c932cf9e086067f
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) {
Copy link
Member

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 below

Copy link
Contributor

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

Copy link
Member Author

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) {
  // $service is an array of tags.
  var_dump($service);
}

array(2) {
  [0] =>
  array(0) {
  }
  [1] =>
  array(2) {
    'clearer' =>
    string(21) "cache.default_clearer"
    'unlazy' =>
    bool(true)
  }
}

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)));
Copy link
Member

@nicolas-grekas nicolas-grekas Jan 2, 2017

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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.
We have two options here: keep it this way so that even non-used private service could be seen in the panel,
or register this pass just before CachePoolClearerPass.

Copy link
Member Author

Choose a reason for hiding this comment

The 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?

Sure. But can that be left out of this PR as a future improvement?

or register this pass just before CachePoolClearerPass.

What do you prefer? Should I change the order of the passes

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $

$loader->load('profiling.xml');
$loader->load('collectors.xml');
$loader->load('cache_debug.xml');

if ($this->formConfigEnabled) {
$loader->load('form_debug.xml');
Expand Down
3 changes: 3 additions & 0 deletions 3 src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddDebugLogProcessorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CacheCollectorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolClearerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ControllerArgumentValueResolverPass;
Expand Down Expand Up @@ -105,6 +107,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new CompilerDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new ConfigCachePass());
$container->addCompilerPass(new CacheCollectorPass());
}
}
}
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">
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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 %}
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no it is not done

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I misunderstood you. I put the aggregated stats for each adapter as big numbers.
screen shot 2017-01-02 at 18 46 11

<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 %}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
175 changes: 175 additions & 0 deletions 175 src/Symfony/Component/Cache/DataCollector/CacheDataCollector.php
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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about getItems?

Copy link
Member Author

@Nyholm Nyholm Dec 28, 2016

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.