Verified Commit d552da85 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3421881 by catch, kristiaanvandeneynde, Wim Leers: Track cache tag...

Issue #3421881 by catch, kristiaanvandeneynde, Wim Leers: Track cache tag queries separately in performance tests

(cherry picked from commit 07a3ac84)
parent 95c48c33
Loading
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -12,3 +12,8 @@ services:
    public: false
    decorates: cache_factory
    arguments: ['@performance_test.cache_factory.inner', '@Drupal\performance_test\PerformanceDataCollector']
  performance_test.cache_tags.invalidator.checksum:
    class: Drupal\performance_test\Cache\CacheTagsChecksumDecorator
    public: false
    decorates: cache_tags.invalidator.checksum
    arguments: ['@performance_test.cache_tags.invalidator.checksum.inner', '@Drupal\performance_test\PerformanceDataCollector']
+17 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\performance_test\Cache;

/**
 * The cache tag operations we are tracking as part of our performance data.
 *
 * @see \Drupal\Core\Cache\CacheTagsChecksumInterface
 * @see \Drupal\Core\Cache\CacheTagsInvalidatorInterface
 */
enum CacheTagOperation {
  case getCurrentChecksum;
  case invalidateTags;
  case isValid;
}
+81 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\performance_test\Cache;

use Drupal\Core\Cache\CacheTagsChecksumInterface;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\performance_test\PerformanceDataCollector;

/**
 * Wraps an existing cache tags checksum invalidator to track calls separately.
 */
class CacheTagsChecksumDecorator implements CacheTagsChecksumInterface, CacheTagsInvalidatorInterface {

  public function __construct(protected readonly CacheTagsChecksumInterface $checksumInvalidator, protected readonly PerformanceDataCollector $performanceDataCollector) {}

  /**
   * {@inheritdoc}
   */
  public function getCurrentChecksum(array $tags) {
    $start = microtime(TRUE);
    $return = $this->checksumInvalidator->getCurrentChecksum($tags);
    $stop = microtime(TRUE);
    $this->logCacheTagOperation($tags, $start, $stop, CacheTagOperation::getCurrentChecksum);
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  public function isValid($checksum, array $tags) {
    $start = microtime(TRUE);
    $return = $this->checksumInvalidator->isValid($checksum, $tags);
    $stop = microtime(TRUE);
    $this->logCacheTagOperation($tags, $start, $stop, CacheTagOperation::isValid);
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  public function invalidateTags(array $tags) {
    $start = microtime(TRUE);
    $return = $this->checksumInvalidator->invalidateTags($tags);
    $stop = microtime(TRUE);
    $this->logCacheTagOperation($tags, $start, $stop, CacheTagOperation::invalidateTags);
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  public function reset() {
    $this->checksumInvalidator->reset();
  }

  /**
   * Logs a cache tag operation.
   *
   * @param string[] $tags
   *   The cache tags.
   * @param float $start
   *   The start microtime.
   * @param float $stop
   *   The stop microtime.
   * @param \Drupal\performance_test\Cache\CacheTagOperation $operation
   *   The type of operation being logged.
   *
   * @return void
   */
  protected function logCacheTagOperation(array $tags, float $start, float $stop, CacheTagOperation $operation): void {
    $this->performanceDataCollector->addCacheTagOperation([
      'operation' => $operation,
      'tags' => implode(', ', $tags),
      'start' => $start,
      'stop' => $stop,
    ]);
  }

}
+14 −0
Original line number Diff line number Diff line
@@ -20,6 +20,11 @@ class PerformanceDataCollector implements EventSubscriberInterface, Destructable
   */
  protected array $cacheOperations = [];

  /**
   * Cache tag operations collected during the request.
   */
  protected array $cacheTagOperations = [];

  /**
   * {@inheritdoc}
   */
@@ -44,6 +49,13 @@ public function addCacheOperation(array $operation) {
    $this->cacheOperations[] = $operation;
  }

  /**
   * Adds a cache tag operation.
   */
  public function addCacheTagOperation(array $operation) {
    $this->cacheTagOperations[] = $operation;
  }

  /**
   * {@inheritdoc}
   */
@@ -65,9 +77,11 @@ public function destruct(): void {
    $existing_data = $collection->get('performance_test_data') ?? [
      'database_events' => [],
      'cache_operations' => [],
      'cache_tag_operations' => [],
    ];
    $existing_data['database_events'] = array_merge($existing_data['database_events'], $database_events);
    $existing_data['cache_operations'] = array_merge($existing_data['cache_operations'], $this->cacheOperations);
    $existing_data['cache_tag_operations'] = array_merge($existing_data['cache_tag_operations'], $this->cacheTagOperations);
    $collection->set('performance_test_data', $existing_data);
    $lock->release('performance_test');
  }
+5 −2
Original line number Diff line number Diff line
@@ -35,11 +35,14 @@ public function testFrontPageAuthenticatedWarmCache(): void {
    $performance_data = $this->collectPerformanceData(function () {
      $this->drupalGet('<front>');
    }, 'authenticatedFrontPage');
    $this->assertGreaterThanOrEqual(15, $performance_data->getQueryCount());
    $this->assertLessThanOrEqual(17, $performance_data->getQueryCount());
    $this->assertGreaterThanOrEqual(10, $performance_data->getQueryCount());
    $this->assertLessThanOrEqual(12, $performance_data->getQueryCount());
    $this->assertSame(45, $performance_data->getCacheGetCount());
    $this->assertSame(0, $performance_data->getCacheSetCount());
    $this->assertSame(0, $performance_data->getCacheDeleteCount());
    $this->assertSame(0, $performance_data->getCacheTagChecksumCount());
    $this->assertSame(54, $performance_data->getCacheTagIsValidCount());
    $this->assertSame(0, $performance_data->getCacheTagInvalidationCount());
  }

}
Loading