8000 [Cache] fix collecting cache stats when nesting computations by nicolas-grekas · Pull Request #49031 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] fix collecting cache stats when nesting computations #49031

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

Merged
merged 1 commit into from
Jan 20, 2023
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
[Cache] fix collecting cache stats when nesting computations
  • Loading branch information
nicolas-grekas committed Jan 19, 2023
commit 52a0e43a977ca9df442c2a17e2d4d0c11a6ec791
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private function calculateStatistics(): array
/** @var TraceableAdapterEvent $call */
foreach ($calls as $call) {
++$statistics[$name]['calls'];
$statistics[$name]['time'] += $call->end - $call->start;
$statistics[$name]['time'] += ($call->end ?? microtime(true)) - $call->start;
if ('get' === $call->name) {
++$statistics[$name]['reads'];
if ($call->hits) {
Expand All @@ -136,10 +136,8 @@ private function calculateStatistics(): array
$statistics[$name]['misses'] += $call->misses;
} elseif ('hasItem' === $call->name) {
++$statistics[$name]['reads'];
if (false === $call->result) {
++$statistics[$name]['misses'];
} else {
++$statistics[$name]['hits'];
foreach ($call->result ?? [] as $result) {
++$statistics[$name][$result ? 'hits' : 'misses'];
}
} elseif ('save' === $call->name) {
++$statistics[$name]['writes'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Cache\Tests\DataCollector;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\Cache\Adapter\TraceableAdapter;
use Symfony\Component\Cache\DataCollector\CacheDataCollector;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -55,16 +56,16 @@ public function testHitedEventDataCollector()
$traceableAdapterEvent->name = 'hasItem';
$traceableAdapterEvent->start = 0;
$traceableAdapterEvent->end = 0;
$traceableAdapterEvent->hits = 1;
$traceableAdapterEvent->hits = 0;
$traceableAdapterEvent->misses = 0;
$traceableAdapterEvent->result = ['foo' => false];

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

$this->assertEquals($statistics[self::INSTANCE_NAME]['calls'], 1, 'calls');
$this->assertEquals($statistics[self::INSTANCE_NAME]['reads'], 1, 'reads');
$this->assertEquals($statistics[self::INSTANCE_NAME]['hits'], 1, 'hits');
$this->assertEquals($statistics[self::INSTANCE_NAME]['misses'], 0, 'misses');
$this->assertEquals($statistics[self::INSTANCE_NAME]['hits'], 0, 'hits');
$this->assertEquals($statistics[self::INSTANCE_NAME]['misses'], 1, 'misses');
$this->assertEquals($statistics[self::INSTANCE_NAME]['writes'], 0, 'writes');
}

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

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

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

$adapter->get('foo', function () use ($collector) {
$collector->collect(new Request(), new Response());

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');
}

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