[Cache] Recognize commit events as writes in CacheDataCollector

This commit is contained in:
Ellie Schaffer
2025-11-12 21:59:09 -05:00
committed by Nicolas Grekas
parent f021d84781
commit 81eb494fc2
2 changed files with 42 additions and 0 deletions

View File

@@ -143,6 +143,8 @@ class CacheDataCollector extends DataCollector implements LateDataCollectorInter
}
} elseif ('save' === $call->name) {
++$statistics[$name]['writes'];
} elseif ('saveDeferred' === $call->name) {
++$statistics[$name]['writes'];
} elseif ('deleteItem' === $call->name) {
++$statistics[$name]['deletes'];
}

View File

@@ -86,6 +86,46 @@ class CacheDataCollectorTest extends TestCase
$this->assertEquals($statistics[self::INSTANCE_NAME]['writes'], 1, 'writes');
}
public function testSaveDeferredEventWithoutExplicitCommitDataCollector()
{
$traceableAdapterEvent = new \stdClass();
$traceableAdapterEvent->name = 'saveDeferred';
$traceableAdapterEvent->start = 0;
$traceableAdapterEvent->end = 0;
$statistics = $this->getCacheDataCollectorStatisticsFromEvents([$traceableAdapterEvent]);
$this->assertSame(1, $statistics[self::INSTANCE_NAME]['calls'], 'calls');
$this->assertSame(0, $statistics[self::INSTANCE_NAME]['reads'], 'reads');
$this->assertSame(0, $statistics[self::INSTANCE_NAME]['hits'], 'hits');
$this->assertSame(0, $statistics[self::INSTANCE_NAME]['misses'], 'misses');
$this->assertSame(1, $statistics[self::INSTANCE_NAME]['writes'], 'writes');
}
public function testSaveDeferredEventWithExplicitCommitDataCollector()
{
$traceableAdapterSaveDeferredEvent = new \stdClass();
$traceableAdapterSaveDeferredEvent->name = 'saveDeferred';
$traceableAdapterSaveDeferredEvent->start = 0;
$traceableAdapterSaveDeferredEvent->end = 0;
$traceableAdapterCommitEvent = new \stdClass();
$traceableAdapterCommitEvent->name = 'commit';
$traceableAdapterCommitEvent->start = 0;
$traceableAdapterCommitEvent->end = 0;
$statistics = $this->getCacheDataCollectorStatisticsFromEvents([
$traceableAdapterSaveDeferredEvent,
$traceableAdapterCommitEvent,
]);
$this->assertSame(2, $statistics[self::INSTANCE_NAME]['calls'], 'calls');
$this->assertSame(0, $statistics[self::INSTANCE_NAME]['reads'], 'reads');
$this->assertSame(0, $statistics[self::INSTANCE_NAME]['hits'], 'hits');
$this->assertSame(0, $statistics[self::INSTANCE_NAME]['misses'], 'misses');
$this->assertSame(1, $statistics[self::INSTANCE_NAME]['writes'], 'writes');
}
public function testCollectBeforeEnd()
{
$adapter = new TraceableAdapter(new NullAdapter());