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

Issue #3444060 by longwave, catch, mondrake: Implement failed database...

Issue #3444060 by longwave, catch, mondrake: Implement failed database statement events in performance test query logging

(cherry picked from commit 970ff6e9)
parent 4ed57d45
Loading
Loading
Loading
Loading
Loading
+2 −9
Original line number Diff line number Diff line
@@ -3,11 +3,10 @@
namespace Drupal\performance_test;

use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Event\StatementEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Database\Event\StatementExecutionEndEvent;
use Drupal\Core\Database\Event\StatementExecutionStartEvent;

class DatabaseEventEnabler implements HttpKernelInterface {

@@ -18,13 +17,7 @@ public function __construct(protected readonly HttpKernelInterface $httpKernel,
   */
  public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE): Response {
    if ($type === static::MAIN_REQUEST) {
      $this->connection->enableEvents([
        // StatementExecutionStartEvent must be enabled in order for
        // StatementExecutionEndEvent to be fired, even though we only subscribe
        // to the latter event.
        StatementExecutionStartEvent::class,
        StatementExecutionEndEvent::class,
      ]);
      $this->connection->enableEvents(StatementEvent::all());
    }
    return $this->httpKernel->handle($request, $type, $catch);
  }
+6 −3
Original line number Diff line number Diff line
@@ -2,7 +2,9 @@

namespace Drupal\performance_test;

use Drupal\Core\Database\Event\DatabaseEvent;
use Drupal\Core\Database\Event\StatementExecutionEndEvent;
use Drupal\Core\Database\Event\StatementExecutionFailureEvent;
use Drupal\Core\DestructableInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

@@ -11,7 +13,7 @@ class PerformanceDataCollector implements EventSubscriberInterface, Destructable
  /**
   * Database events collected during the request.
   *
   * @var Drupal\Core\Database\Event\StatementExecutionEndEvent[]
   * @var \Drupal\Core\Database\Event\DatabaseEvent[]
   */
  protected array $databaseEvents = [];

@@ -30,14 +32,15 @@ class PerformanceDataCollector implements EventSubscriberInterface, Destructable
   */
  public static function getSubscribedEvents(): array {
    return [
      StatementExecutionEndEvent::class => 'onStatementExecutionEnd',
      StatementExecutionEndEvent::class => 'onDatabaseEvent',
      StatementExecutionFailureEvent::class => 'onDatabaseEvent',
    ];
  }

  /**
   * Logs database statements.
   */
  public function onStatementExecutionEnd(StatementExecutionEndEvent $event): void {
  public function onDatabaseEvent(DatabaseEvent $event): void {
    // Use the event object as a value object.
    $this->databaseEvents[] = $event;
  }
+4 −2
Original line number Diff line number Diff line
@@ -202,6 +202,7 @@ public function testLogin(): void {
    }, 'standardLogin');

    $expected_queries = [
      'SELECT "name", "value" FROM "key_value_expire" WHERE "expire" > "NOW" AND "name" IN ( "KEY" ) AND "collection" = "form"',
      'SELECT COUNT(*) AS "expression" FROM (SELECT 1 AS "expression" FROM "flood" "f" WHERE ("event" = "user.failed_login_ip") AND ("identifier" = "CLIENT_IP") AND ("timestamp" > "TIMESTAMP")) "subquery"',
      'SELECT "base_table"."uid" AS "uid", "base_table"."uid" AS "base_table_uid" FROM "users" "base_table" INNER JOIN "users_field_data" "users_field_data" ON "users_field_data"."uid" = "base_table"."uid" WHERE ("users_field_data"."name" IN ("ACCOUNT_NAME")) AND ("users_field_data"."default_langcode" IN (1))',
      'SELECT COUNT(*) AS "expression" FROM (SELECT 1 AS "expression" FROM "flood" "f" WHERE ("event" = "user.failed_login_user") AND ("identifier" = "CLIENT_IP") AND ("timestamp" > "TIMESTAMP")) "subquery"',
@@ -220,7 +221,7 @@ public function testLogin(): void {
    ];
    $recorded_queries = $performance_data->getQueries();
    $this->assertSame($expected_queries, $recorded_queries);
    $this->assertSame(15, $performance_data->getQueryCount());
    $this->assertSame(16, $performance_data->getQueryCount());
    $this->assertSame(60, $performance_data->getCacheGetCount());
    $this->assertSame(1, $performance_data->getCacheSetCount());
    $this->assertSame(1, $performance_data->getCacheDeleteCount());
@@ -256,6 +257,7 @@ public function testLoginBlock(): void {
    $expected_queries = [
      'SELECT "name", "value" FROM "key_value" WHERE "name" IN ( "theme:stark" ) AND "collection" = "config.entity.key_store.block"',
      'SELECT "config"."name" AS "name" FROM "config" "config" WHERE ("collection" = "") AND ("name" LIKE "search.page.%" ESCAPE ' . "'\\\\'" . ') ORDER BY "collection" ASC, "name" ASC',
      'SELECT "name", "value" FROM "key_value_expire" WHERE "expire" > "NOW" AND "name" IN ( "KEY" ) AND "collection" = "form"',
      'SELECT COUNT(*) AS "expression" FROM (SELECT 1 AS "expression" FROM "flood" "f" WHERE ("event" = "user.failed_login_ip") AND ("identifier" = "CLIENT_IP") AND ("timestamp" > "TIMESTAMP")) "subquery"',
      'SELECT "base_table"."uid" AS "uid", "base_table"."uid" AS "base_table_uid" FROM "users" "base_table" INNER JOIN "users_field_data" "users_field_data" ON "users_field_data"."uid" = "base_table"."uid" WHERE ("users_field_data"."name" IN ("ACCOUNT_NAME")) AND ("users_field_data"."default_langcode" IN (1))',
      'SELECT "base"."uid" AS "uid", "base"."uuid" AS "uuid", "base"."langcode" AS "langcode" FROM "users" "base" WHERE "base"."uid" IN (2)',
@@ -274,7 +276,7 @@ public function testLoginBlock(): void {
    ];
    $recorded_queries = $performance_data->getQueries();
    $this->assertSame($expected_queries, $recorded_queries);
    $this->assertSame(17, $performance_data->getQueryCount());
    $this->assertSame(18, $performance_data->getQueryCount());
    $this->assertSame(107, $performance_data->getCacheGetCount());
    $this->assertSame(1, $performance_data->getCacheSetCount());
    $this->assertSame(1, $performance_data->getCacheDeleteCount());
+4 −0
Original line number Diff line number Diff line
@@ -243,6 +243,10 @@ protected static function logQuery(PerformanceData $performance_data, string $qu
        $args[':db_condition_placeholder_1'] = 'CSS_FILE';
      }
    }
    elseif (str_starts_with($query, 'SELECT "name", "value" FROM "key_value_expire" WHERE "expire" >')) {
      $args[':now'] = 'NOW';
      $args[':keys__0'] = 'KEY';
    }

    // Inline query arguments and log the query.
    $query = str_replace(array_keys($args), array_values(static::quoteQueryArgs($args)), $query);