8000 bug #49031 [Cache] fix collecting cache stats when nesting computatio… · symfony/symfony@39cd93a · GitHub
[go: up one dir, main page]

Skip to content

Commit 39cd93a

Browse files
bug #49031 [Cache] fix collecting cache stats when nesting computations (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- [Cache] fix collecting cache stats when nesting computations | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #48910 | License | MIT | Doc PR | - Commits ------- 52a0e43 [Cache] fix collecting cache stats when nesting computations
2 parents dd30a08 + 52a0e43 commit 39cd93a

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/Symfony/Component/Cache/DataCollector/CacheDataCollector.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private function calculateStatistics(): array
114114
/** @var TraceableAdapterEvent $call */
115115
foreach ($calls as $call) {
116116
++$statistics[$name]['calls'];
117-
$statistics[$name]['time'] += $call->end - $call->start;
117+
$statistics[$name]['time'] += ($call->end ?? microtime(true)) - $call->start;
118118
if ('get' === $call->name) {
119119
++$statistics[$name]['reads'];
120120
if ($call->hits) {
@@ -136,10 +136,8 @@ private function calculateStatistics(): array
136136
$statistics[$name]['misses'] += $call->misses;
137137
} elseif ('hasItem' === $call->name) {
138138
++$statistics[$name]['reads'];
139-
if (false === $call->result) {
140-
++$statistics[$name]['misses'];
141-
} else {
142-
++$statistics[$name]['hits'];
139+
foreach ($call->result ?? [] as $result) {
140+
++$statistics[$name][$result ? 'hits' : 'misses'];
143141
}
144142
} elseif ('save' === $call->name) {
145143
++$statistics[$name]['writes'];

src/Symfony/Component/Cache/Tests/DataCollector/CacheDataCollectorTest.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Cache\Tests\DataCollector;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Cache\Adapter\NullAdapter;
1516
use Symfony\Component\Cache\Adapter\TraceableAdapter;
1617
use Symfony\Component\Cache\DataCollector\CacheDataCollector;
1718
use Symfony\Component\HttpFoundation\Request;
@@ -55,16 +56,16 @@ public function testHitedEventDataCollector()
5556
$traceableAdapterEvent->name = 'hasItem';
5657
$traceableAdapterEvent->start = 0;
5758
$traceableAdapterEvent->end = 0;
58-
$traceableAdapterEvent->hits = 1;
59+
$traceableAdapterEvent->hits = 0;
5960
$traceableAdapterEvent->misses = 0;
6061
$traceableAdapterEvent->result = ['foo' => false];
6162

6263
$statistics = $this->getCacheDataCollectorStatisticsFromEvents([$traceableAdapterEvent]);
6364

6465
$this->assertEquals($statistics[self::INSTANCE_NAME]['calls'], 1, 'calls');
6566
$this->assertEquals($statistics[self::INSTANCE_NAME]['reads'], 1, 'reads');
66-
$this->assertEquals($statistics[self::INSTANCE_NAME]['hits'], 1, 'hits');
67-
$this->assertEquals($statistics[self::INSTANCE_NAME]['misses'], 0, 'misses');
67+
$this->assertEquals($statistics[self::INSTANCE_NAME]['hits'], 0, 'hits');
68+
$this->assertEquals($statistics[self::INSTANCE_NAME]['misses'], 1, 'misses');
6869
$this->assertEquals($statistics[self::INSTANCE_NAME]['writes'], 0, 'writes');
6970
}
7071

@@ -84,6 +85,25 @@ public function testSavedEventDataCollector()
8485
$this->assertEquals($statistics[self::INSTANCE_NAME]['writes'], 1, 'writes');
8586
}
8687

88+
public function testCollectBeforeEnd()
89+
{
90+
$adapter = new TraceableAdapter(new NullAdapter());
91+
92+
$collector = new CacheDataCollector();
93+
$collector->addInstance(self::INSTANCE_NAME, $adapter);
94+
95+
$adapter->get('foo', function () use ($collector) {
96+
$collector->collect(new Request(), new Response());
97+
98+
return 123;
99+
});
100+
101+
$stats = $collector->getStatistics();
102+
$this->assertGreaterThan(0, $stats[self::INSTANCE_NAME]['time']);
103+
$this->assertEquals($stats[self::INSTANCE_NAME]['hits'], 0, 'hits');
104+
$this->assertEquals($stats[self::INSTANCE_NAME]['misses'], 1, 'misses');
105+
}
106+
87107
private function getCacheDataCollectorStatisticsFromEvents(array $traceableAdapterEvents)
88108
{
89109
$traceableAdapterMock = $this->createMock(TraceableAdapter::class);

0 commit comments

Comments
 (0)
0