Add call summaries by type and key in cache profiler

This commit is contained in:
Thibaud Fabre
2016-01-20 12:09:39 +01:00
parent f5ef76168f
commit 2b7d5e382f
5 changed files with 124 additions and 2 deletions

View File

@@ -42,6 +42,21 @@ class CacheStatisticsSubscriber implements EventSubscriberInterface
return '[ root ]'; return '[ root ]';
} }
public function getCallSummary()
{
if ($this->cache instanceof TraceableCache) {
return $this->cache->getSummary();
}
return [
'calls' => 0,
'hits' => 0,
'misses' => 0,
'calls_by_type' => [],
'calls_by_key' => []
];
}
public function getCalls() public function getCalls()
{ {
if ($this->cache instanceof TraceableCache) { if ($this->cache instanceof TraceableCache) {

View File

@@ -35,6 +35,11 @@ class CacheDataCollector implements DataCollectorInterface
*/ */
private $calls = []; private $calls = [];
/**
* @var array
*/
private $callSummary;
/** /**
* @param CacheStatisticsSubscriber $cacheStatisticsSubscriber * @param CacheStatisticsSubscriber $cacheStatisticsSubscriber
*/ */
@@ -73,6 +78,7 @@ class CacheDataCollector implements DataCollectorInterface
); );
$this->calls = $this->statsListener->getCalls(); $this->calls = $this->statsListener->getCalls();
$this->callSummary = $this->statsListener->getCallSummary();
} }
/** /**
@@ -96,6 +102,11 @@ class CacheDataCollector implements DataCollectorInterface
return $this->calls; return $this->calls;
} }
public function getCallSummary()
{
return $this->callSummary;
}
/** /**
* @return CacheProfileSummary * @return CacheProfileSummary
*/ */

View File

@@ -24,7 +24,15 @@ class TraceableCache implements Cache, PhraseaCache
*/ */
private $calls = []; private $calls = [];
/** private $summary = [
'calls' => 0,
'hits' => 0,
'misses' => 0,
'calls_by_type' => [],
'calls_by_key' => [],
];
/*s*
* @param PhraseaCache $cache * @param PhraseaCache $cache
*/ */
public function __construct(PhraseaCache $cache) public function __construct(PhraseaCache $cache)
@@ -34,12 +42,29 @@ class TraceableCache implements Cache, PhraseaCache
private function collect($type, $id, $hit = true, $result = null) private function collect($type, $id, $hit = true, $result = null)
{ {
$this->summary['calls']++;
$this->summary['hits'] += $hit ? 1 : 0;
$this->summary['misses'] += $hit ? 0 : 1;
if (! array_key_exists($type, $this->summary['calls_by_type'])) {
$this->summary['calls_by_type'][$type] = 0;
}
$this->summary['calls_by_type'][$type]++;
if (! array_key_exists($id, $this->summary['calls_by_key'])) {
$this->summary['calls_by_key'][$id] = 0;
}
$this->summary['calls_by_key'][$id]++;
$this->calls[] = [ $this->calls[] = [
'type' => $type, 'type' => $type,
'key' => $id, 'key' => $id,
'result' => $result, 'result' => $result,
'hit' => (bool) $hit 'hit' => (bool) $hit
]; ];
} }
/** /**
@@ -58,6 +83,14 @@ class TraceableCache implements Cache, PhraseaCache
return $this->calls; return $this->calls;
} }
/**
* @return array
*/
public function getSummary()
{
return $this->summary;
}
/** /**
* Fetches an entry from the cache. * Fetches an entry from the cache.
* *

View File

@@ -75,7 +75,7 @@ class WebProfilerServiceProvider implements ServiceProviderInterface
return new CacheStatisticsSubscriber($app['cache']); return new CacheStatisticsSubscriber($app['cache']);
}); });
$app->share($app->extend('data_collectors', function ($collectors) use ($app) { $app['data_collectors'] = $app->share($app->extend('data_collectors', function ($collectors) use ($app) {
$collectors['db'] = $app->share(function ($app) { $collectors['db'] = $app->share(function ($app) {
/** @var DoctrineDataCollector $collector */ /** @var DoctrineDataCollector $collector */
$collector = $app['data_collectors.doctrine']; $collector = $app['data_collectors.doctrine'];

View File

@@ -80,6 +80,69 @@
</tr> </tr>
</table> </table>
<h3>Hit miss summary</h3>
<table>
<thead>
<tr>
<th>Call stat</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Hits</th>
<td>{{ collector.callSummary['hits'] }}</td>
</tr>
<tr>
<th scope="row">Misses</th>
<td>{{ collector.callSummary['misses'] }}</td>
</tr>
<tr>
<th scope="row">Total</th>
<td>{{ collector.callSummary['calls'] }}</td>
</tr>
</tbody>
</table>
<h3>Calls by type summary</h3>
<table>
<thead>
<tr>
<th>Operation</th>
<th>Count</th>
</tr>
</thead>
<tbody>
{% for callType, count in collector.callSummary['calls_by_type'] %}
<tr>
<td>{{ callType }}</td>
<td>{{ count }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h3>Calls by key summary</h3>
<table>
<thead>
<tr>
<th>Operation</th>
<th>Count</th>
</tr>
</thead>
<tbody>
{% for callKey, count in collector.callSummary['calls_by_key'] %}
<tr>
<td>{{ callKey }}</td>
<td>{{ count }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h3>Call list</h3> <h3>Call list</h3>
<table> <table>