Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 10 additions & 10 deletions src/Symfony/Component/Cache/DataCollector/CacheDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,7 @@ public function addInstance(string $name, TraceableAdapter $instance): void

public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
{
$empty = ['calls' => [], 'adapters' => [], 'config' => [], 'options' => [], 'statistics' => []];
$this->data = ['instances' => $empty, 'total' => $empty];
foreach ($this->instances as $name => $instance) {
$this->data['instances']['calls'][$name] = $instance->getCalls();
$this->data['instances']['adapters'][$name] = get_debug_type($instance->getPool());
}

$this->data['instances']['statistics'] = $this->calculateStatistics();
$this->data['total']['statistics'] = $this->calculateTotalStatistics();
$this->lateCollect();
}

public function reset(): void
Expand All @@ -59,7 +51,15 @@ public function reset(): void

public function lateCollect(): void
{
$this->data['instances']['calls'] = $this->cloneVar($this->data['instances']['calls']);
$empty = ['calls' => [], 'adapters' => [], 'config' => [], 'options' => [], 'statistics' => []];
$this->data = ['instances' => $empty, 'total' => $empty];
foreach ($this->instances as $name => $instance) {
$this->data['instances']['calls'][$name] = $instance->getCalls();
$this->data['instances']['adapters'][$name] = get_debug_type($instance->getPool());
}

$this->data['instances']['statistics'] = $this->calculateStatistics();
$this->data['total']['statistics'] = $this->calculateTotalStatistics();
}

public function getName(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ public function testCollectBeforeEnd()
$this->assertEquals($stats[self::INSTANCE_NAME]['misses'], 1, 'misses');
}

public function testLateCollect()
{
$adapter = new TraceableAdapter(new NullAdapter());

$collector = new CacheDataCollector();
$collector->addInstance(self::INSTANCE_NAME, $adapter);

$adapter->get('foo', function () use ($collector) {
$collector->lateCollect();

return 123;
});

$stats = $collector->getStatistics();
$this->assertGreaterThan(0, $stats[self::INSTANCE_NAME]['time']);
$this->assertEquals($stats[self::INSTANCE_NAME]['hits'], 0, 'hits');
$this->assertEquals($stats[self::INSTANCE_NAME]['misses'], 1, 'misses');
$this->assertEquals($stats[self::INSTANCE_NAME]['calls'], 1, 'calls');
}

private function getCacheDataCollectorStatisticsFromEvents(array $traceableAdapterEvents)
{
$traceableAdapterMock = $this->createMock(TraceableAdapter::class);
Expand Down
Loading