function PerformanceTest::testLogin

Tests performance of the navigation toolbar.

File

core/modules/navigation/tests/src/FunctionalJavascript/PerformanceTest.php, line 49

Class

PerformanceTest
Tests performance with the navigation toolbar enabled.

Namespace

Drupal\Tests\navigation\FunctionalJavascript

Code

public function testLogin() : void {
  $user = $this->drupalCreateUser();
  $user->addRole('administrator');
  $user->save();
  $this->drupalLogin($user);
  // Request the front page twice to ensure all cache collectors are fully
  // warmed. The exact contents of cache collectors depends on the order in
  // which requests complete so this ensures that the second request completes
  // after asset aggregates are served.
  $this->drupalGet('');
  sleep(1);
  $this->drupalGet('');
  // Flush the dynamic page cache to simulate visiting a page that is not
  // already fully cached.
  \Drupal::cache('dynamic_page_cache')->deleteAll();
  $performance_data = $this->collectPerformanceData(function () {
    $this->drupalGet('');
  }, 'navigation');
  $expected_queries = [
    'SELECT "session" FROM "sessions" WHERE "sid" = "SESSION_ID" LIMIT 0, 1',
    'SELECT * FROM "users_field_data" "u" WHERE "u"."uid" = "2" AND "u"."default_langcode" = 1',
    'SELECT "roles_target_id" FROM "user__roles" WHERE "entity_id" = "2"',
    'SELECT "name", "value" FROM "key_value" WHERE "name" IN ( "theme:stark" ) AND "collection" = "config.entity.key_store.block"',
  ];
  $recorded_queries = $performance_data->getQueries();
  $this->assertSame($expected_queries, $recorded_queries);
  $expected = [
    'QueryCount' => 4,
    'CacheGetCount' => 45,
    'CacheGetCountByBin' => [
      'config' => 10,
      'data' => 4,
      'discovery' => 9,
      'bootstrap' => 8,
      'dynamic_page_cache' => 1,
      'render' => 12,
      'menu' => 1,
    ],
    'CacheSetCount' => 2,
    'CacheSetCountByBin' => [
      'dynamic_page_cache' => 2,
    ],
    'CacheDeleteCount' => 0,
    'CacheTagInvalidationCount' => 0,
    'CacheTagLookupQueryCount' => 8,
    'ScriptCount' => 3,
    'ScriptBytes' => 140745,
    'StylesheetCount' => 2,
    'StylesheetBytes' => 41524,
  ];
  $this->assertMetrics($expected, $performance_data);
  // Check that the navigation toolbar is cached without any high-cardinality
  // cache contexts (user, route, query parameters etc.).
  $this->assertIsObject(\Drupal::cache('render')->get('navigation:navigation:[languages:language_interface]=en:[theme]=stark:[user.permissions]=is-admin'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.